使用 apache commons 电子邮件库出现错误必须首先发出 starttls 命令。请帮忙
Email email = new SimpleEmail();
String authuser = "[email protected]";
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("[email protected]", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("[email protected]", "ToName");
email.setTLS(true);
email.send();
Email email = new SimpleEmail();
String authuser = "[email protected]";
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("[email protected]", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("[email protected]", "ToName");
email.setTLS(true);
email.send();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
摆脱所有
email.getMailSession().getProperties()
- 它们不是必需的,并且可能会污染设置。然后删除设置端口的行。 (
email.setSmtpPort(587);
)Get rid of all
email.getMailSession().getProperties()
- they are not required and may be polluting the setup.Then remove the line that sets the port. (
email.setSmtpPort(587);
)