Labels

Wednesday, July 4, 2012

How to configur JBoss mail service ?

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:



<subsystem xmlns="urn:jboss:domain:mail:1.0">
    <mail-session jndi-name="java:jboss/mail/Default">
          <smtp-server outbound-socket-binding-ref="mail-smtp"/>
    </mail-session>
</subsystem>
 
 
The Session is bound to the following host and port:
 
 
 
<outbound-socket-binding name="mail-smtp">
   <remote-destination host="localhost" port="25"/>
</outbound-socket-binding>
 
 
 

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:




<subsystem xmlns="urn:jboss:domain:mail:1.0">
    <mail-session jndi-name="java:jboss/mail/Default">
         <smtp-server outbound-socket-binding-ref="mail-smtp">
              <login name="fmarchioni" password="fmarchioni"/>
        </smtp-server>
    </mail-session>
</subsystem>
 
 
 
 
 
 
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:
jboss mail service jboss mail service
Now let's create a simple Servlet which uses the Java Mail session to send a mail:




package com.sample;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.annotation.Resource;

@WebServlet(value="/mail")
public class SendMail 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("senderemailaddress.com");
                Address[] to = new InternetAddress[] {new InternetAddress("receiveremailaddress.com") };

                m.setFrom(from);
                m.setRecipients(Message.RecipientType.TO, to);
                m.setSubject("JBoss AS 7 Mail");
                m.setSentDate(new java.util.Date());
                m.setContent("Mail sent from JBoss AS 7","text/plain");
                Transport.send(m);
                out.println("Mail sent!");
            }
            catch (javax.mail.MessagingException e)
            {
                e.printStackTrace();
                out.println("Error in Sending Mail: "+e);
            }
        }
    }
}
 
As you can see this Servlet injects the Java Mail Session using the Java EE @Resource annotation:
@Resource(mappedName="java:jboss/mail/Default")
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:


<mail-session jndi-name="java:jboss/mail/Default">
    <smtp-server ssl="true" outbound-socket-binding-ref="mail-smtp">
                 <login name="yourusergmail.com" password="password"/>
    </smtp-server>
</mail-session>
. . . .
<outbound-socket-binding name="mail-smtp">
    <remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding> 
 
 

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>
 
 
 
 

 

 

 

 

 

 

No comments:

Post a Comment