【发布时间】:2017-06-25 06:25:09
【问题描述】:
我正在使用 java 邮件在按下按钮时发送自动邮件。
我在 android studio 上工作,我收到错误 javax.mail.AuthenticationFailedException,我找不到原因。
- 我尝试将端口更改为 25 - 587 和 465
- ID 正确
- 在模拟器和真机上试过
- 我的 gmail 帐户对低安全性应用程序开放
这里是代码:
Properties props = new Properties();
props.put("mail.smtp.host" , "smtp.gmail.com");
props.put("mail.stmp.user" , "horiond@gmail.com");
//TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "xxxxx");
//SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance( props , null);
String to = "horiond@gmail.com";
String from = "horiond@gmail.com";
String subject = "Testing...";
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Working fine..!");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com" , 465 , "horiov@gmail.com", "xxxx");
transport.send(msg);
System.out.println("fine!!");
}
catch(Exception exc) {
System.out.println(exc);
}
【问题讨论】:
-
修复这些common JavaMail mistakes,使用新代码更新您的帖子,如果仍然无法使用,请使用JavaMail debug output 更新您的帖子。
标签: java android email jakarta-mail