使用apache commons电子邮件库获取错误必须首先发出starttls命令。请帮忙

时间:2010-01-25 12:54:54

标签: java apache-commons-email

Email email = new SimpleEmail();
String authuser = "......@gmail.com";
String authpwd = "*******";
// Very Important, Don't use email.setAuthentication()
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true); // true if you want to debug
email.setHostName("smtp.gmail.com");

email.getMailSession().getProperties().put("mail.smtp.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtp.port", "587");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "587");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo(".............@gmail.com", "ToName");
email.setTLS(true);
email.send();

1 个答案:

答案 0 :(得分:0)

摆脱所有email.getMailSession().getProperties() - 它们不是必需的,可能会污染设置。

然后删除设置端口的行。 (email.setSmtpPort(587);