Monday, June 30, 2014

WildFly 8 : Sending Email using Gmail smtp server


Sending email is usually used as messaging tool to notify events occurred in corporate systems. JBoss WildFly 8 as Java EE implementer provides mechanism to send email by configuring remote email server information then get email session from JNDI.

This blog presents steps of configuring JNDI email session using Gmail as smtp server.

The convenient way to setup WildFly is by using web console.
Start WildFly application server, open web browser and go to http://localhost:9990 (Assume that you have already set authentication data for the web console)

After logged in, scroll down to select Socket Binding under General Configuration tab where we configure Gmail smtp server information.


Click View link to see details of standard-sockets information.


Select Outbound Remote tab then edit email server information.

Host : smtp.gmail.com
Port : 465

Remember that the name of Socket Binding is mail-smtp, we will use this name later to configure mail server.

Click Mail under Connector subsystems.


Click Add button to add new JNDI name for email session. Give the following information.
JNDI Name : java:jboss/mail/Gmail
Default From : (your gmail email address)

Click View link of added JNDI Name.


Add new Mail Server with the following information.

Socket Binding : mail-smtp
Type : smtp
UserName : your gmail email address
Password : your email password
Use SSL? : checked

Reload server configuration.



Now we have done configuration part, next we write the code to apply this JNDI email session.

Example EJB code to send email using configured JNDI email session.

import javax.annotation.Resource;
import javax.ejb.Asynchronous;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
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;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Session Bean implementation class GwMessage
 */
@Stateless
@LocalBean
public class GwMessage {
 private static final Logger log = LoggerFactory.getLogger(GwMessage.class);
 
 @Resource(mappedName="java:jboss/mail/Gmail")
 Session gmailSession;
 
 /**
     * Default constructor. 
     */
    public GwMessage() {
    }
    
    @Asynchronous
    public void sendEmail(String to, String from, String subject, String content) {
     
     log.info("Sending Email from " + from + " to " + to + " : " + subject);
     
  try {
 
   Message message = new MimeMessage(gmailSession);
   message.setFrom(new InternetAddress(from));
   message.setRecipients(Message.RecipientType.TO,
    InternetAddress.parse(to));
   message.setSubject(subject);
   message.setText(content);
 
   Transport.send(message);
 
   log.debug("Email was sent");
 
  } catch (MessagingException e) {
   log.error("Error while sending email : " + e.getMessage());
  }
    }

}

Description
Using resource injection to get email session.

@Resource(mappedName="java:jboss/mail/Gmail")
Session gmailSession;


Sending email is usually take time, @Asynchronous  help client not to wait until sending email has done, it just call and then go on immediately.