Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessagingExceptionIOException while sending message in java?

I use the following code to send mail.Text message sending is working fine but Mail with attachment is not working it gives the Exception.How to solve this

javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_10430987.1294298904906" at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676) at javax.mail.Transport.send0(Transport.java:189) at javax.mail.Transport.send(Transport.java:118) at Gmailer.GMailSender.sendMailAttach(GMailSender.java:114) at SendMail.main(SendMail.java:22) Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_10430987.1294298904906" at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) at javax.activation.DataHandler.writeTo(Unknown Source) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636) ... 4 more

Mail with Attachment code :

public synchronized void sendMailAttach(String subject, String body,
    String sender, String recipients)  {
try {
    MimeMessage message = new MimeMessage(session);

    message.setSender(new InternetAddress(sender));
    message.setSubject(subject);

    // Create the message part 
    BodyPart messageBodyPart = new MimeBodyPart();

    // Fill the message
    messageBodyPart.setText("hi Demo");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    String filename = "mail.txt";
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);

    // Put parts in message
    message.setContent(multipart);

    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 (MessagingException e) {
    System.out.println("MessagingException" + e.getMessage());
}
catch (Exception e) {
    System.out.println("Mail Send Exception " + e.getMessage());
}
 }

Text Mail send code:

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

}
}
like image 728
Finder Avatar asked Jan 06 '11 07:01

Finder


2 Answers

Firstly, you could make your code a little more concise by using MimeBodyPart.attachFile() instead of wrangling the DataSource/DataHandler code yourself.

Secondly, try setting the Content-Type and Content-Disposition headers on your attachment part with appropriate values. (attachFile will set the Content-Disposition for you by default.) For instance,

messageBodyPart = new MimeBodyPart();
messageBodyPart.attachFile(new File("mail.txt"));
messageBodyPart.setHeader("Content-Type", "text/plain; charset=\"us-ascii\"; name=\"mail.txt\"");



EDIT:

After thinking a bit, this has to be something amiss with class loading. Please check this other SO thread to see if it remedies your situation. (General issue: Probably an extra activation.jar in your classpath; a few other possibilities also thought to cause it.)

like image 76
dkarp Avatar answered Sep 16 '22 20:09

dkarp


Use that code put that code and then chek that is really works. Frist, import that package

import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;

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);
like image 37
Renish Khunt Avatar answered Sep 17 '22 20:09

Renish Khunt