Reputation: 1217
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) {
}
}
Upvotes: 5
Views: 64157
Reputation: 1
Pls refer to this link: http://forum.spring.io/forum/osgi-related/dm-server-general/61205-problems-sending-mime-multipart-mails
It explains a probable cause of the problem and generally advise on resolving same.
I encountered same while using apache camel on jboss fuse. I had to modify my pom.xml... see snippets below:
<!-- add a depency on javax.mail, in addition to the dependency on camel-mail -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mail</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.1</version>
</dependency>
<!-- explicitly import the com.sun.mail.handlers package under the Import-Package section of the pom.xml -->
<Import-Package>com.sun.mail.handlers, *</Import-Package>
Upvotes: 0
Reputation: 65
Set this value:
message.setContent(body, "text/html; charset=UTF-8");
messageBodyPart.setContent(body, "text/html; charset=UTF-8");
Also check the file path, or please write complete path in your log
Upvotes: 1
Reputation: 5824
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);
Upvotes: 1
Reputation: 1304
Try to define the exact path of your file, the mail.txt
.
For example if it is in C:
then go ahead and include the whole path like
String filename = ("c:\\users\\mail.txt");
and please note that sometime you might experience problems with ("c:\users\mail.txt")
which has single backslashes so to be safe just make them double backslashes
Also always make sure you use .printStackTrace()
method since it will help you identify where exactly your problem will be arising so that when you search on that problem you will be in good position to know exactly what you will be looking for.
Upvotes: 0
Reputation: 1
static {
// add handlers for main MIME types
MailcapCommandMap mcap = new MailcapCommandMap();
mcap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mcap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mcap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mcap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true");
mcap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mcap);
}
Upvotes: 0
Reputation: 14763
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.)
Upvotes: 7
Reputation: 4740
This may stem from class loading issues (duplicat versions of activation.jar). See for example http://forum.springsource.org/archive/index.php/t-69180.html
or google some more
Upvotes: 0
Reputation: 65
I think Your program is not able to find the file mail.txt. Please give correct/complete path. An exception stacktrace can give more idea. If possible reply with that. e.printStacktrace();
Upvotes: 0