gpt4 book ai didi

java - 无法从 GAE 的 java mail APi 发送邮件

转载 作者:行者123 更新时间:2023-12-02 09:17:15 27 4
gpt4 key购买 nike

我正在尝试使用 Java 邮件 Api 发送电子邮件,并且我的应用程序部署在 Google App Engine 上,

早些时候我能够从我的java代码发送电子邮件,但最近它停止了,

下面是错误

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuq
534-5.7.14 szPbAPjs-PmJgBd5xmPAVgRB8-3WxkoQy7WaGBIGRyltJO9LxKnw0oagw1e-jZeQcgABnK
534-5.7.14 mcxSUPyD4ohtBPkovBS45tnMElRdeWM7mzfw2wU3vqqQpDw7_8Z9rphQR_SZpR> Please
534-5.7.14 log in via your web browser and then try again.
534-5.7.14 Learn more at
534 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(用户名));

对此:

message.setFrom(new InternetAddress(用户名, "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();
}
}

关于java - 无法从 GAE 的 java mail APi 发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58910300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com