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
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);
}
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
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>
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);
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();
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.