Wednesday, December 31, 2014

Configure JNDI Mail Session with WebLogic and Spring, Gmail

In this tutorial I will explain how to configure a JNDI mail session using Oracle WebLogic 12c and Spring Framework and how to send email from a Java web application. In this example we will use GMail as smtp service.

WebLogic configuration:

First of all we have to configure a new JNDI Mail Session in WebLogic.
To do this, log in into your weblogic console, i.e:


then go under Services -> Mail Sessions and configure a mail session, in the following way (change names and parameters as you need):

Name: mail/mymailsession
JNDI name: mail/mymailsession
Password:  [your gmail password]
JavaMail Properties:
mail.smtp.password=[your gmail password]

Don't forget to select a target server at the end of the procedure, in order to make the session available.

Maven configuration

You have to import the Spring framework java mail libraries, adding them to your build path or to your pom.xml in case you have a maven project.

If you have not a maven project you have to download the jar:  spring-context-support-4.1.1.RELEASE.jar, in order to make available to java the class org.springframework.mail.javamail.JavaMailSenderImpl

Spring configuration

Open you servlet-context.xml (you could have another name in your Spring app) and add:
<jee:jndi-lookup id="mailSession" jndi-name="mail/mymailsession" resource-ref="true"/>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
     <property name="session" ref="mailSession"/>

<bean id="mailutils" class="com.mytroubleshootings.utils.Mail"></bean>

The first line creates an object for looking up the created session trough JNDI.
Remember to add the correct jee namespace to your beans main tag
or modify the <jee:jndi-lookup> tag in order to match the correct namespace if you alredy specified a different namespace for, otherwise it will not find it and it will not work!

The second tag creates a bean for a MailServer implementation, that we can user in order to send email, specifing that the javax.mail.Session that the bean object will use is the one we looked up before in the first line, configured in WebLogic.
The last line is just the initialization of a bean util we can create to send our email, it's just a choice, you could choose another way to do this.

Java Utils Class

Here is the Java utils class you could use to send a generic email, it's just an example.

package com.mytroubleshootings.utils;

import javax.ejb.Stateless;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class Mail {
    private MailSender mailSender;

     * This method will send compose and send the message
     * */
    public void sendMail(String to, String subject, String body)
        SimpleMailMessage message = new SimpleMailMessage();

Call example from a Java method

This is how to make a simple call to the utils class to send our email:

public class TestMail {

     Mail mail;

     public void doTestMail() {
          mail.sendMail("", "My Subject", "My Body Message");

No comments:

Post a Comment

(c) Copyright 2020 -