javax.mail.MessagingException: 530 5.7.57 SMTP;在MAIL FROM期间,客户端未经身份验证以发送匿名邮件

6

我有一个使用office365 SMTP发送电子邮件的Java程序(从Google复制而来),作为独立的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;
   }
}

我正在使用的smtp主机和端口号是 String smtpHost = "smtp.office365.com"; String portNumber = "587"; - Jay
我遇到了完全相同的问题... - Sunil Khokhar
4个回答

9
您可以尝试以下配置,因为它对我有效:

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

另外,我使用了主机:

host = "Outlook.office365.com"

希望这能帮助您或其他人。

1
我尝试过了。但是同样的异常再次出现。 530 5.7.57 SMTP; 客户端在 MAIL FROM 期间未经身份验证,无法发送匿名邮件。 - apm
1
@apm 这是因为发送者与您登录的用户名不同。 - Sanjay Bharathi

6

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


}  

1

如果有人遇到以下问题:

javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during 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

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


0
请将以下 X-Mailer 设置为 message.setHeader("X-Mailer", "您的应用程序名称");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接