Java mail 报错 trying to connect to host "*.*.com", port 587, isSSL true的另一种解决办法

1
2
3
4
DEBUG SMTP: trying to connect to host "xxx.xxx.com", port 587, isSSL true
send failed... the exception is javax.mail.MessagingException: Could not connect to SMTP host: xxx.xxx.com, port: 587;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

如果你遇到了这种报错,而其它办法又解决不了。
不妨试试调整properties的put()方法放置顺序吧。
起初我也觉得put()顺序并不会影响到后续的其他方法块对它的读取啊。
然而当我调转了它们的位置时,不再报错了。。
邮件成功的发送出去了。

这是我放置properties的方法块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MailSSLSocketFactory mssf = null;
try {
mssf = new MailSSLSocketFactory();
mssf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.socketFactory", mssf);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.ssl.socketFactory.port", "587");
properties.setProperty("mail.imap.ssl.socketFactory.class", "DummySSLSocketFactory");
properties.setProperty("mail.imap.ssl.socketFactory.fallback", "false");
// properties.setProperty("mail.smtp.port", "587");
// properties.setProperty("mail.smtp.socketFactory.port", "587");
// properties.setProperty("mail.imap.ssl.enable", "true");
// properties.setProperty("mail.imap.ssl.socketFactory.class", "DummySSLSocketFactory");
// properties.setProperty("mail.imap.ssl.socketFactory.fallback", "false");
} catch (final GeneralSecurityException e) {
e.printStackTrace();
}

而你需要做的便是把

properties.put(“mail.smtp.ssl.enable”, “true”);

放在

properties.put(“mail.smtp.port”, “587”);
properties.put(“mail.smtp.ssl.socketFactory.port”, “587”);

的下面。