事实证明,JavaMail 比我想象的要令人沮丧一些。我在网上查看了几个有关如何通过 Gmail 服务器(但不通过 SSL)发送简单 SMTP 电子邮件的示例。在尝试了几个不同的代码示例之后,当我调用时,我不断得出相同的示例异常transport.connect()
。我不断收到这个堆栈跟踪:
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. l10sm302158wfk.21
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
at SendEmail.main(SendEmail.java:47)
有人可以告诉我应该添加什么或做什么来解决这个问题吗?
这是我的代码:
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", "smtp.gmail.com");
props.put("mail.user", "[email protected] /cdn-cgi/l/email-protection");
props.put("mail.password", "blah");
props.put("mail.port", "587");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("This is a test");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected] /cdn-cgi/l/email-protection"));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
您需要启用STARTTLS
。在您的配置中再添加一项属性:
props.put("mail.smtp.starttls.enable", "true");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)