【问题标题】:Client was not authenticated to send anonymous mail during MAIL FROM在 MAIL FROM 期间,客户端未通过身份验证发送匿名邮件
【发布时间】:2018-08-02 15:00:40
【问题描述】:

我正在尝试在调用此方法的 java 应用程序中发送电子邮件:

public static void sendEmail() {

    // Create object of Property file
    Properties props = new Properties();

    // this will set host of server- you can change based on your requirement 
    props.put("mail.smtp.host", "smtp.office365.com");

    // set the port of socket factory 
    //props.put("mail.smtp.socketFactory.port", "587");

    // set socket factory
    //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

    // set the authentication to true
    props.put("mail.smtp.auth", "true");

    // set the port of SMTP server
    props.put("mail.smtp.port", "587");

    // This will handle the complete authentication
    Session session = Session.getDefaultInstance(props,

            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication("xx@mail.com", "xx");


                }

            });

    try {

        // Create object of MimeMessage class
        Message message = new MimeMessage(session);

        // Set the from address
        message.setFrom(new InternetAddress("xx@mail.com"));

        // Set the recipient address
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("yy@mail.com"));

                    // Add the subject link
        message.setSubject("Testing Subject");

        // Create object to add multimedia type content
        BodyPart messageBodyPart1 = new MimeBodyPart();

        // Set the body of email
        messageBodyPart1.setText("This is message body");

        // Create object of MimeMultipart class
        Multipart multipart = new MimeMultipart();

        // add body part 2
        multipart.addBodyPart(messageBodyPart1);

        // set the content
        message.setContent(multipart);

        // finally send the email
        Transport.send(message);

        System.out.println("=====Email Sent=====");

    } catch (MessagingException e) {

        throw new RuntimeException(e);

    }

}

但由于某种原因,当调试命中 Transport.send() 时,我得到了这个异常:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [DB6PR0802CA0037.eurprd08.prod.outlook.com]

即使我使用了 Authenticator(),为什么会发生这种情况?

【问题讨论】:

    标签: java smtp jakarta-mail


    【解决方案1】:

    如果您通过端口 587 进行连接,您最初会启动一个普通连接,您必须通过显式发送 STARTTLS 命令来启动 TLS。您必须告诉 JavaMail 这样做,否则它将尝试不安全地进行。除非建立了 TLS 连接,否则 SMTP 服务器不会发送任何身份验证机制信息,因此 JavaMail 假设不需要身份验证并尝试在没有身份验证的情况下发送邮件。

    将以下条目添加到属性中:

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

    JavaMail 应该在尝试进行身份验证之前尝试切换到 TLS。

    如果失败,则需要通过设置属性来启用调试

    props.put("mail.debug", "true");
    

    并在此处发布输出。

    【讨论】:

    • 另外,修复这些 common JavaMail mistakes 并摆脱 Authenticator。
    • 谢谢@Lothar,这个 props.put("mail.smtp.starttls.enable", "true");解决了问题!
    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2019-06-05
    • 2017-10-17
    相关资源
    最近更新 更多