Learn to configure mail resource in Wildfly with a sample java client

This tutorial assumes that you have the following installed on your local system: JBoss 7.2 eclipse to run a project a mail server, can...

This tutorial assumes that you have the following installed on your local system:

  1. JBoss 7.2
  2. eclipse to run a project
  3. a mail server, can be gmail
In this page we will summarize how to configure a mail dataSource in Jboss and create a JavaEE6 client to send a sample email.

Configure Mail datasource in JBoss 7.2

  1. Open standalone.xml
  2. Search for the mail subsystem
    <subsystem xmlns="urn:jboss:domain:mail:1.1"></subsystem>
  3. Define a mail session
    <mail-session jndi-name="java:/czetsuyaMail">
     <smtp-server outbound-socket-binding-ref="mail-smtp">
      <login name="" password="secret"/>
  4. Define an outbound socket we will create 2: smtp with port 587 (eg zimbra) and ssl with port 465 (for gmail). Search for socket-binding-group section and add the following lines: for Zimbra
    <outbound-socket-binding name="mail-smtp">
     <remote-destination host="" port="587"/>
    OR for gmail
    <outbound-socket-binding name="mail-smtp-gmail">
     <remote-destination host="" port="465"/>
    In greater version of JBoss now Wildfly, username and password must now be specified in the smtp-server tag:
    <smtp-server outbound-socket-binding-ref="mail-smtp-gmail" username="" password="secret">

Sample Client
package org.czetsuya.test;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Asynchronous;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailService {
 @Resource(lookup = "java:/czetsuyaMail")
 private Session mailSession;

 private void init() throws AddressException, MessagingException {

 private void sendAsyncMessage(String htmlMessage) throws AddressException,
   MessagingException {

  MimeMessage msg = new MimeMessage(mailSession);
  msg.setFrom(new InternetAddress(""));
  msg.setSentDate(new Date());
  msg.setContent(htmlMessage, "text/html");

  msg.setRecipient(RecipientType.TO, new InternetAddress(
  InternetAddress[] replytoAddress = { new InternetAddress(
    "") };


You can actually test with telnet if your port is open by executing this command in command prompt:
telnet server port



wildfly 1422908331514827490

Post a Comment Default Comments