Currently I\'m inline of writing a code that will be listening to a directory. when the directory is updated with .apk file, I\'ll send a mail with this .apk file to a gmail
Even I had faced same issue. I tried different versions of javamail it didnt work. The problem was Transport.send()
was using MailCapCommandMap
class from the default java jdk (JAVA 8 in my case) that loaded outdated mailcap files.
So I used the latest version of JAVA after which it used MailCapCommandMap
from the activation package which loaded the correct mailcap file.
If any anyone faces same problem in future just add a breakpoint in MailCapCommandMap
classes available so that u know which mailcap file it is using.
Adding the current thread before sending the email is the solution:
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
JavaMail depends on some configuration files to map MIME types to Java classes (e.g., multipart/mixed
to javax.mail.internet.MimeMultipart
). These configuration files are loaded using the ClassLoader for the application. If the ClassLoader doesn't function properly, these configuration files won't be found.
You can simply add below lines .. that solves the issue .
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");
I am busy converting a Java 8 project to Java 10. At the same time I have been updating all of the dependencies. I was getting a similar exception and none of the above solutions worked for me.
I have the following in my pom.xml:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
I did a bit more research and found the following link:
http://www.jguru.com/faq/view.jsp?EID=237257
So I tried adding the following dependency to my pom.xml:
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
That fixed the problem, I was able to send mail with attachments again.
This issue can get a solution by taking up the below two steps.
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
If your build.xml does this: zipfileset src="javamail-1.4.7/mail.jar" excludes="META-INF/"**
Then you're stripping out the configuration information.