Mail session configuration on WildFly

In this simple tutorial we will show how to use the Java Mail service from WildFly – JBoss EAP, either connecting to a local SMTP server or using Google SMTP server.

Let’s start configuring JBoss Mail service on WildFly, then we will see how to configure the service on older application server versions.

WildFly Mail service configuration

WildFly mail subsystem is contained in all server configurations and exposes a Mail service bound at the JNDI name “java:jboss/mail/Default”:

<subsystem xmlns="urn:jboss:domain:mail:2.0">
   <mail-session name="default" jndi-name="java:jboss/mail/Default">
        <smtp-server outbound-socket-binding-ref="mail-smtp" />
   </mail-session>
</subsystem>

The Mail Session in turn references an smtp host bound at localhost at port 25:

<outbound-socket-binding name="mail-smtp">
   <remote-destination host="localhost" port="25"/>
</outbound-socket-binding>

In order to configure the connection towards a pop/smtp server we need to set up username and password in the mail-session. As most servers require it, we will enable tls. The following CLI scripts can be used to connect to GMail SMTP server using an example account (adjust user and password accordingly):

/subsystem=mail/mail-session=default/server=smtp/:write-attribute(name=username,value=myuser@gmail.com)
/subsystem=mail/mail-session=default/server=smtp/:write-attribute(name=password,value=mypassword)
/subsystem=mail/mail-session=default/server=smtp/:write-attribute(name=tls,value=true)

Done with the mail session configuration, we will now set the outbound sockets, by setting host and port to GMail’s defaults:

/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=mail-smtp/:write-attribute(name=host,value=smtp.gmail.com)
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=mail-smtp/:write-attribute(name=port,value=587)

Here is the updated configuration for the mail subsystem and the outbound socketbinding:

<subsystem xmlns="urn:jboss:domain:mail:4.0">
    <mail-session name="default" jndi-name="java:jboss/mail/Default">
        <smtp-server outbound-socket-binding-ref="mail-smtp" tls="true" username="myuser@gmail.com" password="mypassword"/>
    </mail-session>
</subsystem>

<!-- . . . . . . -->

<outbound-socket-binding name="mail-smtp">
    <remote-destination host="smtp.gmail.com" port="587"/>
</outbound-socket-binding>

Coding a Mail Servlet

To test our configuration, we will create a Servlet which sends a message using a mail session injected as Resource:

@WebServlet(value="/mail")
public class MailServlet extends HttpServlet
{
    @Resource(mappedName="java:jboss/mail/Default")
    private Session mailSession;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        {

            PrintWriter out=response.getWriter();
            try    {
                MimeMessage m = new MimeMessage(mailSession);
                Address from = new InternetAddress("test@mastertheboss.com");
                Address[] to = new InternetAddress[] {new InternetAddress("user@gmail.com") };

                m.setFrom(from);
                m.setRecipients(Message.RecipientType.TO, to);
                m.setSubject(WildFly Mail");
                m.setSentDate(new java.util.Date());
                m.setContent("Mail sent from WildFly","text/plain");
                Transport.send(m);
                out.println("Mail sent!");
            }
            catch (javax.mail.MessagingException e)
            {
                e.printStackTrace();
                out.println("Error in Sending Mail: "+e);
            }
        }
    }
}

If you are using a Gmail SMTP server, you will stumble upon the following error:

javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted.

The reason for that is that you need to generate an application specific password instead of your original password.

jboss mail configuration

You can generate one at the link https://myaccount.google.com/apppasswords and use the generated application specific password in place your original password.

Update your smtp configuration with the App Password:

/subsystem=mail/mail-session=default/server=smtp/:write-attribute(name=password,value=apppassword)

You can find the source code for this tutorial here: https://github.com/fmarchioni/mastertheboss/tree/master/mail/basic

Using a Java Mail Server

If you want to experiment locally with a Java server, you can try Apache James which is an opensource mail server, pretty light and simple to get started with it. Download it. Next unzip it and launch the run script which will start the mail server.

At startup James server just contains an administrative root account, so we will use this to add an user account. Unfortunately James does not provide (as far as I know!) a GUI interface to add users, to do that, telnet to localhost on port 4555 with the command:

telnet localhost 4555

You can log in with the root user name and password. After the login, we’ll add an user. The adduser command expects a username and password combination. After adding users, you can use the listusers command to verify the entries and then exit the remote manager by typing quit. The whole session should look like this:
jboss mail service jboss mail service

JBoss AS 5/6 Mail configuration

In earlier releases of JBoss application server, you can configure the Java Mail service by dropping a mail-service.xml into the deploy folder. Here’s a sample configuration which uses an unauthenticated session:

<server>
  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    ...
    <attribute name="Configuration">
      <configuration>
        ...
        <property name="mail.smtp.host" value="localhost"/>
        <property name="mail.smtp.port" value="25"/>
        ...
      </configuration>
    </attribute>
    ...
  </mbean>
</server>

Adding support for user authentication:

You can follow the procedure we have exposed for AS 7 to create an user on your James server: the configuration would change to:

<server>
  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    ...
       <attribute name="User">fmarchioni</attribute>
        <attribute name="Password">fmarchioni</attribute>   

        <attribute name="JNDIName">java:/Mail</attribute>
        <attribute name="Configuration">
      <configuration>
        ...

        <property name="mail.smtp.host" value="localhost"/>
         <property name="mail.smtp.port" value="25"/>

        <property name="mail.smtp.auth" value="true"/>
        <property name="mail.smtp.user" value="fmarchioni"/>
        ...
      </configuration>
    </attribute>
    ...
  </mbean>
</server>

Adding support for SSL/TSL:

Finally, if you want to enable SSL/TSL transport (as for GMail accounts) just add the following MBean configuration:

<server>
   <mbean code="org.jboss.mail.MailService"
          name="jboss:service=Mail">
     ...
     <attribute name="User">fmarchioni</attribute>
     <attribute name="Password">fmarchioni</attribute>

    <attribute name="JNDIName">java:/Mail</attribute>
     <attribute name="Configuration">
       <configuration>
         ...

        <property name="mail.smtp.host" value="smtp.gmail.com"/>

        <property name="mail.smtp.auth" value="true"/>
        <property name="mail.smtp.port" value="465"/>
        <property name="mail.smtp.starttls.enable" value="true"/>
        <property name="mail.smtp.socketFactory.class" value="javax.net.ssl.SSLSocketFactory"/>
        ...
      </configuration>
    </attribute>
    ...
  </mbean>
</server>