我查看了其他類似的問題,這些問題建議添加properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");仍然對我不起作用的問題。我還看到一些建議注釋掉的答案,properties.put("mail.smtp.starttls.enable", "true");
這對我也不起作用。除了默認的 windows one 之外,我沒有任何防病毒軟體,也沒有其他特殊程式;為什么會這樣?
代碼
public static void sendMail(String recipient) throws MessagingException {
System.out.println("Preparing to send email...");
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.debug", "true");
properties.put("mail.smtp.port", "587");
String myAccountEmail = "[email protected]";
String myAccountPassword = "alexIsFarting!";
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, myAccountPassword);
}
});
Message message = prepareMessage(session, myAccountEmail, recipient);
Transport.send(message);
System.out.println("Email sent!");
}
private static Message prepareMessage(Session session, String myAccountEmail, String recipient) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(myAccountEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("Hello, this is a test email!");
message.setText("Big strong men");
return message;
} catch (Exception ex) {
Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
完全錯誤
Exception in thread "main" javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at SendMail.sendMail(SendMail.java:33)
at Main.main(Main.java:5)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172)
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:239)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:443)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:527)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:464)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
... 8 more
uj5u.com熱心網友回復:
我找到了解決方案,將此行添加properties.put("mail.smtp.ssl.protocols", "TLSv1.2"到屬性中,以及為您使用的谷歌帳戶創建應用程式密碼修復了它。您需要在帳戶上啟用 2FA,然后在您的程式中使用創建的密碼
完成最終屬性
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.starttls.required", "true");
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/383136.html
