I’ve just joined a project as a freelancer to customize a commercial e-commerce solution (so let called it EP). OOTB, the email sending of EP does not support authentication, which is a weird thing for such a cool product like that. Actually, EP does declare an JNDI mail session in the context, but in the code they don’t use it. With the source code of EP in hand, things get much easier. First, we declare a JNDI resource in our application context (either in your context file in the conf/Catalina/localhost/ or in your context.xml under META-INF inside your war):

    <Resource name="mail/Session" 
              mail.smtp.host= "xxx.xxx.xxx.xxx"
              mail.smtp.auth= "true"
              mail.smtp.port= "25"
              description="Global E-Mail Resource">

And put those in your web.xml file:

    <description>Email Session</description>

And now, in your Java code, you will use this email session like this:

    Context initContext = new InitialContext();
    Context context = (Context) initContext.lookup("java:comp/env");
    Session mailSession = (Session) context.lookup("mail/Session");
    //Your code to send the email goes here

If you get a java.lang.ClassCastException, don’t worry, this is a known issue. It’s because you have multiple Java Mail jar file and Java Activation Framework jar file in your CLASSPATH!

Hope that may help!