事实证明,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", "blahblah@gmail.com");
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("blahblah2@gmail.com"));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
530 5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257 g2sm368231pbh.15... 我查看了它包含的网站,但没有找到任何有用的信息。 - Brian
mail.smtp.auth
属性设置为true
。 - Rob Harrop