gpt4 book ai didi

javax.mail.MessagingException : Could not convert socket to TLS

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:31 29 4
gpt4 key购买 nike

我有另一个convert socket 异常。 Stackoverflow 将我链接到相关答案,我已经完成了人们在那里建议的一切。

这里我们有异常描述:

Exception in thread "main" javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class:
sun.security.ssl.SSLContextImpl$DefaultSSLContext) at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1880) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:648) at javax.mail.Service.connect(Service.java:295) at XMailMessenger.MailMessenger.sendMail(MailMessenger.java:414) at alfa.runner.main(runner.java:38) Caused by: java.net.SocketException: java.security.NoSuchAlgorithmException: Error
constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext) at javax.net.ssl.DefaultSSLSocketFactory.throwException(Unknown Source) at javax.net.ssl.DefaultSSLSocketFactory.createSocket(Unknown Source) at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:432) at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1875) ... 4 more Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation
(algorithm: Default, provider: SunJSSE, class:
sun.security.ssl.SSLContextImpl$DefaultSSLContext) at java.security.Provider$Service.newInstance(Unknown Source) at sun.security.jca.GetInstance.getInstance(Unknown Source) at sun.security.jca.GetInstance.getInstance(Unknown Source) at javax.net.ssl.SSLContext.getInstance(Unknown Source) at javax.net.ssl.SSLContext.getDefault(Unknown Source) at javax.net.ssl.SSLSocketFactory.getDefault(Unknown Source) at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:427) ... 5 more Caused by: java.lang.NullPointerException at sun.security.ssl.KeyManagerFactoryImpl$SunX509.engineInit(Unknown Source) at javax.net.ssl.KeyManagerFactory.init(Unknown Source) at sun.security.ssl.SSLContextImpl$DefaultSSLContext.getDefaultKeyManager(Unknown
Source)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.(Unknown Source) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) ... 12 more

所以我决定再写两个类:

      package XMailMessenger;

import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
/**
* @author Raminderjeet Singh
*/
public class DummyTrustManager implements X509TrustManager {

public void checkClientTrusted(X509Certificate[] cert, String authType) {
// everything is trusted


}

public void checkServerTrusted(X509Certificate[] cert, String authType) {
// everything is trusted
}

public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}

第二个:

 package XMailMessenger;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.*;

/**
* @author Raminderjeet Singh
*/
public class DummySSLSocketFactory extends SSLSocketFactory {
private SSLSocketFactory factory;

public DummySSLSocketFactory() {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null,
new TrustManager[] { new DummyTrustManager()},
null);
factory = (SSLSocketFactory)sslcontext.getSocketFactory();
} catch(Exception ex) {
// ignore
}
}

public static SocketFactory getDefault() {
return new DummySSLSocketFactory();
}

public Socket createSocket() throws IOException {
return factory.createSocket();
}

public Socket createSocket(Socket socket, String s, int i, boolean flag)
throws IOException {
return factory.createSocket(socket, s, i, flag);
}

public Socket createSocket(InetAddress inaddr, int i,
InetAddress inaddr1, int j) throws IOException {
return factory.createSocket(inaddr, i, inaddr1, j);
}

public Socket createSocket(InetAddress inaddr, int i)
throws IOException {
return factory.createSocket(inaddr, i);
}

public Socket createSocket(String s, int i, InetAddress inaddr, int j)
throws IOException {
return factory.createSocket(s, i, inaddr, j);
}

public Socket createSocket(String s, int i) throws IOException {
return factory.createSocket(s, i);
}

public String[] getDefaultCipherSuites() {
return factory.getDefaultCipherSuites();
}

public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
}

终于有了发送邮件的方法了:

        Properties props = new Properties();
props.put("mail.smtp.host", currentSettingsDocument.getSMTPHostServer());
props.put("mail.smtp.user", currentSettingsDocument.getLogin());
props.put("mail.smtp.password", currentSettingsDocument.getPassword());
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.timeout" , "3000");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.socketFactory.port", "465");

props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("javax.net.debug","ssl,session");

// Required to avoid security exception.
Authenticator authentication = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(currentSettingsDocument.getLogin() ,
currentSettingsDocument.getPassword());
}
};
Session session = Session.getInstance(props, authentication);
// create some properties and get the default Session

// create a message
MimeMessage msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new


InternetAddress(currentSettingsDocument.getSenderMail());
msg.setFrom(addressFrom);


for(InternetAddress addr: recepients )
msg.addRecipient(Message.RecipientType.TO, addr );

// Optional : You can also set your custom headers in the Email if you
//
// msg.addHeader("MyHeaderName", "myHeaderValue");

// Setting the Subject and Content Type
msg.setContent("1111", "text/plain");
Transport transport = session.getTransport("smtp");
Document.getSMTPHostServer(), Document.getPassword());
transport.connect(currentSettingsDocument.getSMTPHostServer(), 465,
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

在调用 sendMessage 之后我得到异常:

окт 18, 2013 5:36:18 PM XMailMessenger.MailMessenger sendMail SEVERE: null javax.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketTimeoutException: Read timed out

PS Google 邮件正常运行

最佳答案

该异常是由旧 Windows(XP、Vista)上不支持的 TLS 引起的,并由 SocketFetcher 类(com.sun.mail.util 包;)在第

行抛出
MailSSLSocketFactory msf = new MailSSLSocketFactory();

和 MailSSLSocketFactory(同一个包)/**
* 初始化一个新的 MailSSLSocketFactory。
*
* @throws 一般安全异常
*/
公共(public) MailSSLSocketFactory() 抛出 GeneralSecurityException {
这(“TLS”);
}

关于javax.mail.MessagingException : Could not convert socket to TLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19451290/

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