【问题标题】:javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROMjavax.mail.MessagingException:530 5.7.57 SMTP;在 MAIL FROM 期间,客户端未通过身份验证发送匿名邮件
【发布时间】:2020-07-17 22:43:23
【问题描述】:

我有 java 程序(从 google 复制)使用 office365 SMTP 发送电子邮件,它作为一个标准 java 程序运行良好,但是当我将此 java 程序部署为 web 应用程序的web-inf/lib 中的 jar 文件并调用该方法时从 JSP 抛出以下错误:

javax.mail.SendFailedException: Sending failed;   nested exception is:
javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not
authenticated to send anonymous mail during MAIL FROM

有人可以分享他们对这个问题的看法吗? java代码:

import java.util.Properties;

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;

import org.apache.log4j.Logger;

public class SendEmailUsingSMTP {
    
    
   public static boolean sendEmail(String toAddress, String fromAddress, String userName, String userPassword,String smtpHost, String portNumber, String emailSubject,String emailBody) {
      // Recipient's email ID needs to be mentioned.
       
       Logger log = Logger.getLogger(SendEmailUsingSMTP.class);
       log.info("toAddress : "+toAddress);
       log.info("fromAddress : "+fromAddress);
       log.info("userName : "+userName);
       log.info("userPassword : "+userPassword);
       log.info("smtpHost : "+smtpHost);
       log.info("portNumber : "+portNumber);
       log.info("emailSubject : "+emailSubject);
       log.info("emailBody : "+emailBody);
       
       String to = toAddress;

      // Sender's email ID needs to be mentioned
      String from = fromAddress;//change accordingly
      final String username = userName;//change accordingly
      final String password = userPassword;//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = smtpHost;

      Properties props = new Properties();
      
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.socketFactory.fallback", "false");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.socketFactory.port", portNumber);
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", portNumber);

      // Get the Session object.
      SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password);
      props.put("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
      Session session = Session.getInstance(props, authenticator);


      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
         InternetAddress.parse(to));

         // Set Subject: header field
         message.setSubject(emailSubject);

         // Now set the actual message
         message.setText(emailBody);

         // Send message
         Transport.send(message);

         System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
            throw new RuntimeException(e);
      }
    return true;
   }
}

【问题讨论】:

  • 我使用的 smtpHost,portNumber String smtpHost = "smtp.office365.com";字符串端口号 = "587";
  • 我面临着完全相同的用途...

标签: java email


【解决方案1】:

您可以尝试以下配置,因为它适用于我:

"mail.smtp.starttls.enable":"true"

另外,我使用了主机:

host = "Outlook.office365.com"

希望这对您或其他人有所帮助。

【讨论】:

  • 我试过这个。但同样的例外又来了 530 5.7.57 SMTP;在 MAIL FROM 期间,客户端未通过身份验证发送匿名邮件
  • @apm 这是因为发件人与你登录的用户名不同
【解决方案2】:

smtp.starttls.enable 设置为true 并将主机设置为smtp.office365.com 为我解决了类似的问题。

我仅使用以下 3 个属性在您的代码中对其进行了测试:

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

主持人:smtp.office365.com,我可以从我的帐户发送电子邮件。

整个函数:

  public static boolean sendEmail(String toAddress, String fromAddress, String userName, String userPassword,String smtpHost, String emailSubject,String emailBody) {
      // Recipient's email ID needs to be mentioned.


      String to = toAddress;

      // Sender's email ID needs to be mentioned
      String from = fromAddress;//change accordingly
      final String username = userName;//change accordingly
      final String password = userPassword;//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = smtpHost;

      Properties props = new Properties();

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

      // Get the Session object.
      SimpleMailAuthenticator authenticator = new SimpleMailAuthenticator(username, password);
      Session session = Session.getInstance(props, authenticator);


      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
         InternetAddress.parse(to));

         // Set Subject: header field
         message.setSubject(emailSubject);

         // Now set the actual message
         message.setText(emailBody);

         // Send message
         Transport.send(message);

         System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
            throw new RuntimeException(e);
      }
    return true;
   }

和验证者

class SimpleMailAuthenticator extends Authenticator {


    String userName;
    String password;
    PasswordAuthentication authentication;

    public SimpleMailAuthenticator(String userName,String password) {
        super();
        this.userName = userName;
        this.password = password;           
        authentication = new PasswordAuthentication(userName, password);
    }

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return authentication;
    }


}  

【讨论】:

    【解决方案3】:

    所以如果有人遇到这样的问题:---

    javax.mail.MessagingException: 530 5.7.57 SMTP;客户不是 在 MAIL FROM 期间通过身份验证发送匿名邮件

    在 Linux 操作系统上使用时,只需执行以下步骤:- 使用 sudo 编辑您的默认 Jenkins 文件,因为它是只读文件。

    sudo vim /etc/default/jenkins 
    

    并添加这两行:--

    JAVA_ARGS="-Xmx2048m -XX:MaxPermSize=512m -Djava.awt.headless=true"
    JAVA_ARGS="-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true"
    

    编辑/追加文件后,重启 Jenkins。

    命令:- sudo /etc/init.d/Jenkins restart

    现在测试您的配置,希望您会收到电子邮件已成功发送。

    【讨论】:

      【解决方案4】:

      请设置以下 X-Mailer as 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
        相关资源
        最近更新 更多