Unable to send Mail with Attachments Android
Asked Answered
M

0

1

Friends

I am trying to send mail form my application i received this error

this is my error:

02-07 15:28:06.852: W/System.err(1051): javax.mail.MessagingException: IOException while sending message;
02-07 15:28:06.852: W/System.err(1051):   nested exception is:
02-07 15:28:06.852: W/System.err(1051):     javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 

02-07 15:28:06.852: W/System.err(1051):     boundary="----=_Part_0_1156236192.1328608681807"
02-07 15:28:06.882: W/System.err(1051):     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141)
02-07 15:28:06.882: W/System.err(1051):     at javax.mail.Transport.send0(Transport.java:195)
02-07 15:28:06.882: W/System.err(1051):     at javax.mail.Transport.send(Transport.java:124)
02-07 15:28:06.882: W/System.err(1051):     at com.controltest.activities.GMailSender.sendMail(GMailSender.java:115)
02-07 15:28:06.892: W/System.err(1051):     at com.controltest.activities.SmtActivity$1.onClick(SmtActivity.java:62)
02-07 15:28:06.892: W/System.err(1051):     at android.view.View.performClick(View.java:2408)
02-07 15:28:06.892: W/System.err(1051):     at android.view.View$PerformClick.run(View.java:8816)
02-07 15:28:06.892: W/System.err(1051):     at android.os.Handler.handleCallback(Handler.java:587)
02-07 15:28:06.892: W/System.err(1051):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 15:28:06.892: W/System.err(1051):     at android.os.Looper.loop(Looper.java:123)
02-07 15:28:06.892: W/System.err(1051):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-07 15:28:06.892: W/System.err(1051):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 15:28:06.892: W/System.err(1051):     at java.lang.reflect.Method.invoke(Method.java:521)
02-07 15:28:06.892: W/System.err(1051):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-07 15:28:06.892: W/System.err(1051):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-07 15:28:06.892: W/System.err(1051):     at dalvik.system.NativeStart.main(Native Method)
02-07 15:28:06.892: W/System.err(1051): Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 

02-07 15:28:06.902: W/System.err(1051):     boundary="----=_Part_0_1156236192.1328608681807"
02-07 15:28:06.902: W/System.err(1051):     at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905)
02-07 15:28:06.902: W/System.err(1051):     at javax.activation.DataHandler.writeTo(DataHandler.java:330)
02-07 15:28:06.902: W/System.err(1051):     at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476)
02-07 15:28:06.902: W/System.err(1051):     at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772)
02-07 15:28:06.902: W/System.err(1051):     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099)
02-07 15:28:06.902: W/System.err(1051):     ... 15 more

my code:

senderactivity.java

try{
File file1 = new File(path);

sender.sendMail("Test File", message,"sendermailaddress", "receivermailaddress",file1);

}
catch(Exception e){
e.printStackTrace();
}

.....................................

GmailSender.java

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Provider;
import java.security.Security;
import java.util.Properties;

public class GMailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;

    static {
        Security.addProvider(new com.controltest.activities.JSSEProvider());
    }

    public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);

    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body,
            String sender, String recipients) 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));
            Transport.send(message);
        } catch (Exception e) {
         e.printStackTrace();
        }
    }

    public synchronized void sendMail(String subject, String body,
            String sender, String recipients, File attachment) throws Exception {
        try {
            MimeMessage message = new MimeMessage(session);
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);

            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(body);

//          FileInputStream fis = new FileInputStream(attachment);
            MimeBodyPart mbp2 = new MimeBodyPart();
            mbp2.attachFile(attachment);
//          mbp2.setHeader("Content-Type", "text/plain; charset=\"us-ascii\"; name=\"\"");
//          FileDataSource fds = new FileDataSource(attachment);
//          mbp2.setDataHandler(new DataHandler(fds));
            Log.d("File Name",""+attachment.getName());
             mbp2.setFileName(attachment.getName());

            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);

            message.setContent(mp);

            if (recipients.indexOf(',') > 0)
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO,
                        new InternetAddress(recipients));
            Transport.send(message);
        } catch (Exception e) {
               e.printStackTrace();
        }
    }

    public class ByteArrayDataSource implements DataSource {
        private byte[] data;
        private String type;

        public ByteArrayDataSource(byte[] data, String type) {
            super();
            this.data = data;
            this.type = type;
        }

        public ByteArrayDataSource(byte[] data) {
            super();
            this.data = data;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getContentType() {
            if (type == null)
                return "application/octet-stream";
            else
                return type;
        }

        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(data);
        }

        public String getName() {
            return "ByteArrayDataSource";
        }

        public OutputStream getOutputStream() throws IOException {
            throw new IOException("Not Supported");
        }
    }
}

.................................................................................

JSSEProvider.java

import java.security.AccessController;
import java.security.Provider;

public final class JSSEProvider extends Provider {

    public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
            public Void run() {
                put("SSLContext.TLS",
                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }
}

......................

i have added all jars : mail.jar, activation.jar additionnal.jar

i tried this link's solution but its not working. MessagingExceptionIOException while sending message in java?

This is only problem with attachments without attachments its working fine

any help will be appreciate.

Thanks...

Mckinzie answered 7/2, 2012 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.