JavaMail:套接字读取超时
我正在使用 JavaMail,我希望它通过每个线程的代理工作(我有多线程应用程序)。我为此使用 SMTPTransport.connect(Socket socket) 。
这是套接字初始化:
socket = new Socket();
socket.setSoTimeout(10000);
socket.connect(new InetSocketAddress(smtpHost, smtpPort));
这是 SMTPTransport 调用:
SMTPTransport transport = null;
try
{
transport = (SMTPTransport) mail.getTransport("smtp");
transport.connect(socket);
System.out.println("ok");
等等。但我发生这个错误:
调试:JavaMail 版本 1.4.4 调试:成功加载资源: /META-INF/javamail.default.providers DEBUG:加载的提供程序表 调试:按类名称列出的提供程序: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun 微系统公司], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun 微系统公司], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun 微系统公司], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun 微系统公司], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun 微系统公司], 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 微系统公司], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun 微系统公司], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun 微系统公司], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun 微系统公司], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun 微系统公司], 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 调试 SMTP: useEhlo true、useAuth true DEBUG SMTP:启动主机协议 “smtp.googlemail.com”,端口 465 DEBUG SMTP:读取异常 响应:java.net.SocketTimeoutException:读取超时异常 读取响应 javax.mail.MessagingException:读取异常 回复;嵌套异常是:java.net.SocketTimeoutException: 读取超时于 com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2153) 在 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1956) 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:636) 在 javax.mail.Service.connect(Service.java:317) 处 javax.mail.Service.connect(Service.java:176) 在 javax.mail.Service.connect(Service.java:125) 在 com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:274) 在 lsmtpc.CheckAccount.run(CheckAccount.java:203) 在 java.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) 处 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 在 java.lang.Thread.run(Thread.java:722) 引起的: java.net.SocketTimeoutException:读取超时 java.net.SocketInputStream.socketRead0(本机方法)位于 java.net.SocketInputStream.read(SocketInputStream.java:150) 在 java.net.SocketInputStream.read(SocketInputStream.java:121) 在 com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110) 在 java.io.BufferedInputStream.fill(BufferedInputStream.java:235) 在 java.io.BufferedInputStream.read(BufferedInputStream.java:254) 在 com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89) 在 com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2131) ... 13 更多
因此,据我所知,JavaMail 无法从套接字读取。那么我做错了什么?如果我尝试使用 Transport.connect() 方法而不在构造函数中使用 Socket,那么所有工作都完美,并且 smtpHost/smtpPort 可以从 telnet 访问,并且我没有任何防火墙/防病毒软件。
I'm using JavaMail and I want it to work through proxy for every threads (I have multithreading application). I'm using SMTPTransport.connect(Socket socket) for this.
Here is socket initialization:
socket = new Socket();
socket.setSoTimeout(10000);
socket.connect(new InetSocketAddress(smtpHost, smtpPort));
Here is SMTPTransport call:
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
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
response; 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.SocketTimeoutException: 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
So as I see JavaMail can't read from socket. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 com.sun.mail.smtp.SMTPTransport 的文档:
JavaMail 教程: http://java.sun.com/developer/onlineTraining/JavaMail/ content.html
可能是您没有传递身份验证信息。您可能正在使用普通套接字连接到安全主机。您可能需要阅读链接的教程,了解使用 JavaMail 的最佳方法。
From the documentation for com.sun.mail.smtp.SMTPTransport:
JavaMail tutorial: 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.