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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30931213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:50:47  来源:igfitidea点击:

javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

java

提问by Jay

I have java program(Copied from google) to send emails using office365 SMTP, it is working fine as a stand java program but when I deploy this java program as jar file in web-inf/lib of a web application and calling the method from jsp it is throwing the below error: 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 程序(从谷歌复制)使用 office365 SMTP 发送电子邮件,它作为一个独立的 java 程序工作正常,但是当我将此 java 程序部署为 web 应用程序的 web-inf/lib 中的 jar 文件并从jsp 抛出以下错误: javax.mail.SendFailedException: Sending failed; 嵌套异常是:javax.mail.MessagingException:530 5.7.57 SMTP;在 MAIL FROM 期间,客户端未通过身份验证发送匿名邮件

Can someone please share their views on this issue.

有人可以分享他们对这个问题的看法。

The java code :

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

回答by Alex Vargas

You can try with the following configuration, as it works for me:

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

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

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

Also, I used host:

另外,我使用了主机:

host = "Outlook.office365.com"

主机 = "Outlook.office365.com"

Hope this helps you or someone else.

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

回答by Zielu

Setting the smtp.starttls.enableto trueand the host to smtp.office365.comsolved the similar problem for me.

设置smtp.starttls.enabletotrue和 hostsmtp.office365.com为我解决了类似的问题。

I tested it with your code using only these 3 properties:

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

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

the host: smtp.office365.comand I could sent the emails from my account.

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

The whole function:

整个功能:

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

and the authenticator

和验证器

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


}  

回答by Nethaji Narasimalu

Please set the below X-Mailer as message.setHeader("X-Mailer", "Your application name");

请将下面的 X-Mailer 设置为 message.setHeader("X-Mailer", "Your application name");

回答by uttam

So if anyone having issue like :---

因此,如果有人遇到以下问题:---

"javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM"

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

while using on Linux OS then,just perform steps given below:- Edit your default jenkins file using sudo because it's read only file.

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

sudo vim /etc/default/jenkins

须藤vim /etc/default/jenkins

and add these two lines:--

并添加这两行:--

JAVA_ARGS="-Xmx2048m -XX:MaxPermSize=512m -Djava.awt.headless=true"

JAVA_ARGS="-Xmx2048m -XX:MaxPermSize=512m -Djava.awt.headless=true"

JAVA_ARGS="-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true"

JAVA_ARGS="-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true"

After editing/appending the file , restart Jenkins. command:- sudo /etc/init.d/Jenkins restart

编辑/附加文件后,重新启动 Jenkins。命令:- sudo /etc/init.d/Jenkins restart

Now test your configuration , Hope you will get Email was successfully sent

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