【发布时间】:2020-01-12 10:12:27
【问题描述】:
我正在尝试使用 javax.mail 版本 1.4.7 发送电子邮件。这是我用来发送邮件的代码:
String protocollo = "smtps";
String usernamePec = "fromAddress@pec.it";
String passwordPec = "mypassword";
String pecTo = "ToAddress@pec.it";
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", "smtps.pec.aruba.it"); // esempio smtp.gmail.com
props.put("mail.smtps.auth", "true");
Session session = Session.getInstance(props,new javax.mail.Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication( usernamePec,passwordPec);
}
});
MimeMessage messaggioEmail = new MimeMessage( session );
MimeMessageHelper helper = new MimeMessageHelper(messaggioEmail, true);
helper.setFrom(usernamePec);
helper.setTo(pecTo);
helper.setSubject("Test Subject");
helper.setPriority(1);
helper.setText("Body Mail ", false);
messaggioEmail.saveChanges();
com.sun.mail.smtp.SMTPMessage mex = new SMTPMessage(messaggioEmail);
com.sun.mail.smtp.SMTPSSLTransport t =
(com.sun.mail.smtp.SMTPSSLTransport)session.getTransport(protocollo); // <--SMTPS
t.setStartTLS(true); //<-- impostiamo il flag per iniziare la comunicazione sicura
t.connect( "smtps.pec.aruba.it", usernamePec,passwordPec);
t.sendMessage( mex, mex.getAllRecipients());
t.close();
运行此代码我得到以下异常:
javax.mail.MessagingException: Could not connect to SMTP host: smtps.pec.aruba.it, port: 465;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at test.Test.main(Test.java:199)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 4 more
由于 ssl 证书握手错误,这似乎是一个问题,对此有何建议,我在哪里做错了?
【问题讨论】:
-
您的 SSL 设置可能存在问题。
STARTTLS is different to SSL and TLS. Before encryption was standard, many connections between an email client and the server were done insecurely. This put personal information in danger of being stolen. STARTTLS helped to reduce this risk by taking an existing insecure connection and upgrading it to a secure connection that used SSL/TLS. STARTTLS works with either SSL or TLS.reference。因此,您可能需要从一开始就对您的连接进行 SSL/TLS 加密。
标签: java jakarta-mail tls1.2