【发布时间】:2022-01-18 02:02:33
【问题描述】:
我查看了其他类似的问题,这些问题建议添加 properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");,但仍然对我不起作用。我还看到了一些建议将 properties.put("mail.smtp.starttls.enable", "true"); 注释掉的答案
这对我也不起作用。除了默认的 windows 1 和其他特殊程序之外,我没有任何杀毒软件;为什么会这样?
代码
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 = "alexfarts05@gmail.com";
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
【问题讨论】:
-
您是否尝试过添加
props.put("properties.transport.protocol", "smtps")并将smtp替换为smtps? -
是的,我收到
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;错误 -
是的,我完全复制了它,但我仍然收到错误
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587
标签: java email ssl smtp jakarta-mail