Wednesday 11 July 2012

Java Mail does not set a subject problem (Conflicting with openejb)

Considering the following code:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Test{
    private static Properties props = System.getProperties();

    public static void main(String[] args) throws Exception {
        String     host  = "xxxx";
        String     port = "xx";
        String     user = "xxxx";
        String     from = "xxxx";
        String     pass = "xxxx";
        String     text = "Test mail";
        String     to   = "xxxx";
       
        javax.mail.Session session = javax.mail.Session.getDefaultInstance(props, null); 
        javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session); 
        message.setFrom(new javax.mail.internet.InternetAddress(from)); 
        message.addRecipient(javax.mail.Message.RecipientType.TO, new javax.mail.internet.InternetAddress(to));
        message.setSubject("Test subject"); 
        message.setText(text); 
               
        javax.mail.Transport transport = session.getTransport("smtp"); 
        transport.connect(host, Integer.parseInt(port), user, pass);
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close();
    }
}

It works fine as a standalone Java app, but not working with Apache openejb. When I commented out this dependency in Maven pom.xml, it works again :-)
       <dependency>
            <groupId>org.apache.openejb</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5.0-1</version>
            <scope>provided</scope>
        </dependency>

A related post is here: http://bit.ly/NSIoyx. The text is copied below:
----------------------------------------------------------------------------------

Strange javamail behaviour inside tomcat

In the series: 'weird problems you'd rather not spend your valuable time on' today I present a strange javamail/tomcat related problem and its solution.

While preparing the next version of our java web app I noticed that emails sent by our app were missing the mail subject. Moreover the message appeared to get sent as plain text instead of HTML so the message body was displaying ugly html. 

While debugging everything seemed OK and the javamail API (invoked via Spring) was invoked with the correct parameters and a non-null subject.

So then I wrote a jUnit test to further isolate the problem and of course the unit test, invoking the same server-side java code as before, worked like a charm: the subject was present and the message body was interpreted as HTML.

I was now faced with a configuration problem because the exact same code was working fine from a unit test but was failing when executing from within Tomcat. After some googling I found the advice to check the classpath for duplicate or conflicting javamail implementations. I use the very handy maven command:


mvn dependency:tree

which shows the full dependency tree of your referenced libraries including implicit references, i.e. a jar required for one of my own dependencies. Then I noticed that axis-2 uses a geronimo-javamail implementation; in addition to the 'standard' javax.mail javamail. Sure enough when I excluded this implicit dependency like so:
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.4.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-activation_1.1_spec</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-javamail_1.4_spec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

the mail got sent correctly.





2 comments:

  1. I just faced the same issue, your post save me some debugging time, thanks !

    ReplyDelete