MDB and mails: yes it works!

Message Driven Beans (MDBs) are often linked to JMS but actually there is no link at all!

Using ironjacamar you can listen on your mail box :).

To deploy it in tomee the setup is pretty easy: get ironjacamar rar(, add the api (jar with MailListener) and jboss logging in tomee libs (yes this is not that clean on this part, you can desire to deploy jboss logging + ironjacamar mail.jar in tomee and just a rar with the rar.xml as resource adapter) and then simply define the new MDB container:

  <Resource id="mail-ra" class-name="org.jboss.jca.adapters.mail.MailResourceAdapter" />
  <Container id="mail-mdb-container" type="MESSAGE">
    ResourceAdapter = mail-ra

or simply define the rar which will define the container automatically:

  <Deployments file="/path/to/ironjacamar.rar" />

Then in your project just implement MailListener:

public class MyListener implements MailListener {
    public void onMessage(final Message message) {
        try {
            System.out.println("Mail >>> " + message.getSubject());
        } catch (final MessagingException e) {

Then you need to configure it, here is a sample with gmail:

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "mailServer", propertyValue = ""),
    @ActivationConfigProperty(propertyName = "port", propertyValue = "995"),
    @ActivationConfigProperty(propertyName = "mailFolder", propertyValue = "INBOX"),
    @ActivationConfigProperty(propertyName = "storeProtocol", propertyValue = "pop3s"),
    @ActivationConfigProperty(propertyName = "userName", propertyValue="I don't want it here"),
    @ActivationConfigProperty(propertyName = "password", propertyValue="I don't want it here at all"),
    @ActivationConfigProperty(propertyName = "starttls", propertyValue="true"),
    @ActivationConfigProperty(propertyName = "pollingInterval", propertyValue="5000"),
    @ActivationConfigProperty(propertyName = "debug", propertyValue = "false"),
    @ActivationConfigProperty(propertyName = "flush", propertyValue = "false")
public class MyListener implements MailListener {
    // ....

Main issue is how to set address and password? We don’t want it in annotations for sure but in ejb-jar.xml neither since it will not be easily overridable.

For that TomEE/OpenEJB provides a nice feature: setting activation config properties as system properties.

Here you can set:


And it will be set as if it was in @ActivationConfigProperty :).

Side note: to use it with gmail (pop3s) javax.mail was not working well and geronimo-mail needed a little workaround (fixed on trunk). Here is the workaround: create a META-INF/javamail.providers with the line “protocol=pop3s; type=store;; vendor=Me”, MyPOP3SSLStore extends and overrides Folder getFolder(String) simply by delegating to super result excepted exist() which is ‘return “INBOX”.equals(name) || superFolder.exists();’.

This is really nice since it opens the door to a bunch of systems: all external systems like a FTP server, a messaging server like XMPP, a SSH server and so on can listen to new inputs through MDBs.

JCA is really the most unknown part of EE but it does worth looking into it since that’s where you can do what you want and then wiring it to CDI is just built-in since you can fire a CDI event from you MDB for instance!


4 thoughts on “MDB and mails: yes it works!

  1. Bart van Leeuwen


    Looks like a great idea, unfortunatly I couldn’t find any of the rar’s jar’s you refering to in your article.
    I’m curious if this would work for IMAP as well.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s