我想用JavaMailAPIJavaMail的異常javax.mail.AuthenticationFailedException特定應用程序534-5.7.9密碼所需
我已經做了一些代碼來發送郵件,但它不工作引發異常: -
留言發送Failedjavax.mail.AuthenticationFailedException:534-5.7.9需要應用程序特定的密碼。
package com.appreciationcard.service;
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 com.appreciationcard.dao.DAOFactory;
import com.appreciationcard.forms.ComposeForm;
public class MailServiceImpl implements MailService {
public boolean mailsent(ComposeForm composeForm) {
String to = composeForm.getTo();
String from = composeForm.getFrom();
String cc = composeForm.getCc();
String bcc = composeForm.getBcc();
String subject = composeForm.getSubject();
String messages = composeForm.getMessage();
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", "587");
props.put("mail.transport.protocol", "smtp");
props.put("mail.debug", "true");
System.out.println("Properties" + props);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"[email protected]", "xxxx");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(subject);
message.setText(messages);
Transport.send(message);
} catch (MessagingException mex) {
System.out.println("Message Sending Failed" + mex);
mex.printStackTrace();
}
}
}
我服務器控制檯上獲得異常
消息發送Failedjavax.mail.AuthenticationFailedException:需要534-5.7.9應用程序專用密碼。
瞭解更多534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 o5sm11464195pdr.50 - gsmtp
將在任何一個好心幫我解決這個問題。
要清楚,您是否按照鏈接中的說明生成了應用專用密碼?即使使用應用程序特定的密碼,您是否也會收到錯誤? – ZoogieZork 2014-10-27 18:21:04
您是否閱讀過該頁面? – SLaks 2014-10-27 18:21:26
在某些系統上,鏈接到上面列出的有用URL會被清除。這就是我所在的地方 - 所以這在這方面仍然是一個有用的問題。 – davidjmcclelland 2015-01-31 20:15:28