How to Configure the Email Notifier with Payara Server 171 & Gmail

Uncategorized

The latest version of Payara Server extends the Notification Service with many ways how to process notifications and send them to external services.  Sending notifications as email messages to an SMTP server is now provided out of the box. It is even possible to use an account on a public email server, such as Gmail. In this guest blog, Alessio Gennari – a member of the Payara open source community – explains to you exactly how it can be done.

 

{{cta(‘397f0696-0d00-4949-b21a-72e4dfbd321d’)}}

 

To successfully configure a JavaMail Session in Payara Server 171 with your Gmail account, first you need to modify the following settings for a JavaMail session in Payara Server:

  • set JNDI Name with a name that will be used to refer to it from the Notification Service
  • set Mail Host to smtp.gmail.com
  • set Default User with your gmail address
  • set Default Sender Address with any email address you prefer

Now you need to set some additional properties of the JavaMail Session to enable Payara Server to send emails through your Gmail account. Here are the properties you have to set:

  • mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
  • mail.smtp.socketFactory.port=465
  • mail.smtp.port=465
  • mail.smtp.auth=true
  • mail.smtp.password=<gmail account password>
  • mail.smtp.user=<gmail account email address>

You can find the correct settings in the following figure:

 

payara Server gmail integration.png

 

All other settings can be left at default values.

 

Now you can enable email notifications using the JNDI name of the JavaMail session, and Payara Server can send notifications via your Gmail account. 

 

{{cta(‘f53ec10a-12d5-483b-b089-2d4cdab10448’)}}

 

 

Comments (8)

Post a comment

Your email address will not be published. Required fields are marked *

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

  1. Martin Charlesworth

    Thanks Alessio, is it possible to use this to send emails from an application?

    1. Ondro Mihalyi

      Hi Martin,

      The Notification service is used to route events from the Payara container not from applications. The email notifier uses a standard JavaEE JavaMail session, which can also be used from an application to send emails.

      Here’s an example of such an application: https://github.com/javaee-samples/javaee7-samples/blob/master/javamail/definition/src/main/java/org/javaee7/javamail/definition/AnnotatedEmailServlet.java

      To use the JavaMail session configuration described in the blog, just use its JNDI name in @Resource when you inject javax.mail.Session. If you do so, you don’t need to define a JavaMail resource in the application using @MailSessionDefinition.

  2. Sam Samualson

    Hi, please help,
    How it must be configured for payra micro?

    1. Ondro Mihalyi

      Hi,

      You can configure JavaMail session definition and also Notification Service and the JavaMail notifier in Payara Micro with asadmin commands (the same which work with Payara Server) in a file using the –postbootcommandfile argument (documentation here: https://docs.payara.fish/documentation/payara-micro/asadmin/pre-and-post-boot-scripts.html)

      You need to configure the JavaMail session with these asadmin commands:

      create-javamail-resource

      create-resource-ref

      set resources.mail-resource.mail/myMailSession.property.mail.smtp.* (instead of * you need to pass each property separately).

      For example, this would configure the same JavaMail session as in the article above (note that dots after property. have to be escaped with ):

      create-javamail-resource –debug=false –storeProtocol=imap –auth=false –transportProtocol=smtp –host=smtp.gmail.com –storeProtocolClass=com.sun.mail.imap.IMAPStore –from=default-sender@email.com –transportProtocolClass=com.sun.mail.smtp.SMTPTransport –user=default@email.com –enabled=true –target=domain mail/myMailSession
      create-resource-ref –enabled=true –target=server mail/myMailSession
      set resources.mail-resource.mail/myMailSession.property.mail.smtp.user=myemail@gmail.com resources.mail-resource.mail/myMailSession.property.mail.smtp.password=${ALIAS=mygmail_pwd} resources.mail-resource.mail/myMailSession.property.mail.smtp.auth=true resources.mail-resource.mail/myMailSession.property.mail.smtp.port=465 resources.mail-resource.mail/myMailSession.property.mail.smtp.socketFactory.port=465 resources.mail-resource.mail/myMailSession.property.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

  3. Sam Samualson

    Hi,
    please help! How it must be configured for payara micro?

  4. Stanyslas Matayo

    How can we do it with Docker? Is it possible?

    1. Ondro Mihalyi

      Hi Stanyslas,

      Everything is possible in Docker but the best practice is to configure everything using asadmin commands in the Dockerfile. You can execute asadmin commands either during build time (during the build, you need to start the server, execute the commands and then stop the server). You can execute the same commands also upon server startup each time the container is started, with the –prebootcommandfile argument (https://docs.payara.fish/documentation/payara-server/app-deployment/app-deployment.html), which you can add to the end of the docker run command.

      An example of asadmin commands that configure a JavaMail session as in the article above (dots after “properties.” have to be escaped with ):

      create-javamail-resource –debug=false –storeProtocol=imap –auth=false –transportProtocol=smtp –host=smtp.gmail.com –storeProtocolClass=com.sun.mail.imap.IMAPStore –from=default-sender@email.com –transportProtocolClass=com.sun.mail.smtp.SMTPTransport –user=default@email.com –enabled=true –target=domain mail/myMailSession
      create-resource-ref –enabled=true –target=server mail/myMailSession
      set resources.mail-resource.mail/myMailSession.property.mail.smtp.user=myemail@gmail.com resources.mail-resource.mail/myMailSession.property.mail.smtp.password=${ALIAS=mygmail_pwd} resources.mail-resource.mail/myMailSession.property.mail.smtp.auth=true resources.mail-resource.mail/myMailSession.property.mail.smtp.port=465 resources.mail-resource.mail/myMailSession.property.mail.smtp.socketFactory.port=465 resources.mail-resource.mail/myMailSession.property.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

      You also need to configure the Notification service and the JavaMail notifier with another set of asadmin commands.

      1. Stanyslas Matayo

        many thanks, I did it the same way you presented it and it is working, I want to move to Docker and I will apply the same approach as describe before, many thanks for the support. One important point is also to open appropriate Port also and if there is firewall to configure outbound communication also.

        I saw also that there is possibility to do clustering with Payara Micro and Docker, please tell me if we need to configure it on all servers or just one….

Related Posts

4 minutes
Uncategorized

Leading the Way: Payara Platform Community 7 Beta Now Fully Jakarta EE 11 Certified

We’re excited to announce that Payara Platform Community 7 Beta application server is now fully certified as Jakarta EE 11 […]

What Is a Java Application Server? A Short Guide 6 minutes
Jakarta EE

What Is a Java Application Server? A Short Guide

Enterprise Java applications power global commerce, healthcare, government and countless other industries. These systems must be scalable, secure and […]

10 minutes
Uncategorized

Java’s 30th Anniversary: A Celebration of Legacy, Evolution and Community

May 2025 marks a monumental milestone in software development: Java turns 30. The impact of this language on the […]