【问题标题】:Must issue a STARTTLS command first. Sending email with Java and Google Apps必须先发出 STARTTLS 命令。使用 Java 和 Google Apps 发送电子邮件
【发布时间】:2010-09-28 00:23:51
【问题描述】:

我正在尝试使用 Bill the Lizard's code 通过 Google Apps 发送电子邮件。我收到此错误:

Exception in thread "main" javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. f3sm9277120nfh.74

    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:81)
    at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:44)

Bill 的代码包含下一行,似乎与错误有关:

   props.put("mail.smtp.starttls.enable","true");

但是,它没有帮助。

这些是我的导入语句:

import java.util.Properties; 
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

有人知道这个错误吗?

【问题讨论】:

    标签: java email google-apps


    【解决方案1】:

    我发现了问题。以前我使用 j2ee.jar 导入 javax.mail。

    我从类路径中删除了 j2ee.jar 并下载了 JavaMail 1.4.1 并将两个 jars smtp.jar 和 mailapi.jar 放入我的类路径中。我现在使用 smtps 而不是 smtp

    Transport transport = session.getTransport("smtps");            
    

    现在 Bill the Lizard 的代码可以工作了。

    【讨论】:

      【解决方案2】:

      设置以下标签。它会起作用的。

      props.put("mail.smtp.user","username"); 
      props.put("mail.smtp.host", "smtp.gmail.com"); 
      props.put("mail.smtp.port", "25"); 
      props.put("mail.debug", "true"); 
      props.put("mail.smtp.auth", "true"); 
      props.put("mail.smtp.starttls.enable","true"); 
      props.put("mail.smtp.EnableSSL.enable","true");
      
      props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
      props.setProperty("mail.smtp.socketFactory.fallback", "false");   
      props.setProperty("mail.smtp.port", "465");   
      props.setProperty("mail.smtp.socketFactory.port", "465"); 
      

      【讨论】:

      • 似乎是明智之举,但为什么要乱用 25 端口呢?它通常总是被 ISP 屏蔽,实际上在几行后重置回 465...
      【解决方案3】:

      是java邮件API的版本。 我遇到了这个问题,我刚刚将 java 邮件 API 更新到 1.4.3 对我来说很好用!

      谢谢!

      【讨论】:

      • 我在使用 1.4.2 时遇到了同样的问题,更新到 1.4.3 不是解决方案。不确定是版本问题。
      【解决方案4】:

      我认为这与使用 SMTPS 而不是 SMTP 进行邮件传输有关。这是一个不同的版本,仿照JavaMail FAQ on accessing Gmail。请注意,为了清楚起见,我省略了所有更精细的异常处理。

      private static void send(
              final String username,
              final String password,
              final String recipients,
              final String subject,
              final String body)
              throws Exception
      {
          final Session session = Session.getInstance(System.getProperties(), null);
          final Message msg = new MimeMessage(session);
          final String senderEmail = username.contains("@") ? username : (username + "@gmail.com");
          msg.setFrom(new InternetAddress(senderEmail));
      
          final Address[] recipientAddresses = InternetAddress.parse(recipients);
          msg.setRecipients(Message.RecipientType.TO, recipientAddresses);
      
          msg.setSentDate(new Date());
          msg.setSubject(subject);
          msg.setText(body);
      
          final Transport transport = session.getTransport("smtps");
          transport.connect(GMAIL_SMTP_HOST, GMAIL_SMTP_PORT, username, password);
          transport.sendMessage(msg, recipientAddresses);
          transport.close();
      }
      

      【讨论】:

      【解决方案5】:

      这让我发疯了,所以只想添加对我有用的东西。我必须更新我的 JavaMail (1.4.5) 版本才能正常工作 - 不确定之前使用的是什么版本。

      一旦我更新到 JavaMail 的新版本,以下代码对我有用(可以取消注释调试行以获取更多调试信息 - 端口是 587,主机是 smtp.gmail.com):

      public void sendMailWithAuth(String host, String user, String password, 
          String port, List<String> toList, String htmlBody, 
              String subject) throws Exception {
      
          Properties props = System.getProperties();
      
          props.put("mail.smtp.user",user); 
          props.put("mail.smtp.password", password);
          props.put("mail.smtp.host", host); 
          props.put("mail.smtp.port", port); 
          //props.put("mail.debug", "true"); 
          props.put("mail.smtp.auth", "true"); 
          props.put("mail.smtp.starttls.enable","true"); 
          props.put("mail.smtp.EnableSSL.enable","true");
      
          Session session = Session.getInstance(props, null);
          //session.setDebug(true);
      
          MimeMessage message = new MimeMessage(session);
          message.setFrom(new InternetAddress(user));
      
          // To get the array of addresses
          for (String to: toList) {
              message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          }
      
          message.setSubject(subject);
          message.setContent(htmlBody, "text/html");
      
          Transport transport = session.getTransport("smtp");
          try {
              transport.connect(host, user, password);
              transport.sendMessage(message, message.getAllRecipients());
          } finally {
              transport.close();
          }
      }
      

      【讨论】:

        【解决方案6】:

        试试这个:

        开启:允许您的 gmail 帐户使用安全性较低的应用

        https://myaccount.google.com/lesssecureapps

        【讨论】:

          猜你喜欢
          • 2010-12-11
          • 2014-06-25
          • 2017-01-13
          • 1970-01-01
          • 2023-03-07
          • 1970-01-01
          • 2015-04-24
          • 1970-01-01
          • 2015-12-16
          相关资源
          最近更新 更多