java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail

The javax.mail-api artifact is only good for compiling against. You actually need to run code, so you need a complete implementation of JavaMail API. Use this: <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> NOTE: The version number will probably differ. Check the latest version here.

MimeMessage.saveChanges is really slow

Fix the most common mistakes people make when using JavaMail in your code first. DNS lookup can hurt performance on some machines. For the JDK you can change the security properties for caching DNS lookup networkaddress.cache.ttl and networkaddress.cache.negative.ttl or set the system properties sun.net.inetaddr.ttl and sun.net.inetaddr.negative.ttl. The default behavior in JDK 7 and later does … Read more

Javamail api in android using XOauth

I researched this for some days and I found a solution that is working for me at the moment. I get the oauth2 token from the android AccountManager and then send the email via SMTP using JavaMail. The idea is based on the Java example here http://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCode and on this java Xoauth example here http://google-mail-xoauth-tools.googlecode.com/svn/trunk/java/com/google/code/samples/xoauth/XoauthAuthenticator.java … Read more

How to read text inside body of mail using javax.mail

This answer extends yurin’s answer. The issue he brought up was that the content of a MimeMultipart may itself be another MimeMultipart. The getTextFromMimeMultipart() method below recurses in such cases on the content until the message body has been fully parsed. private String getTextFromMessage(Message message) throws MessagingException, IOException { String result = “”; if (message.isMimeType(“text/plain”)) … Read more

Using JavaMail with TLS

We actually have some notification code in our product that uses TLS to send mail if it is available. You will need to set the Java Mail properties. You only need the TLS one but you might need SSL if your SMTP server uses SSL. Properties props = new Properties(); props.put(“mail.smtp.starttls.enable”,”true”); props.put(“mail.smtp.auth”, “true”); // If … Read more