Sending E-mail Using Google SMTP and JBoss AS 7

If you don’t have your own SMTP server, you can use Google’s in a pinch. This tutorial will show you how to do that using JBoss AS 7.

First, we will need to configure a mail session. Stop JBoss if it is running. Open JBOSS/standalone/configuration/standalone.xml. Search for smtp. You will see a sample mail session defined like this:

<mail-session jndi-name="java:jboss/mail/Default">
  <smtp-server outbound-socket-binding-ref="mail-smtp"/>

Change it like this. I have highlighted the relevant changes.

<mail-session jndi-name="java:jboss/mail/Default">
 <smtp-server ssl="true" outbound-socket-binding-ref="mail-smtp">
  <login name="" password="your_google_password"/>

Basically, you are enabling SSL and entering your Google account information.

Keep searching for smtp and you will see how the SMTP server address is defined.

<outbound-socket-binding name="mail-smtp">
 <remote-destination host="localhost" port="25"/>

Change it as follows.

<outbound-socket-binding name="mail-smtp">
 <remote-destination host="" port="465"/>

Save changes. Start the server.

Write a small Servlet to test things out. Of course, use valid e-mail addresses.

import javax.annotation.Resource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MailTest extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Resource(lookup = "java:jboss/mail/Default")
    private Session mailSession;

    public void service(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        PrintWriter out = res.getWriter();
        try {
            MimeMessage m = new MimeMessage(mailSession);
            Address from = new InternetAddress("");
            Address[] to = new InternetAddress[] { new InternetAddress(
                "") };
            m.setRecipients(Message.RecipientType.TO, to);
            m.setSubject("Google JavaMail Test");
            m.setContent("Test from inside JBoss AS7 Server", "text/plain");

            out.println("Mail Sent Successfully.");
        } catch (javax.mail.MessagingException e) {

Note, Google will ignore the from address set in the code and always use your account as the from field.

One thought on “Sending E-mail Using Google SMTP and JBoss AS 7

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 )

Google+ photo

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

Connecting to %s