Ouertani Slim was born in Tunisia in 1981. Now he is a software engineer since 2004 and he is Java 6 and Spring certified with 100% and 94% respectively. He is very interested in Java technology, Scala and open source projects. He believes that programming is one of the hardest jobs and most beautiful in the world. Slim has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

Sending an Email using the JavaMail Session and Glassfish

  • submit to reddit
PurposeThis tutorial is a supplement to the article of oracle published here.
After reading this later, I decided to share some tips about this EE environment javaMail api configuration.
IntroductionTwo years ago, we decided to move to JEE 6 for our enterprise solutions and take a lot of fun with new EJB 3.1 features and annotations. Glassfish has made ​​our lives easier, it was very easy to declare variables using it's administration console.

Less complicated than using global resource environment for properties It is also the continuation to this tricks this tutorial delegate to Glassfish EE server managing mail session.
PrerequisitesBefore starting this tutorial, you should:
Fully understand of http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/javamail/javamail.html

Have access to an SMTP server. You must know the host name, port number, and security settings for your SMTP server. Web mail providers may offer SMTP access, view your email account settings or help to find further information. Be aware that your user name is often your full email address and not just the name that comes before the @ symbol.

Have basic familiarity with Servlets and CDIs (helpful but not required)

Create a Java mail session resource :
To create and JavaMail Session :

  1. start galssfish server
  2. open admin console ( default localhost:4848)
  3. Go to Resources -> JavaMail Sessions -> click new to add new javaMail resource
  4. make sure the following field are filled in :

  1. Jndi name : EMailME for example, will be used later on lookup resource :
    @Resource(lookup = "EMailME")
  2. Mail host
  3. Default user
  4. Default sender will be used later on mailSession.getProperty("mail.from")
5- For secure Mail add this advanced setting :

1-mail.smtp.password : email password
2-mail.smtp.port : email port
3-mail.smtp.auth : true

This screenshot summarizes all these parameters :
Java Mail Session
II - Using email Service
This tutorial is a supplement to oracle one. We will use CDI with the same example.

1 - MailSessionBean Will be a CDI bean with RequestScope
2- Inject the full JavaMail Session as resource rather than create it

public class EmailSessionBean {

    @Resource(lookup = "EMailME")
    private Session mailSession;

    public void sendEmail(String to, String subject, String body) {
        MimeMessage message = new MimeMessage(mailSession);
        try {

            message.setFrom(new InternetAddress(mailSession.getProperty("mail.from")));
            InternetAddress[] address = {new InternetAddress(to)};
            message.setRecipients(Message.RecipientType.TO, address);
            message.setSentDate(new Date());

        } catch (MessagingException ex) {
Look short !

4- EmailServlet remind the same except using @Inject rather than @EJB

@WebServlet(name = "EmailServlet", urlPatterns = {"/EmailServlet"})
public class EmailServlet extends HttpServlet {

    private EmailSessionBean emailBean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
Conclusion : Let's EE Server manager MailSession for us, keep configuration on server and thanks to Glassfish administration console managing JavaMail Session became easier.
Published at DZone with permission of its author, Slim Ouertani.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Liam Knox replied on Tue, 2012/06/26 - 9:56am

A post that should not be here



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.