gpt4 book ai didi

ssl - Javamail 不适用于 gmail SMTP

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:21 24 4
gpt4 key购买 nike

尝试使用 javamail 从我的 java 代码发送电子邮件时出现异常。我正在尝试使用 gmail SMTP(SSL,端口 465)。 TLS 工作(端口 587)。

这是我正在做的。

mailServerProperties = System.getProperties();    
mailServerProperties.put("mail.smtp.port", "465");
mailServerProperties.put("mail.smtp.auth", "true");

mailServerProperties.put("mail.smtp.socketFactory.port", "465");
mailServerProperties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");


getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(ei.getToaddr()));
generateMailMessage.setFrom(new InternetAddress(ei.getUser()));
generateMailMessage.setSubject(ei.getSubject());
generateMailMessage.setText(ei.getMessage());

transport = getMailSession.getTransport("smtp");
transport.connect("smtp.gmail.com", "user", "password");

获取此异常...

2017-10-01 06:53:51.094 UTC SEVERE: 2542: EnhancedEmailer.generateAndSendEmail: Exception sending email
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2042)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)

是否有任何属性看起来不对?

最佳答案

按照以下步骤操作:

  1. 在您的电子邮件中禁用“双因素身份验证”
  2. 导航至:“https://myaccount.google.com/lesssecureapps?pli=1”并开启“访问安全性较低的应用程序”
  3. 下载 JavaMail API“https://www.oracle.com/technetwork/java/javamail/index-138643.html”并将其添加到您的库

代码

import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class email_try {


public static void main(String ap[]) {



String myEmail = "YOUR EMAIL";
String password = "YOUR PASSWORD";
String opponentEmail = "THEIR EMAIL";
Properties pro = new Properties();
pro.put("mail.smtp.host", "smtp.gmail.com");
pro.put("mail.smtp.starttls.enable", "true");
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.port", "587");
Session ss = Session.getInstance(pro, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myEmail, password);
}
});



try {
Message msg = new MimeMessage(ss);
msg.setFrom(new InternetAddress(myEmail));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(opponentEmail));
msg.setSubject("Your Wish");
msg.setText("java email app");
Transport trans = ss.getTransport("smtp");
Transport.send(msg);
System.out.println("message sent");
} catch (Exception e) {
System.out.println(e.getMessage());
}

}
}

//尝试此代码并输入正确的电子邮件 ID 和密码

关于ssl - Javamail 不适用于 gmail SMTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509971/

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