javax.mail.MessagingException: Could not connect to SMTP host: smtp.sinzhe.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out (Connection timed out)
javax.mail.MessagingException: Could not connect to SMTP host: smtp.sinzhe.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out (Connection timed out)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:297)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transpor
Caused by: java.net.ConnectException: Connection timed out (Connection timed out)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 88 more
阿里云服务器发送邮件,不能发送成功,在本地测试通过的,一番考究之后,是因为阿里云“为了安全”默认将25端口关闭了,一大堆申请开通25端口的教程,别费劲!你没那么大脸,还是老老实实换个思路吧,比如走其他端口
package cn.com.suntree.cmp.utils;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MaliUtils {
//发送邮箱验证码,to 接收方邮箱号码 code:验证码
public static boolean sendMail(String to, String code) {
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.sinzhe.com");//自己邮箱对应host
props.put("mail.transport.protocol", "smtp");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable", "true");
//不用默认的方式,就需要天加如下两行代码
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");//ssl方式
props.put("mail.smtp.socketFactory.port", "465");//发送端口 默认是25
// 访问SMTP服务时需要提供的密码
props.put("mail.user", "发送方邮箱地址");//xxxx@sinzhe.com
props.put("mail.password", "发送方邮箱对应密码");
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人
InternetAddress toMail = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toMail);
// 设置邮件标题
message.setSubject("邮件主题");
// 设置邮件的内容体
message.setContent("邮件正文!","text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
return false;
}
return true;
}
}