![](/img/trans.png)
[英]exception occured while sending ,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; 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; 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;
}
}
您可以嘗試使用以下配置,因為它對我有用:
"mail.smtp.starttls.enable":"true"
另外,我使用了主機:
主機 = "Outlook.office365.com"
希望這可以幫助您或其他人。
設置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;
}
}
因此,如果有人遇到以下問題:---
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
現在測試您的配置,希望您能成功發送電子郵件。
請將下面的 X-Mailer 設置為 message.setHeader("X-Mailer", "Your application name");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.