Search code examples
javaandroidemailjakarta-mail

Exception while sending an email in Java with javax.mail


I'm trying to do an Android app that sends an image automatically without any intervention from the user.

I'm using javax.mail jar (and the activation.jar and additional.jar) in order to do so. I'm doing the following

Properties props = new Properties();
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.enable",true);
props.put("mail.smtp.port", "465");

Session session = Session.getInstance(props,new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(USER,PASS);
    }
});

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("*******@gmail.com"));
message.setSubject("Email Subject");

message.addRecipient(Message.RecipientType.TO, 
                     new InternetAddress("******@gmail.com"));

MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("<img src=\"image.gif\"/>","text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(new File(ruta));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("picture.gif");
messageBodyPart.setDisposition("inline");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);

When I execute this code, I get the following error

W/System.err(24907): javax.mail.MessagingException: IOException while sending message;
W/System.err(24907): nested exception is:
W/System.err(24907): javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;
W/System.err(24907): boundary="----=_Part_0_1095625208.1397499270313"
W/System.err(24907): at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1182)
W/System.err(24907): at javax.mail.Transport.send0(Transport.java:254)
W/System.err(24907): at javax.mail.Transport.send(Transport.java:124)

Any idea on how can I solve this?

I'm using a Debian 64-bits computer and Eclipse Java EE IDE for Web Developers - Kepler. if it's needed for the solution...


Solution

  • DCH is Data Content Handler. MimeType handlers seem to be not configured correctly.

    If you can download and use latest 1.5.* version of java mail api, this error should be getting resolved.

    Otherwise, within the code you can execute some statements to set the MimeType handlers.

    Add following code snippet to your mail module and is available for the mail Session.

    MailcapCommandMap mailcapCommandMap = new MailcapCommandMap(); 
    mailcapCommandMap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true"); 
    CommandMap.setDefaultCommandMap(mailcapCommandMap); 
    

    You can extend this for additional MimeTypes

    mailcapCommandMap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
    mailcapCommandMap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
    mailcapCommandMap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
    mailcapCommandMap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
    

    Refer To:

    Apart from this method, read entire documentation on this class.