title: MessagingExceptionIOException while sending message in java?
body: <p>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</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>Mail with Attachment code :</p>
  
  <pre><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(',') &gt; 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());
  }
   }
  </code></pre>
  
  <p>Text Mail send code:</p>
  
  <pre><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(',') &gt; 0)
          message.setRecipients(Message.RecipientType.TO,
                  InternetAddress.parse(recipients));
      else
          message.setRecipient(Message.RecipientType.TO,
                  new InternetAddress(recipients));
      Transport.send(message);
  } catch (Exception e) {
  
  }
  }
  </code></pre>