JavaMail API 无法使用 Gmail
JavaMail API has trouble to work with the Gmail
我正在编写 http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/javamail/javamail.html 上的教程,其中展示了如何使用 JavaMail API 发送电子邮件。
所以我写了下面的代码片段
public class EmailSessionBean {
private int port = 465;
private String host = "smtp.gmail.com";
private String from = "xxx@gmail.com";
private boolean auth = true;
private String username = "xxx@gmail.com";
private String password = "mypassword";
private Protocol protocol = Protocol.SMTPS;
private boolean debug = true;
public void sendEmail(String to, String subject, String body) {
// Create a Properties object to contain settings for
// the SMTP protocol provider.
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
switch (protocol) {
case SMTPS:
props.put("mail.smtp.ssl.enable", true);
break;
case TLS:
props.put("mail.smtp.starttls.enable", true);
break;
}
// If SMTP authentication is required you must set the mail.smtp.auth
// property and construct a Authenticator instance that returns
// a PasswordAuthentication instance with your username and password.
Authenticator authenticator = null;
if (auth) {
props.put("mail.smtp.auth", true);
authenticator = new Authenticator() {
private PasswordAuthentication pa
= new PasswordAuthentication(username, password);
@Override
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
//Create a Session instance using the Properties object
// and the Authenticator object.
Session session = Session.getInstance(props, authenticator);
session.setDebug(debug);
// Construct a MimeMessage instance, populate the message headers
// and content and then send the message
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(body);
Transport.send(message);
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
}
当我尝试使用 WEB APP 发送电子邮件时,Gmail 阻止我这样做并给我发邮件 "Sign-in attempt prevented"。
P.S。我还在我的 Gmail 帐户中禁用了 "Access for less secure apps"。但在下一次这样做的尝试中,Gmail 暂停了我的帐户。
任何有用的comment/solution将不胜感激。
确保您已检查列出的所有内容 here especially the DisplayUnlockCaptcha
我正在编写 http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/javamail/javamail.html 上的教程,其中展示了如何使用 JavaMail API 发送电子邮件。
所以我写了下面的代码片段
public class EmailSessionBean {
private int port = 465;
private String host = "smtp.gmail.com";
private String from = "xxx@gmail.com";
private boolean auth = true;
private String username = "xxx@gmail.com";
private String password = "mypassword";
private Protocol protocol = Protocol.SMTPS;
private boolean debug = true;
public void sendEmail(String to, String subject, String body) {
// Create a Properties object to contain settings for
// the SMTP protocol provider.
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
switch (protocol) {
case SMTPS:
props.put("mail.smtp.ssl.enable", true);
break;
case TLS:
props.put("mail.smtp.starttls.enable", true);
break;
}
// If SMTP authentication is required you must set the mail.smtp.auth
// property and construct a Authenticator instance that returns
// a PasswordAuthentication instance with your username and password.
Authenticator authenticator = null;
if (auth) {
props.put("mail.smtp.auth", true);
authenticator = new Authenticator() {
private PasswordAuthentication pa
= new PasswordAuthentication(username, password);
@Override
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
//Create a Session instance using the Properties object
// and the Authenticator object.
Session session = Session.getInstance(props, authenticator);
session.setDebug(debug);
// Construct a MimeMessage instance, populate the message headers
// and content and then send the message
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(body);
Transport.send(message);
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
}
当我尝试使用 WEB APP 发送电子邮件时,Gmail 阻止我这样做并给我发邮件 "Sign-in attempt prevented"。
P.S。我还在我的 Gmail 帐户中禁用了 "Access for less secure apps"。但在下一次这样做的尝试中,Gmail 暂停了我的帐户。
任何有用的comment/solution将不胜感激。
确保您已检查列出的所有内容 here especially the DisplayUnlockCaptcha