使用Javamail发送带附件的电子邮件会抛出UnsupportedDataException

时间:2014-05-18 15:14:12

标签: java android email javamail

我知道这个问题经常被问到,但我仍然不知道如何解决我的问题。 我正在尝试发送带有附件的电子邮件。 虽然我尝试了几个似乎适用于其他用户的修复程序,但我无法摆脱这种情况。

    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

这是我的功能:

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

我不知道它是否重要,我从oracles网页下载了最新的javax.mail.jar-version(1.5.2),但部件activation.jar和additional.jar来自googlecode,原因我在webpage上找不到它们。

如果您需要进一步的信息,请发表评论。

2 个答案:

答案 0 :(得分:0)

如果您使用的是java 6或更新版本,则activ.jar(JavaBeans Activation Framework,JAF)已包含在标准库中,因此您不需要它们。我不知道extra.jar,我想你也不需要它。有关如何发送带附件的邮件的示例,请参阅此处:http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm

如果你仍然需要关于activation.jar / JAF的信息,请看这里:http://www.oracle.com/technetwork/java/jaf11-139815.html

答案 1 :(得分:0)

尝试使用here所述的解决方法。

如果这不起作用,请尝试将其添加到您的程序中:

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