先前使用的代码:
String host = "xxx";
String from = "xxx";
String name = "xx";
String sender = "XXX";
String pwd = XXX";
String port = "465";
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", "smtp");
//服务器
prop.setProperty("mail.smtp.host", host);
//端口
prop.setProperty("mail.smtp.port", port + "");
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", "true");
//使用SSL,企业邮箱必需!
//开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(prop, new MyAuthenricator(name, pwd));
session.setDebug(true);
// 建立邮件消息
MimeMessage mailMessage = new MimeMessage(session);
Transport.send(mailMessage);
然而却出错了!
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP:AUTH LOGIN failed
2017-05-04 16:41:35.589 ERROR com.rbao.east.task.TaskJobImpl - 182 -
sendEmail error mailMessage=javax.mail.internet.MimeMessage@39f840f5
javax.mail.AuthenticationFailedException: 535 Error: authentication failed, system busy
登陆授权失败!
弄了半天,将上述代码开启安全协议改了下:
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.socketFactory.port", port);
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
最终发送成功了!!!