In this simple tutorial we will show how to use the Java Mail service from JBoss AS, either connecting to a local SMTP server or using Google SMTP server.
The JBoss Mail subsystem configures a JavaMail session binding into JNDI. Let's start at first with JBoss AS 7 configuration. Open your AS 7 configuration file (standalone.xml/domain.xml) and find the following line:
The Session is bound to the following host and port:
Adding support for User Authentication:
As most of mail servers requires user authentication we will at first add login information by adding the <login> element:
A more complex configuration
Here's a more complex configuration which sets some more properties, such as the from field of the mail, a pop3-server authenticated socket connection, and e remote mail server destination.
Now let's configure a mail server which will deliver our mails. 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:
Now let's create a simple Servlet which uses the Java Mail session to send a mail:
As you can see this Servlet injects the Java Mail Session using the Java EE @Resource annotation:
private Session mailSession;
Fairly simple. Now test your servlet before moving to the next section.
Sending mail to a Remote GMail server
You can slighty change your configuration to use your GMail server account. The only tricky part of it is that it requires enabling the TLS/SSL transport, otherwise the GMail server will refuse to accept your connection.
In earlier JBoss AS version, you needed to set the mail.smtp.starttls.enable to true, in order to enable secure connection.
When using JBoss AS 7, you need to set the ssl property of the smtp server element. Here's a sample configuration:
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:
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:
Adding support for SSL/TSL:
Finally, if you want to enable SSL/TSL transport (as for GMail accounts) just add the following MBean configuration:<