Sending e-mail Using Gmail SMTP Server and TomEE 1.5.2

In a previous post I showed how to send e-mail using Gmail’s SMTP server from JBoss. In this post I will show how to do the same using TomEE 1.5.2.

Open conf/tomee.xml from the TomEE’s installation folder. Add these lines to define the JavaMail resource.

<Resource id="mail/Default" type="javax.mail.Session">
mail.transport.protocol=smtp 
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory 
mail.smtp.socketFactory.fallback=false 
mail.smtp.host=smtp.gmail.com 
mail.smtp.port=465 
mail.smtp.auth=true 
mail.smtp.user=YOUR_GMAIL_USER_ID@gmail.com
password=YOUR_GMAIL_PASSWORD
</Resource>

Note: The password is set using the “password” property and not “mail.smtp.password” property.

From within your Java code, inject the resource by it’s id.

@Stateless
public class MailManager {
    @Resource(name = "mail/Default")
    private Session mailSession;

    ...
}

That’s it! One thing to note. If you are running a ¬†virus checker in your development machine, you might get an error. The exception will be:¬†PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. This is true, even if you are running TomEE in a virtual machine and the virus checker is running in the host.

2 thoughts on “Sending e-mail Using Gmail SMTP Server and TomEE 1.5.2

  1. Is this really work for you?
    If you append “mail.debug=true” to this resource definition you will see in the logs javamail connects to localhost:25 and use smtp transport.

    The working resource definition:

    mail.transport.protocol=smtp
    mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    mail.smtp.socketFactory.fallback=false
    mail.smtp.host=smtp.gmail.com
    mail.smtp.port=465
    mail.smtp.auth=true
    mail.smtp.user=YOUR_GMAIL_USER_ID@gmail.com
    mail.from=YOUR_GMAIL_USER_ID@gmail.com
    password=YOUR_GMAIL_PASSWORD

    • I have changed my post with your suggested changes which I have successfully tested. My old blog post worked fine. Then something changed either in TomEE or JDK and it no longer worked. I have code still in production that uses the old approach successfully.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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