Jollobajano

Programming

MailSession for WildFly 8 and GMail

· 1 min read

Firstly, add the ourbound sockets for GMail SMTP and IMAP to standalone.xml:

    <socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
    ...
    <outbound-socket-binding name="mail-smtp">
    <remote-destination host="smtp.gmail.com" port="465"/>
    </outbound-socket-binding>
    <outbound-socket-binding name="mail-imap">
    <remote-destination host="imap.gmail.com" port="993"/>
    </outbound-socket-binding>
    <outbound-socket-binding name="mail-smtp-25000">
    <remote-destination host="localhost" port="25000"/>
    </outbound-socket-binding>
    ...
    </socket-binding-group>
    </server>

Then add the mail-sessionelement:

        <subsystem xmlns="urn:jboss:domain:mail:2.0">
            <mail-session name="gmail" jndi-name="java:jboss/mail/Gmail">
                <smtp-server outbound-socket-binding-ref="mail-smtp"
                 ssl="true" username="${gmail.username}"
                 password="${gmail.password}"/> 
                <custom-server name="imap"
                   outbound-socket-binding-ref="mail-imap"
                   ssl="true" username="${gmail.username}"
                   password="${gmail.password}">
                    <property name="connectiontimeout" value="30000"/>
                    <property name="timeout" value="30000"/>
                </custom-server>
            </mail-session>
        </subsystem>

Restart.

Now, well try the connection, using the following servlet:

package jollobajano.sendmail;
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 SendmailServlet extends HttpServlet
{
    @Resource(mappedName="java:jboss/mail/Gmail")
    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("AN EMAIL ADDRESS");
                Address[] to = new InternetAddress[] {new InternetAddress("ANOTHER EMAIL ADDRESS") };

                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; charset=utf-8");
                Transport.send(m);
                out.println("Mail sent!");
            }
            catch (javax.mail.MessagingException e)
            {
                e.printStackTrace();
                out.println("Error in Sending Mail: "+e);
            }
        }
    }
}