java 必须先发出STARTTLS命令

hjzp0vay  于 2023-01-24  发布在  Java
关注(0)|答案(8)|浏览(106)

我正在使用我的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);
        }
   }
}
7vux5j2d

7vux5j2d1#

你可能试图使用Gmail服务器的25端口通过未经认证的连接向第三方发送邮件。Gmail不允许你这样做,因为这样一来,任何人都可以使用Gmail的服务器向其他人发送邮件。这被称为“开放中继”,在早期是垃圾邮件的常见推动者。开放中继在互联网上不再被接受。
您需要请求SMTP客户端使用经过身份验证的连接(可能在587端口)连接到Gmail。

nmpmafwu

nmpmafwu2#

必须更改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();
   }

希望它对你有用。

oipij1gg

oipij1gg3#

添加

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

解决了我的问题)

我的问题是

邮件发送失败异常: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)
oyjwcjzk

oyjwcjzk4#

我也面临着同样的问题,而我正在建立电子邮件通知应用程序。你只需要添加一行。下面一个救了我的一天。

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

邮件发送失败异常: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)

希望这对你有帮助。

0aydgbwb

0aydgbwb5#

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

nnsrf1az

nnsrf1az6#

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();
smdnsysy

smdnsysy7#

请尝试以下代码:

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);
                        }

你必须降低邮件发送者的安全性。如果问题仍然存在,我认为可能是防病毒软件引起的,试着禁用它。

agyaoht7

agyaoht78#

当我设置props.put(“mail.smtp.starttls.enable”,myMailConfig. starttls)时,我也收到了这个错误;这里myMailConfig.StartTLS是一个字符串并且有一个真值,所以这个字符串是第一个原因。所以试着像props.put(“mail.smtp.starttls.enable”,true)一样设置它(布尔值)为真;或者如果是动态的,那么确保你的变量是布尔值,或者如果是字符串,那么db值应该设置为真。

相关问题