gpt4 book ai didi

java - 如何使用 Gmail 服务器使用 java.mail 从 Web 服务器发送电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:01 24 4
gpt4 key购买 nike

我正在尝试使用 javax.mail 使用 gmail smtp 发送电子邮件。以下是我的代码

 public static void send(String from,String password,String to,String sub,String msg){  
//Get properties object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
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");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}

}

代码工作正常当我在本地服务器上运行它时,但当我尝试在 Elastic beanstalk 上运行它时(我的服务器在 AWS EBS 上运行),则会出现身份验证失败异常注意:我已从 Google A/c 设置启用对不太安全的应用程序的访问权限,但仍然收到此错误

javax.mail.AuthenticationFailedException: 534-5.7.14 Please 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 l13sm3053341iti.6 - gsmtp

最佳答案

我最近遇到了同样的问题,我发现问题出在 EC2 区域。 Google 不允许从用户不常用的位置登录不太安全的应用程序。您可以使用 Google Mail API 或使用其他一些邮件平台(例如 Yahoo)。检查您的 EC2 实例区域。尝试使用 yahoo mail 执行以下代码,将其部署在 Elastic beanstalk 或您正在使用的任何环境上。这对我有用。

public void yahooSend(String mail,String subject,String msg) {

// Sender's email ID needs to be mentioned
String from = "YOUR_YAHOO_MAIL";
String pass ="YOUR_YAHOO_PASSWORD";
// Recipient's email ID needs to be mentioned.
String to = mail;
String host = "smtp.mail.yahoo.com";

// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
// props.put("mail.smtp.user", "YAHOO_USER_NAME");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");


// Get the default Session object.
Session session = Session.getDefaultInstance(properties);

try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// Set Subject: header field
message.setSubject(subject);

// Now set the actual message
message.setText(msg);
System.out.print("Sending msg "+msg);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host,587, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System. out.println("Sent message successfully....");
}catch (MessagingException mex) {
System. out.print(mex);
mex.printStackTrace();
}
}

关于java - 如何使用 Gmail 服务器使用 java.mail 从 Web 服务器发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089555/

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