0
該程序嘗試發送電子郵件,但會引發運行時異常:AuthenticationFailedException我已經提到了stackoverflow quetion並回答了我已經實現的同樣的事情,但仍然得到了像這樣的異常任何人都可以解決這個問題。
異常如何解決javax.mail.AuthenticationFailedException問題
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:267)
at javax.mail.Service.connect(Service.java:137)
at javax.mail.Service.connect(Service.java:86)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.treamis.transport.vehicle.javaMail.send(javaMail.java:81)
at com.treamis.transport.vehicle.MysqlBackup.backupDataWithDatabase(Mysq
lBackup.java:97)
at com.treamis.transport.vehicle.MysqlBackup.run(MysqlBackup.java:118)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Sms sent xl sheet is generated is generated
Java郵件代碼
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class javaMail {
private String SMTP_PORT = "465";
private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private String SMTP_HOST_NAME = "smtp.gmail.com";
private Properties smtpProperties;
public javaMail() {
initProperties();
}
private void initProperties() {
smtpProperties = new Properties();
smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME);
smtpProperties.put("mail.smtp.auth", "true");
smtpProperties.put("mail.debug", "true");
smtpProperties.put("mail.smtp.port", SMTP_PORT);
smtpProperties.put("mail.smtp.socketFactory.port", SMTP_PORT);
smtpProperties.put("mail.smtp.socketFactory.class", SSL_FACTORY);
smtpProperties.put("mail.smtp.socketFactory.fallback", "false");
}
public String send(String[] to, final String from, final String pwd, String subject, String body) {
javaMail tjm = new javaMail();
try {
Properties props = tjm.getSmtpProperties();
// -- Attaching to default Session, or we could start a new one --
// Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(from, pwd);
// }
// });
Session session = Session.getInstance(props, new GMailAuthenticator(from, pwd));
// Session session = Session.getInstance(props, new javax.mail.Authenticator() {
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(userName, password);
// }
//});
Message msg = new MimeMessage(session);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Test mail one");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(body);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(body);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent OK.");
return "success";
} catch (Exception ex) {
ex.printStackTrace();
ex.getMessage();
}
return null;
}
public Properties getSmtpProperties() {
return smtpProperties;
}
public void setSmtpProperties(Properties smtpProperties) {
this.smtpProperties = smtpProperties;
}
}
GMailAuthenticator代碼 */
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
class GMailAuthenticator extends Authenticator {
String user;
String pw;
public GMailAuthenticator (String username, String password)
{
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
}
嗨可以任何一個PLZ解決我的probelm – user3214269
請檢查您的地址和密碼..並確保您的地址沒有一個額外的身份驗證..如移動一次性密碼 – Naren
我不理解你的評論 – user3214269