0

I am developing one simple Android project to capture picture from the tablet and mail it to some given id, for mailing i followed this link and it works good so i tried to attach the image by adding this method to the GMailSender class.
my code:

 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();

    }
}   

it works properly in sometimes but sometimes it throws this exception..

IOException while sending message

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

i am using Lenovo with Android ICS,
any solution ??

Community
  • 1
  • 1

1 Answers1

0

Turn on JavaMail session debugging and examine the debug output. It may give you clues as to why the peer is dropping the connection. Possibly you're just running into an unreliable server or network connection.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • yes i turned on the session debugging but the same SSLException is shown again "javax.net.ssl.SSLException: Write error: ssl=0x12975a0: I/O error during system call, Connection reset by peer" – Gopikrishnan Jul 09 '13 at 08:37
  • Are you sure you're making the SSL connection to an SSL port and not to a plain text port? Usually it will detect that error and give you an appropriate exception, but... Did you try the other tips for debugging connection problems? [This FAQ entry](http://www.oracle.com/technetwork/java/javamail/faq/index.html#installcert) might also be relevant, even though you're not getting exactly that error; in particular, the "other common causes" section. – Bill Shannon Jul 09 '13 at 18:35
  • really the problem is with my internet connection, thank you Bill Shannon. After changing the network i didn't face this problem. – Gopikrishnan Oct 18 '13 at 04:03