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:

http://localhost:7001/console

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
Username: yourmail@gmail.com
Password:  [your gmail password]
JavaMail Properties:
mail.smtp.starttls.enable=true
mail.smtp.port=587
mail.user=yourmail
mail.smtp.ssl.trust=smtp.gmail.com
mail.smtp.auth=true
mail.smtp.host=smtp.gmail.com
mail.smtp.password=[your gmail password]
mail.debug=True
username=yourmail@gmail.com
mail.from=yourmail@gmail.com
mail.smtp.user=yourmail@gmail.com
mail.transport.protocol=smtp

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.

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-support</artifactId>
 <version>4.1.1.RELEASE</version>
</dependency>  
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>

<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
xmlns:jee="http://www.springframework.org/schema/jee"
or modify the <jee:jndi-lookup> tag in order to match the correct namespace if you alredy specified a different namespace for http://www.springframework.org/schema/jee, 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;

@Stateless
public class Mail {
    
     @Autowired
    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();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        mailSender.send(message);
    }    
}

Call example from a Java method

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

@Stateless
public class TestMail {

     @EJB
     Mail mail;

     public void doTestMail() {
          mail.sendMail("redpelle@gmail.com", "My Subject", "My Body Message");
     }
}

No comments:

Post a Comment

(c) Copyright 2020 - MyTroubleshooting.com