简体   繁体   中英

Sending a email with an attachment using Javamail throws UnsupportedDataException

I know that this question is asked very often but I still don´t know how to fix my problem. I´m trying to send an email with an attachment. Although I tried several fixes which seem to work for other users I can´t get rid of this.

    05-18 16:52:36.974: W/System.err(3580): javax.mail.MessagingException: IOException        while sending message;
    05-18 16:52:36.974: W/System.err(3580):   nested exception is:
    05-18 16:52:36.974: W/System.err(3580):           javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
    05-18 16:52:36.974: W/System.err(3580):     boundary="----=_Part_0_1110436240.1400424754410"
    05-18 16:52:36.974: W/System.err(3580):     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1244)
    05-18 16:52:36.974: W/System.err(3580):     at walla.dev.shareit.ShareTask.sendEmail(ShareTask.java:176)
    05-18 16:52:36.974: W/System.err(3580):     at walla.dev.shareit.ShareTask.doInBackground(ShareTask.java:37)
    05-18 16:52:36.974: W/System.err(3580):     at walla.dev.shareit.ShareTask.doInBackground(ShareTask.java:1)
    05-18 16:52:36.974: W/System.err(3580):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    05-18 16:52:36.974: W/System.err(3580):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    05-18 16:52:36.974: W/System.err(3580):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    05-18 16:52:36.974: W/System.err(3580):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    05-18 16:52:36.974: W/System.err(3580):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    05-18 16:52:36.974: W/System.err(3580):     at java.lang.Thread.run(Thread.java:841)
    05-18 16:52:36.974: W/System.err(3580): Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
    05-18 16:52:36.974: W/System.err(3580):     boundary="----=_Part_0_1110436240.1400424754410"
    05-18 16:52:36.974: W/System.err(3580):     at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:905)
    05-18 16:52:36.974: W/System.err(3580):     at javax.activation.DataHandler.writeTo(DataHandler.java:330)
    05-18 16:52:36.974: W/System.err(3580):     at      javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1608)
    05-18 16:52:36.974: W/System.err(3580):     at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1849)    05-18 16:52:36.984:        W/System.err(3580):     at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1196)   05-18 16:52:36.984: W/System.err(3580):   ... 9 more

This is my function:

private void sendEmail(String phonenumber,String...params) throws AddressException, MessagingException {
    String host = "smtp.gmail.com";
    String address = "********@gmail.com";

    String from = "***********@gmail.com";
    String pass = "***********";
    String to="xxxxxxxx@gmail.com";

    Multipart multiPart;
    String finalString="";


    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", address);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    Log.i("Check", "done pops");
    Session session = Session.getDefaultInstance(props, null);
    DataHandler handler=new DataHandler(new ByteArrayDataSource(finalString.getBytes(),"text/plain" ));
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setDataHandler(handler);
    Log.i("Check", "done sessions");

    multiPart=new MimeMultipart();

    InternetAddress toAddress;
    toAddress = new InternetAddress(to);
    message.addRecipient(Message.RecipientType.TO, toAddress);
    Log.i("Check", "added recipient");
    message.setSubject("Auto-Mail with pictures for the agency");
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("This is an automatic message for used to share pictures to the agency attached the pictures. The Sharers phonenumber is:"+phonenumber);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    MimeBodyPart msgBodyPart = new MimeBodyPart();
  //search File filename by uri
    File filename=new File(params[1]);
    DataSource source = new FileDataSource(filename);
    //messageBodyPart.setDataHandler(new DataHandler(source));
    msgBodyPart.setFileName(params[1]);
    msgBodyPart.setHeader("Content-type", "image/jpeg");
    //msgBodyPart.setDisposition();
    try {
        msgBodyPart.attachFile(new File(params[1]));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    multipart.addBodyPart(msgBodyPart);
    message.setContent(multiPart);

    //message.setText("This is an automatic message for used to share pictures to the agency attached the pictures. The Sharers phonenumber is:"+phonenumber);




    Log.i("check", "transport");
    Transport transport = session.getTransport("smtp");
    Log.i("check", "connecting");
    transport.connect(host,address , pass);
    Log.i("check", "wana send");
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

    Log.i("check", "sent");

I don´t know wether it´s important, I´ve downloaded the newest javax.mail.jar-version (1.5.2) from oracles webpage but the parts activation.jar and additional.jar are from googlecode, cause I don´t find them on the webpage .

If you need further informations please comment.

If you are using java 6 or newer the activation.jar (JavaBeans Activation Framework, JAF) is already included in the standard libs, so you don't need them. The additional.jar is unknown to me, i guess you don't need it too. For examples how to send mails with attachments look here: http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm

If you still need infos about activation.jar/JAF look here: http://www.oracle.com/technetwork/java/jaf11-139815.html

Try the workaround described here .

If that doesn't work, try adding this to your program:

MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();  
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");  
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");  
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");  
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");  
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");  
CommandMap.setDefaultCommandMap(mc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM