简体   繁体   English

JavaMail:套接字读取超时

[英]JavaMail: Socket read timeout

I'm using JavaMail and I want it to work through proxy for every threads (I have multithreading application). 我正在使用JavaMail,并且希望它通过代理处理每个线程(我有多线程应用程序)。 I'm using SMTPTransport.connect(Socket socket) for this. 我为此使用SMTPTransport.connect(Socket socket)。

Here is socket initialization: 这是套接字初始化:

socket = new Socket();
socket.setSoTimeout(10000);
socket.connect(new InetSocketAddress(smtpHost, smtpPort));

Here is SMTPTransport call: 这是SMTPTransport调用:

SMTPTransport transport = null;
try
    {
     transport = (SMTPTransport) mail.getTransport("smtp");
     transport.connect(socket);
     System.out.println("ok");

And so on. 等等。 But I this error happens: 但我发生此错误:

DEBUG: JavaMail version 1.4.4 DEBUG: successfully loaded resource: /META-INF/javamail.default.providers DEBUG: Tables of loaded providers DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]} DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun 调试:JavaMail版本1.4.4调试:成功加载资源:/META-INF/javamail.default.providers调试:已加载的提供程序表调试:按类名称列出的提供程序:{com.sun.mail.smtp.SMTPSSLTransport = javax。 mail.Provider [TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems,Inc],com.sun.mail.smtp.SMTPTransport = javax.mail.Provider [TRANSPORT,smtp,com.sun.mail。 smtp.SMTPTransport,Sun Microsystems,Inc],com.sun.mail.imap.IMAPSSLStore = javax.mail.Provider [STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems,Inc],com.sun。 mail.pop3.POP3SSLStore = javax.mail.Provider [STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems,Inc],com.sun.mail.imap.IMAPStore = javax.mail.Provider [STORE, imap,com.sun.mail.imap.IMAPStore,Sun Microsystems,Inc],com.sun.mail.pop3.POP3Store = javax.mail.Provider [STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems ,Inc]}调试:按协议列出的提供商:{imaps = javax.mail.Provider [STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]} DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: starting protocol to host "smtp.googlemail.com", port 465 DEBUG SMTP: exception reading response: java.net.SocketTimeoutException: Read timed out Exception reading response javax.mail.MessagingException: Exception reading respo Microsystems,Inc],imap = javax.mail.Provider [STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems,Inc],smtps = javax.mail.Provider [TRANSPORT,smtps,com.sun.mail .smtp.SMTPSSLTransport,Sun Microsystems,Inc],pop3 = javax.mail.Provider [STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems,Inc],pop3s = javax.mail.Provider [STORE,pop3s ,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems,Inc],smtp = javax.mail.Provider [TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems,Inc]}调试:成功加载资源:/META-INF/javamail.default.address.map调试:getProvider()返回javax.mail.Provider [TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems,Inc]调试SMTP:useEhlo true, useAuth true DEBUG SMTP:useEhlo是true,useAuth true DEBUG SMTP:主机“ smtp.googlemail.com”的启动协议,端口465 DEBUG SMTP:异常读取响应:java.net.SocketTimeoutException:读取超时异常读取响应javax.mail。 MessagingException:读取respo的异常 nse; nse; nested exception is: java.net.SocketTimeoutException: Read timed out at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2153) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1956) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:636) 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 com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:274) at lsmtpc.CheckAccount.run(CheckAccount.java:203) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) Caused by: java.net.SocketTimeout 嵌套的异常是:java.net.SocketTimeoutException:com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1956)的com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2153)读取超时)的com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:636)的javax.mail.Service.connect(Service.java:317)的javax.mail.Service.connect(Service.java:176)的在Java的lsmtpc.CheckAccount.run(CheckAccount.java:203)在com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:274)在javax.mail.Service.connect(Service.java:125)处。 util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)在java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:334)在java.util.concurrent.FutureTask.run(FutureTask.java: 166)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)at java.lang.Thread.run(Thread.java) :722)由:java.net.SocketTimeout Exception: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:150) at java.net.SocketInputStream.read(SocketInputStream.java:121) at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110) at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) at java.io.BufferedInputStream.read(BufferedInputStream.java:254) at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89) at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2131) ... 13 more 例外:读取时间在com的java.net.SocketInputStream.read(SocketInputStream.java:150)处的java.net.SocketInputStream.socketRead0(本机方法)处超时。 com.sun处的java.io.BufferedInputStream.fill(BufferedInputStream.java:235)处的sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)com.sun处的java.io.BufferedInputStream.read(BufferedInputStream.java:254) com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2131)上的.mail.util.LineInputStream.readLine(LineInputStream.java:89)...还有13个

So as I see JavaMail can't read from socket. 因此,正如我所见,JavaMail无法从套接字读取。 So what am I doing wrong? 那我在做什么错? If I try to use transport.connect() method without using Socket in constructor all works perfectly and smtpHost/smtpPort are accessible from the telnet and I have no any firewalls/antiviruses. 如果我尝试在构造函数中不使用Socket的情况下使用transport.connect()方法,那么它们都可以正常工作,并且可以从telnet访问smtpHost / smtpPort,并且我没有任何防火墙/防病毒软件。

From the documentation for com.sun.mail.smtp.SMTPTransport: 从com.sun.mail.smtp.SMTPTransport的文档中:

In general, applications should not need to use the classes in this package directly. 通常,应用程序不需要直接使用此包中的类。 Instead, they should use the APIs defined by javax.mail package (and subpackages). 相反,他们应该使用javax.mail包(和子包)定义的API。 Applications should never construct instances of SMTPTransport directly. 应用程序永远不要直接构造SMTPTransport的实例。 Instead, they should use the Session method getTransport to acquire an appropriate Transport object. 相反,他们应该使用Session方法getTransport来获取适当的Transport对象。

WARNING: The APIs unique to this package should be considered EXPERIMENTAL. 警告:此软件包独有的API应该被视为实验性的。 They may be changed in the future in ways that are incompatible with applications using the current APIs. 将来可能会以与使用当前API的应用程序不兼容的方式对其进行更改。

JavaMail tutorial: http://java.sun.com/developer/onlineTraining/JavaMail/contents.html JavaMail教程: http : //java.sun.com/developer/onlineTraining/JavaMail/contents.html

Could be you are not passing authentication information. 可能是您没有传递身份验证信息。 Could be you are connecting to a secured host using a plain socket. 可能是您正在使用普通套接字连接到受保护的主机。 You might want to read the tutorial linked for the best way to use JavaMail. 您可能需要阅读链接的教程,以获得使用JavaMail的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM