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) {
}
}
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\"");
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.)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With