我正在尝试使用Java邮件Api发送电子邮件,并且我的应用程序已部署在Google App引擎上,
之前我可以通过Java代码发送电子邮件,但最近它已停止,
波纹管是错误javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuq534-5.7.14 szPbAPjs-PmJgBd5xmPAVgRB8-3WxkoQy7WaGBIGRyltJO9LxKnw0oagw1e-jZeQcgABnK534-5.7.14 mcxSUPyD4ohtBPkovBS45tnMElRdeWM7mzfw2wU3vqqQpDw7_8Z9rphQR_SZpR> Please534-5.7.14 log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 186sm22205348pfb.99 - gsmtp
我已经做了一切,在IAM中添加了gmail帐户,因为所有者在gmail中启用了lesssecure选项,但是仍然出现相同的错误
这是代码
public static void sendWelcomeMail(String msg,String email,String subject) {
System.out.println("Starte - "+email);
final String username = "serv*******@gmail.com";
final String password = "********";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(email+", fo*********@gmail.com")
);
message.setSubject(subject);
Multipart multipart = new MimeMultipart(); //1
// Create the HTML Part
BodyPart htmlBodyPart = new MimeBodyPart(); //4
htmlBodyPart.setContent(msg, "text/html"); //5
multipart.addBodyPart(htmlBodyPart); // 6
// Set the Multipart's to be the email's content
message.setContent(multipart); //7
Transport.send(message);
System.out.println("Done");
}catch(Exception e) {
e.printStackTrace();
}
}
但是我可以从本地计算机发送电子邮件。
让我知道我在做什么错。
TIA
最佳答案
因此,这是我用来解决问题的更改。
我更改了代码的这一部分:
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(email+", fo*********@gmail.com")
);
对此:
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(email, "")
);
以及代码的另一部分:
message.setFrom(new InternetAddress(username));
对此:
message.setFrom(new InternetAddress(username, "Admin"));
结果是完整的代码:
public static void sendWelcomeMail(String msg,String email,String subject) {
final String username = "ser******@gmail.com";
final String password = "**********";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username, "Admin"));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(email, "")
);
message.addRecipient(
Message.RecipientType.CC,
new InternetAddress("fo********@gmail.com", "")
);
message.setSubject(subject);
Multipart multipart = new MimeMultipart(); //1
// Create the HTML Part
BodyPart htmlBodyPart = new MimeBodyPart(); //4
htmlBodyPart.setContent(msg, "text/html"); //5
multipart.addBodyPart(htmlBodyPart); // 6
// Set the Multipart's to be the email's content
message.setContent(multipart); //7
Transport.send(message);
System.out.println("Done");
}catch(Exception e) {
System.out.println("Exception in sending mail - "+e.getMessage());
e.printStackTrace();
}
}