【问题标题】:Must issue a STARTTLS command first必须先发出 STARTTLS 命令
【发布时间】:2012-05-09 04:09:35
【问题描述】:

我正在使用我的Gmail 帐户运行这个简单的示例,但它不起作用并出现以下错误:

      send failed, exception: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. nv2sm4478384pbb.6      

这是我的代码

   public class Email
{
   public static void main(String [] args)
   {

       Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.googlemail.com");
        props.put("mail.from", "myemail@gmail.com");
          Session session = Session.getInstance(props, null);

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom();
            msg.setRecipients(Message.RecipientType.TO,
                              "myemail@hotmail.com");
            msg.setSubject("JavaMail hello world example");
            msg.setSentDate(new Date());
            msg.setText("Hello, world!\n");
            Transport.send(msg);
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);
        }
   }
}

【问题讨论】:

标签: java


【解决方案1】:

您可能正试图使用​​ Gmail 的服务器在端口 25 上通过未经身份验证的连接将邮件传递给第三方。 Gmail 不允许您这样做,因为这样任何人 都可以使用 Gmail 的服务器向其他任何人发送邮件。这称为开放中继,是早期垃圾邮件的常见促成因素。 Internet 上不再接受开放式中继。

您需要让您的 SMTP 客户端使用经过身份验证的连接连接到 Gmail,可能是在 port 587 上。

【讨论】:

  • 当时有没有可以接受未经身份验证的连接的电子邮件提供商?
  • 现在要从任何 Java 客户端访问邮件,您可能需要降低安全性。阅读support.google.com/accounts/answer/6010255
【解决方案2】:

smtp 端口和 socketFactory 必须更改

    String to = "reciveremail@xxxx.xxx";
    String subject = "subject"
    String msg ="email text...."
    final String from ="senderemail@gmail.com"
    final  String password ="senderPassword"


    Properties props = new Properties();  
    props.setProperty("mail.transport.protocol", "smtp");     
    props.setProperty("mail.host", "smtp.gmail.com");  
    props.put("mail.smtp.auth", "true");  
    props.put("mail.smtp.port", "465");  
    props.put("mail.debug", "true");  
    props.put("mail.smtp.socketFactory.port", "465");  
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  
    props.put("mail.smtp.socketFactory.fallback", "false");  
    Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {  
       return new PasswordAuthentication(from,password);  
   }  
   });  

   //session.setDebug(true);  
   Transport transport = session.getTransport();  
   InternetAddress addressFrom = new InternetAddress(from);  

   MimeMessage message = new MimeMessage(session);  
   message.setSender(addressFrom);  
   message.setSubject(subject);  
   message.setContent(msg, "text/plain");  
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  

   transport.connect();  
   Transport.send(message);  
   transport.close();
   }  

希望它对你有用..

【讨论】:

  • 这条消息.setSender(addressFrom);应该是 message.setFrom(addressFrom);
  • 我们可以使用这个发送邮件到webamails吗
  • Transport.send(message);应该是 transport.send(message);
【解决方案3】:

添加

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

解决了我的问题;)

我的问题是

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 必须先发出 STARTTLS 命令。 u186sm7971862pfu.82 - gsmtp

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.example.sendmail.SendEmailExample2.main(SendEmailExample2.java:53)

【讨论】:

  • 这也是我所缺少的。
【解决方案4】:

我在构建电子邮件通知应用程序时也遇到了同样的问题。你只需要添加一行。下面一个拯救了我的一天。

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

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 必须先发出 STARTTLS 命令。 h13-v6sm10627790pgp.13 - gsmtp

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.smruti.email.EmailProject.EmailSend.main(EmailSend.java:99)

希望对你有所帮助。

【讨论】:

    【解决方案5】:

    Google 现在有一项功能,声明它不会允许不安全的设备发送电子邮件。当我运行我的程序时,它在第一篇文章中出现了错误。我必须进入我的帐户并允许不安全的应用程序发送电子邮件,我通过点击我的帐户、进入安全选项卡并允许不安全的应用程序使用我的 gmail 来做到这一点。

    【讨论】:

      【解决方案6】:
          String username = "mail@google.com";
          String password = "some-password";
          String recipient = "myemail@hotmail.com");
      
          Properties props = new Properties();
      
          props.put("mail.smtp.host", "smtp.gmail.com");
          props.put("mail.from", "myemail@gmail.com");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.port", "587");
          props.setProperty("mail.debug", "true");
      
          Session session = Session.getInstance(props, null);
          MimeMessage msg = new MimeMessage(session);
      
          msg.setRecipients(Message.RecipientType.TO, recipient);
          msg.setSubject("JavaMail hello world example");
          msg.setSentDate(new Date());
          msg.setText("Hello, world!\n");
      
          Transport transport = session.getTransport("smtp");
      
          transport.connect(username, password);
          transport.sendMessage(msg, msg.getAllRecipients());
          transport.close();
      

      【讨论】:

        【解决方案7】:

        试试这个代码:

        Properties props = new Properties();
                                props.put("mail.smtp.host", "smtp.gmail.com");
                                props.put("mail.smtp.socketFactory.port", "465");
                                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                                props.put("mail.smtp.auth", "true");
                                props.put("mail.smtp.prot", "465");
        
                                Session session = Session.getDefaultInstance(props,
                                        new javax.mail.Authenticator() {
                                    protected PasswordAuthentication getPasswordAuthentication() {
        
                                        return new PasswordAuthentication("PUT THE MAIL SENDER HERE !", "PUT THE PASSWORD OF THE MAIL SENDER HERE !");
                                    }
                                }
                                );
                                try {
                                    Message message = new MimeMessage(session);
                                    message.setFrom(new InternetAddress("PUT THE MAIL SENDER HERE !"));
                                    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("PUT THE MAIL RECEIVER HERE !"));
                                    message.setSubject("MAIL SUBJECT !");
                                    message.setText("MAIL BODY !");
                                    Transport.send(message);
        
                                } catch (Exception e) {
                                    JOptionPane.showMessageDialog(null, e);
                                }
        

        您必须降低邮件发件人的安全性。如果问题仍然存在,我认为这可能是由防病毒软件引起的,请尝试禁用它..

        【讨论】:

          猜你喜欢
          • 2015-04-24
          • 2017-01-13
          • 2015-12-16
          • 2017-01-13
          • 1970-01-01
          • 1970-01-01
          • 2015-12-28
          • 2015-02-04
          • 1970-01-01
          相关资源
          最近更新 更多