我正在开发一个简单的 Android 项目来从平板电脑捕获图片并将其邮寄到某个给定的 id,为了邮寄,我点击了这个链接,它工作得很好,所以我尝试通过将此方法添加到 GMailSender 类来附加图像。
我的代码:
public synchronized void sendMailandAttachment(String subject, String body, String sender, String recipients, String file) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
// Set the email message text
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setContent(body, "text/html");
// Set the email attachment file
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(file) {
@Override
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(fileDataSource.getName());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
Transport.send(message);
Log.i("Mail","transport completes..");
}catch(Exception e){
Log.i("Mail",e.getMessage());
e.printStackTrace();
}
}
它有时可以正常工作,但有时会引发此异常..
发送消息时出现 IOException
javax.mail.MessagingException: IOException while sending message;
nested exception is:
07-05 00:58:15.680: W/System.err(6960): javax.net.ssl.SSLException: Write error: ssl=0x43cce8: I/O error during system call, Connection reset by peer
07-05 00:58:15.680: W/System.err(6960): at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
07-05 00:58:15.680: W/System.err(6960): at javax.mail.Transport.send0(Transport.java:189)
07-05 00:58:15.680: W/System.err(6960): at javax.mail.Transport.send(Transport.java:118)
07-05 00:58:15.680: W/System.err(6960): at com.test.helpers.GMailSender.sendMailandAttachment(GMailSender.java:115)
07-05 00:58:15.680: W/System.err(6960): at com.test.PostImageActivity$1.run(PostImageActivity.java:75)
07-05 00:58:15.680: W/System.err(6960): Caused by: javax.net.ssl.SSLException: Write error: ssl=0x43cce8: I/O error during system call, Connection reset by peer
07-05 00:58:15.690: W/System.err(6960): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_write(Native Method)
07-05 00:58:15.690: W/System.err(6960): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:713)
07-05 00:58:15.690: W/System.err(6960): at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:114)
07-05 00:58:15.690: W/System.err(6960): at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
07-05 00:58:15.690: W/System.err(6960): at java.io.BufferedOutputStream.write(BufferedOutputStream.java:139)
07-05 00:58:15.690: W/System.err(6960): at com.sun.mail.util.CRLFOutputStream.write(CRLFOutputStream.java:84)
07-05 00:58:15.690: W/System.err(6960): at com.sun.mail.smtp.SMTPOutputStream.write(SMTPOutputStream.java:87)
07-05 00:58:15.690: W/System.err(6960): at com.sun.mail.util.CRLFOutputStream.write(CRLFOutputStream.java:75)
07-05 00:58:15.690: W/System.err(6960): at com.sun.mail.util.BASE64EncoderStream.write(BASE64EncoderStream.java:140)
07-05 00:58:15.690: W/System.err(6960): at javax.activation.DataHandler.writeTo(DataHandler.java:322)
07-05 00:58:15.690: W/System.err(6960): at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
07-05 00:58:15.700: W/System.err(6960): at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:874)
07-05 00:58:15.700: W/System.err(6960): at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:444)
07-05 00:58:15.700: W/System.err(6960): at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:102)
07-05 00:58:15.700: W/System.err(6960): at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:897)
07-05 00:58:15.700: W/System.err(6960): at javax.activation.DataHandler.writeTo(DataHandler.java:330)
07-05 00:58:15.700: W/System.err(6960): at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
07-05 00:58:15.700: W/System.err(6960): at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
07-05 00:58:15.700: W/System.err(6960): at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
07-05 00:58:15.700: W/System.err(6960): ... 4 more
我正在使用带有 Android ICS 的联想,有
什么解决方案吗??