java JavaMail SocketException:连接重置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16256566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:25:29  来源:igfitidea点击:

JavaMail SocketException: Connection reset

javajavamail

提问by user2327683

I'm trying to send emails through JavaMail API but I end up receiving the SocketException: Connection reset.

我正在尝试通过 JavaMail API 发送电子邮件,但我最终收到了 SocketException: Connection reset。

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMailSSL {
 public static void main(String[] args) {

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");

     Authenticator auth = new Authenticator() {

             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication("[email protected]","gmailPassword");
             }
         };

    Session session = Session.getDefaultInstance(props,auth);


    try {

    Message message = new MimeMessage(session);
    Address sender  = new InternetAddress("any@...");
    message.setFrom(sender);

    String recipients = "email1@...,email2@...,email2@...";
    String[] toList = recipients.split(",");
    System.out.println(toList.length);
    Address[] addressTo = new InternetAddress[toList.length];

    for(int i = 0; i < toList.length; i++){
    addressTo[i] = new InternetAddress(toList[i]);  
    }

    for( int i=0; i < addressTo.length; i++) { // changed from a while loop
            message.addRecipient(Message.RecipientType.TO, addressTo[i]);
        }


    message.setSubject("Testing Subject 5");
    message.setText("Dear Message ," +
    "\n\n HELLO, please! \n https://192.168.192.120:8181/centralWeb");
    System.out.println("SENDING MAIL......... " + new Date().toString());

    message.setHeader("Content-type", "text/html; charset=UTF-8");
    Transport.send(message);

    System.out.println("Done  " + new Date().toString());

    } catch (MessagingException e) {
    throw new RuntimeException(e);
    }

 }
}

NetBeans Output:

NetBeans 输出:

    1
SENDING MAIL......... Sun Apr 28 01:30:18 IST 2013
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    java.net.SocketException: Connection reset
    at sendmailssl.SendMailSSL.main(SendMailSSL.java:65)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at sendmailssl.SendMailSSL.main(SendMailSSL.java:60)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:312)
    at sun.security.ssl.InputRecord.read(InputRecord.java:350)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1328)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
    ... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

I've tried disabling IPv6 and turning off my firewall too, but the problem persists. I am using Windows 7 x64, if that matters.

我试过禁用 IPv6 并关闭我的防火墙,但问题仍然存在。如果这很重要,我正在使用 Windows 7 x64。

Thanks to anyone who can help me fix this issue.

感谢任何可以帮助我解决此问题的人。

回答by Bill Shannon

Try these debugging tips from the JavaMail FAQ:

试试 JavaMail 常见问题中的这些调试技巧:

Also, you might want to correct these common mistakes, although I don't think they're related to your problem.

此外,您可能希望更正这些常见错误,尽管我认为它们与您的问题无关。