Setup a local smtp server for JBoss

In my Seam application I needed a local smtp server for testing the email facilities Seam offers. After some research on the internet I came up with a configuration which uses Ubuntu’s Mail Transfer Agent postfix with Gmail as the so called smarthost. Postfix will act as an intermediate smtp server which will dispatch the mails it receives to Gmail‘s smtp server.
This blog will summarize the steps needed for this setup.

Postfix installation and configuration

First, let’s install postfix.

sudo apt-get install postfix

Next, add additional configuration.

sudo dpkg-reconfigure postfix

Choose the following settings. For settings not listed below, you can keep the default settings.

  • General type of mail configuration: Internet with smarthost
  • SMTP relay host: smtp.gmail.com:587
  • Internet protocols to use: all

Next add settings necessary for authentication against the Gmail smtp server.

sudo postconf -e 'smtp_use_tls = yes'
sudo postconf -e 'smtp_sasl_auth_enable = yes'
sudo postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd'
sudo postconf -e 'smtp_sasl_security_options = noanonymous'
sudo postconf -e 'smtp_sasl_tls_security_options = noanonymous'

Next add the password file /etc/postfix/sasl_passwd containing the username and password of your Gmail account.

smtp.gmail.com username@gmail.com:PASSWORD

Now, hash the password file (this will create the hashed sasl_passwd.db file), throw away the original file and restart the postfix daemon.

sudo postmap /etc/postfix/sasl_passwd
sudo rm /etc/postfix/sasl_passwd
sudo /etc/init.d/postfix restart

Testing postfix

You can now test the postfix configuration via telnet.

telnet localhost 25
>mail from:noreply@gmail.com
>rcpt to:username@gmail.com
>data
>Testmail
>.
>quit

This should send a small email to username@gmail.com.

Troubleshooting

If the mail didn’t arrive, you can check a couple of things:
Check if it’s in the postfix queue:

sudo mailq

Flush the queue:

sudo postfix -f

Check the log file:

sudo tail /var/log/mail.log

JBoss configuration

Next we’re gonna add the mail server configuration to JBoss, so it van be accessed via JNDI. Edit the file $JBOSS_HOME/server/default/deploy/mail-service.xml.

<?xml version="1.0" encoding="UTF-8"?>
<server>
  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="Configuration">
      <configuration>
        <!-- Change to your mail server prototocol -->
        <property name="mail.transport.protocol" value="smtp"/>
        <!-- Change to the SMTP gateway server -->
        <property name="mail.smtp.host" value="localhost"/>
        <!-- The mail server port -->
        <property name="mail.smtp.port" value="25"/>
        <!-- Change to the address mail will be from  -->
        <property name="mail.from" value="noreply@gmail.com"/>
        <!-- Enable debugging output from the javamail classes -->
        <property name="mail.debug" value="false"/>
      </configuration>
    </attribute>
    <depends>jboss:service=Naming</depends>
  </mbean>
</server>

Seam configuration

Finally we need to configure seam so it can communicate with our new smtp server. This is done in the component descriptor components.xml.

<?xml version="1.0" encoding="UTF-8"?>
<components
  xmlns="http://jboss.com/products/seam/components"
  xmlns:mail="http://jboss.com/products/seam/mail"
  xsi:schemaLocation="http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
    http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
  ....
  <mail:mail-session session-jndi-name="java:/Mail" />
  ....
</components>

If you’re also using jBPM for business processes within your seam application and need email facilities, you can configure the smtp server in the jBPM configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<jbpm-configuration>
  ....
  <string name="jbpm.mail.smtp.host" value="localhost"/>
  <string name="jbpm.mail.from.address" value="noreply@open18.org"/>
</jbpm-configuration>
%d bloggers like this: