【问题标题】:How to fix Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM如何修复 引起:com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP;在 MAIL FROM 期间,客户端未通过身份验证发送匿名邮件
【发布时间】:2017-08-24 02:32:25
【问题描述】:

我正在尝试使用我的应用程序中的 Java Mail API 发送电子邮件。我使用的代码如下:

   private void sendMail(Properties props, String fromEmail, String password, String toEmail, String messageSubject, String messageBody)
    {
        final String username = fromEmail;
        final String pasword = password;
        //props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, new javax.mail.Authenticator() 
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(username,pasword);
            }
        });
    try 
    {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
        message.setSubject(messageSubject);
        message.setText(messageBody);
        Transport.send(message);
    } 
    catch (MessagingException e)
    {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    }

我有以下属性:

{mail.smtp.port=587, mail.smtp.socketFactory.port=465, mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory, mail.smtp.auth=true, mail.smtp.host=smtp.office365.com}

我收到以下异常:

Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

相同的代码适用于smtp.gmail.com,我能够从基于 gmail 的电子邮件发送邮件。这个问题有解决办法吗?

【问题讨论】:

  • 尝试谷歌错误信息,你会找​​到答案。

标签: java email jakarta-mail office365


【解决方案1】:

摆脱 Authenticator,只需调用 Transport.send method that takes a username and password,如 JavaMail FAQ 中所述。

【讨论】:

    【解决方案2】:

    请在您的代码中设置以下内容

    message.setHeader("X-Mailer","your application name");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 2019-09-15
      • 2019-06-05
      • 2019-10-21
      • 1970-01-01
      相关资源
      最近更新 更多