java.net.SocketException:软件导致连接中止:套接字写入错误[重复]
Posted
技术标签:
【中文标题】java.net.SocketException:软件导致连接中止:套接字写入错误[重复]【英文标题】:java.net.SocketException: Software caused connection abort: socket write error [duplicate] 【发布时间】:2012-01-05 00:51:55 【问题描述】:我正在创建一个应用程序来发送带有附件的邮件。当我附加一个小的 zip 文件时,它工作得很好,但是当我附加一个更大的(> 1mb)zip 文件时,它返回错误“java.net.SocketException:软件导致连接中止:套接字写入错误”并且我的应用程序挂起。
我附加文件的代码是:
protected void addAtachments(String[] attachments, Multipart multipart)
throws MessagingException, AddressException
System.out.println("attaching");
for (int i = 0; i <= attachments.length - 1; i++)
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
//use a JAF FileDataSource as it does MIME type detection
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(filename);
//add the attachment
multipart.addBodyPart(attachmentBodyPart);
System.out.println("attachment added: " +attachments[i]);
另外,有没有什么方法可以使用 javamail 更快地附加文件?发送附加消息花费了太多时间。
堆栈跟踪:
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
220 mx.google.com ESMTP g16sm81301847ibs.8
DEBUG SMTP: connected to host "smtp.gmail.com", port: 25
EHLO vishal
250-mx.google.com at your service, [122.178.231.153]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO vishal
250-mx.google.com at your service, [122.178.231.153]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
dmlzaGFscmsuMDU1QGdtYWlsLmNvbQ==
334 UGFzc3dvcmQ6
dmlzaGFsQG9yYWNsZQ==
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:<xyz@gmail.com>
250 2.1.0 OK g16sm81301847ibs.8
RCPT TO:<xyz@yahoo.co.in>
250 2.1.5 OK g16sm81301847ibs.8
DEBUG SMTP: Verified Addresses
DEBUG SMTP: xyz@yahoo.co.in
DATA
------=_Part_1_25068634.1322105586140
Content-Type: application/octet-stream;
name="C:\\Documents and Settings\\Vishal Kulkarni\\MyDocuments\\akon.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="C:\\Documents and Settings\\Vishal Kulkarni\\My Documents\\akon.zip"
UEsDBBQACAAIAGJMcj8AAAAAAAAAAAAAAAAkAAAARW1pbmVtLSAwMSAtIFB1YmxpYyBBbm5vdW5j
ZW1lbnQubXAz1LtlVBxB1y46DK7BneDu7u7uHmRw9+AwgxNcg7tb8BAgBCe4S7AQCBIkuATtm7zf
e++Pc3/cX3etc2qtvaa6q2s/PU937X6qa7eyHC886G+B95XVVFf/WyH5a7Zu9iAHHx8PEU5OVw9e
DnsviKtrAIfXWz1lPZ6/zfh/TeutlYujNZW0m5v7WzdrW1dbNx89HVnVvy3gv8atZyyv8/cX8V9d
WFhYT1ZT47+bTNyCzHrSajJ/q1h/TdfF0ZVK1wFiE0ClpqWlo2zwdx8jCGSozqlua+MIkXWBeHtr
eTm6QrwClOVAn0Ms52h/7qhGlyiVszDJvf5vB+b/pYOurbW7m83/dPlfy3+7/MXWsoU4G0Bc3tqC
XqD/z34cEEja19YLYm+rZutr6wJSwwSB9LTkuf82If81eVdHN1vX/5fT/3ML8JiC8P91jJHj3/sB
输出会在 15 或 20 分钟后继续打印此加密数据,但会出错
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at com.sun.net.ssl.internal.ssl.OutputRecord.writeBuffer(OutputRecord.java:283)
at com.sun.net.ssl.internal.ssl.OutputRecord.write(OutputRecord.java:272)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:665)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:109)
at com.sun.mail.util.CRLFOutputStream.write(CRLFOutputStream.java:87)
at com.sun.mail.smtp.SMTPOutputStream.write(SMTPOutputStream.java:74)
at com.sun.mail.util.CRLFOutputStream.write(CRLFOutputStream.java:65)
at com.sun.mail.util.BASE64EncoderStream.encode(BASE64EncoderStream.java:182)
at com.sun.mail.util.BASE64EncoderStream.write(BASE64EncoderStream.java:135)
at com.sun.mail.util.BASE64EncoderStream.write(BASE64EncoderStream.java:96)
at javax.activation.DataHandler.writeTo(DataHandler.java:293)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:868)
at javax.activation.DataHandler.writeTo(DataHandler.java:301)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
at escape.SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:131)
at escape.SendMailFrame.sendButtonActionPerformed(SendMailFrame.java:427)
at escape.SendMailFrame.access$300(SendMailFrame.java:29)
at escape.SendMailFrame$4.actionPerformed(SendMailFrame.java:135)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
javax.mail.MessagingException: IOException while sending message;
nested exception is:
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
java.net.SocketException: Software caused connection abort: socket write error
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at escape.SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:131)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at escape.SendMailFrame.sendButtonActionPerformed(SendMailFrame.java:427)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at escape.SendMailFrame.access$300(SendMailFrame.java:29)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at escape.SendMailFrame$4.actionPerformed(SendMailFrame.java:135)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at com.sun.net.ssl.internal.ssl.OutputRecord.writeBuffer(OutputRecord.java:283)
at com.sun.net.ssl.internal.ssl.OutputRecord.write(OutputRecord.java:272)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:665)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:109)
at com.sun.mail.util.CRLFOutputStream.write(CRLFOutputStream.java:87)
at com.sun.mail.smtp.SMTPOutputStream.write(SMTPOutputStream.java:74)
at com.sun.mail.util.CRLFOutputStream.write(CRLFOutputStream.java:65)
at com.sun.mail.util.BASE64EncoderStream.encode(BASE64EncoderStream.java:182)
at com.sun.mail.util.BASE64EncoderStream.write(BASE64EncoderStream.java:135)
at com.sun.mail.util.BASE64EncoderStream.write(BASE64EncoderStream.java:96)
at javax.activation.DataHandler.writeTo(DataHandler.java:293)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845)
IOException while sending message
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:868)
at javax.activation.DataHandler.writeTo(DataHandler.java:301)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
... 29 more
如果附件很小,它可以工作,但需要 10 到 11 分钟才能发送邮件
我正在使用 Jfilechooser 选择要附加的文件
【问题讨论】:
您是否有权访问您要发送到的邮件服务器?它很可能会拒绝数据并关闭您的连接。另外,我们能看到“socket write error”的完整堆栈跟踪吗? 我正在使用 gmail 服务器发送邮件,当我附加小型文件时它工作得很好。但是当我尝试附加更大的文件时它会出错。我已经粘贴了完整的堆栈跟踪。 【参考方案1】:服务器可能正在超时连接,因为它花费的时间太长 将消息发送到服务器。打开session debugging和协议 trace 可能会提供更多信息。
压缩文件存储在哪里?是在JavaMail所在的本地磁盘上吗 应用程序正在运行吗?
【讨论】:
嗨,我已将堆栈跟踪添加到我的问题中。我正在使用 jfilechooser 从本地磁盘中选择 zip 文件 将您对 Transport.send 的调用替换为对 msg.writeTo(new BufferedOutputStream(new FileOutputStream("msg.txt"))) 的调用。如果这与发送消息一样慢,那么问题在于读取您的 zip 文件和/或构建消息(不太可能)。 如果速度很快,那么问题在于发送消息所涉及的网络通信。这可能是名称服务查找邮件服务器名称的延迟;尝试将邮件服务器的名称替换为其 IP 地址。或者问题可能出在您的邮件服务器上,以及它愿意多快接受您的邮件。网络级协议跟踪可能会帮助您找出延迟在哪里。 10分钟发送一条消息是不正常的。 我下载了网络跟踪软件,我无法理解,但它给出了一条消息,因为应用层的 SMTP 服务器响应慢,网络层的 TCP 响应慢。这是发送消息延迟的确切原因吗? 对不起,我不知道那是什么意思。我可能需要查看网络跟踪的更多详细信息才能了解它告诉您的内容。以上是关于java.net.SocketException:软件导致连接中止:套接字写入错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章
是啥导致我的 java.net.SocketException:连接重置? [复制]
如何修复 java.net.SocketException:损坏的管道?
Java 1.6 HttpsURLConnection:java.net.SocketException:连接重置
java.net.SocketException:recvfrom 失败:ETIMEDOUT(连接超时)