阿里云服务器发送邮件失败

2019 精帖
0 967

阿里云esc服务器为了安全默认禁用25端口,所以会导致 JavaMail 发送邮件失败。

错误代码:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163.com, 25; timeout -1;
  nested exception is:
	java.net.ConnectException: Connection timed out: connect
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
	at javax.mail.Service.connect(Service.java:388)
	at javax.mail.Service.connect(Service.java:246)
	at javax.mail.Service.connect(Service.java:195)
	at javax.mail.Transport.send0(Transport.java:254)
	at javax.mail.Transport.send(Transport.java:124)
	at test.Test.sendMail(Test.java:45)
	at test.Test.main(Test.java:18)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:353)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:239)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
	... 8 more

那么我们除了可以向阿里云申请解封25端口外,还可以使用ssl加密465端口发送邮件;

配置如下:

		Properties props=new Properties();
		props.setProperty("mail.smtp.host", "smtp.163.com");
		props.put("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		props.setProperty("mail.smtp.port", "465");

验证成功!

留言(0)
加载更多
猜你喜欢