Version 10

    An MDB (Message Driven Bean) that accepts a javax.mail.Message

     

    JBoss AS provides mail-ra.rar (not bundled in 4.2.x, link needed for where to get it), which is JCA connector for receiving email using the JavaMail API.  To use it, place mail-ra.rar in your deploy folder and then, depending on whether you are using JEE5 or J2EE follow the instructions below for how to implement and wire up your MDB to receive emails.

     

    Full list of activation config properties for mail-ra.rar

     

    • mailServer (e.g. localhost)

    • port (port on which mailServer is listening)

    • mailFolder (e.g. INBOX)

    • storeProtocol (imap/pop3)

    • userName

    • password

    • pollingInterval (interval at which to poll the mailbox)

    • maxMessages (not used)

    • debug (enable JavaMail debugging)

    • flush (for pop3 flush the mailbox after checking)

    • messageSelector (not used)

     

     

    Using EJB3 / JEE 5

     

    /*
     * JBoss, the OpenSource J2EE webOS
     * 
     * Distributable under LGPL license.
     * See terms of license at gnu.org.
     */
    package org.jboss.test.jca.inflowmdb;
    
    import javax.ejb.ActivationConfigProperty;
    import javax.ejb.MessageDriven;
    import javax.mail.Message;
    
    import org.jboss.annotation.ejb.ResourceAdapter;
    import org.jboss.resource.adapter.mail.inflow.MailListener;
    import org.jboss.logging.Logger;
    
    /**
     * A JavaMail based MDB for EJB3 use
     */
    @MessageDriven(activationConfig={
       @ActivationConfigProperty(propertyName="mailServer", propertyValue="mailHost"),
       @ActivationConfigProperty(propertyName="mailFolder", propertyValue="INBOX"),
       @ActivationConfigProperty(propertyName="storeProtocol", propertyValue="imap"),
       @ActivationConfigProperty(propertyName="userName", propertyValue=""),
       @ActivationConfigProperty(propertyName="password", propertyValue="seam")
    })
    @ResourceAdapter("mail-ra.rar")
    public class EJB3TestJavaMailMDB implements MailListener
    {
       private static Logger log = Logger.getLogger(EJB3TestJavaMailMDB.class);
    
       public void onMessage(Message msg)
       {
          log.info("onMessage, msg="+msg);
       }
    }
    

     

    Using J2EE

     

    /*
     * JBoss, the OpenSource J2EE webOS
     * 
     * Distributable under LGPL license.
     * See terms of license at gnu.org.
     */
    package org.jboss.test.jca.inflowmdb;
    
    import javax.ejb.MessageDrivenBean;
    import javax.ejb.MessageDrivenContext;
    import javax.mail.Message;
    
    import org.jboss.resource.adapter.mail.inflow.MailListener;
    import org.jboss.logging.Logger;
    
    /**
     * A JavaMail based MDB.
     * 
     * @author Scott.stark@jboss.org
     * @version $Revision: 1.1 $
     */
    public class TestJavaMailMDB
       implements MessageDrivenBean, MailListener
    {
       private static Logger log = Logger.getLogger(TestJavaMailMDB.class);
    
       private MessageDrivenContext ctx;
    
       public void ejbCreate()
       {
          log.info("ejbCreate");
       }
       
       public void ejbRemove()
       {
          log.info("ejbRemove");
       }
    
       public void setMessageDrivenContext(MessageDrivenContext ctx)
       {
          this.ctx = ctx;
       }
    
       public void onMessage(Message msg)
       {
          log.info("onMessage, msg="+msg);
       }
    }
    

     

    ejb-jar.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
             http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
             version="2.1">
    
       <enterprise-beans>
    
          <message-driven>
             <description>An MDB that accepts mail messages</description>
             <ejb-name>MailMDB</ejb-name>
             <ejb-class>org.jboss.test.jca.inflowmdb.TestJavaMailMDB</ejb-class>
             <messaging-type>org.jboss.resource.adapter.mail.inflow.MailListener</messaging-type>
             <activation-config>
                <activation-config-property>
                   <activation-config-property-name>mailServer</activation-config-property-name>
                   <activation-config-property-value>${mailhost:mailhost}</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                   <activation-config-property-name>mailFolder</activation-config-property-name>
                   <activation-config-property-value>INBOX</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                   <activation-config-property-name>storeProtocol</activation-config-property-name>
                   <activation-config-property-value>imap</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                   <activation-config-property-name>userName</activation-config-property-name>
                   <activation-config-property-value>jduke</activation-config-property-value>
                </activation-config-property>
                <activation-config-property>
                   <activation-config-property-name>password</activation-config-property-name>
                   <activation-config-property-value>theduke</activation-config-property-value>
                </activation-config-property>
            </activation-config>
            <transaction-type>Container</transaction-type>
          </message-driven>
       </enterprise-beans>
    
       <assembly-descriptor>
    
          <container-transaction>
             <method>
                <ejb-name>MailMDB</ejb-name>
                <method-name>*</method-name>
             </method>
             <trans-attribute>Required</trans-attribute>
          </container-transaction>
    
       </assembly-descriptor>
    
    </ejb-jar>
    
    

     

    jboss.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE jboss PUBLIC
       "-//JBoss//DTD JBOSS 4.0//EN"
       "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
    
    <jboss>
       <enterprise-beans>
          <message-driven>
             <ejb-name>MailMDB</ejb-name>
             <resource-adapter-name>mail-ra.rar</resource-adapter-name>
          </message-driven>
       </enterprise-beans>
    </jboss>
    

     

    Related

    JavaMail