gpt4 book ai didi

JavaMail 套接字异常 : Connection reset

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:29:32 29 4
gpt4 key购买 nike

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

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("userName@gmail.com","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 输出:

    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)

我已尝试禁用 IPv6 并关闭我的防火墙,但问题仍然存在。如果重要的话,我使用的是 Windows 7 x64。

感谢任何能帮我解决这个问题的人。

最佳答案

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

此外,您可能想要更正这些 common mistakes ,尽管我认为它们与您的问题无关。

关于JavaMail 套接字异常 : Connection reset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256566/

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