3/10/11

javamail session on Glassfish and how to send an email

javamail session on Glassfish and how to send an email

In order to be able to send an email from a web application using Glassfish you must first setup a javamail session.

Go to glassfish administration console that is usually on port 4848. E.g. if you have installed glassfish locally then you may access it:
http://localhost:4848/

log in and go to Resources/ JavaMail Sessions.Create a new javamail session. Fill the fields accordingly.
mail/yoursessionnamehere  (the JNDI name for a javamail session must always start with "mail/"
Mail Host: smtp.googlemail.com ( I am using google services so my SMTP server is smtp.googlemail.com)
Default User: someuser@yourdomain.com
Default Sender Address: someuser@yourdomain.com
Description:  mail session to send support emails (write something to rememeber what this is for)
Store Protocol:  imap
Store Protocol Class:com.sun.mail.imap.IMAPStore
Transport Protocol:  SMTP
Transport Protocol Class: com.sun.mail.smtp.SMTPTransport
Debug: disabled

Additional Properties (9)
mail-smtp-host smtp.googlemail.com
mail-smtp-user someuser@yourdomain.com
mail-smtp-password yourpassword
mail-smtp-auth true
mail-smtp-port 465
mail-smtp-socketFactory-port 465
mail-smtp-socketFactory-class javax.net.ssl.SSLSocketFactory
mail-smtp-starttls-enable true
mail-smtp-socketFactory-fallback false

I have created a bean to send emails. This is the finally combination of code that worked for me after several tries



/*
 * EmailUtil.java
 *
 * Copyright 1988 MINIpay.
 * All Rights Reserved. Patents pending.
 *
 * This software is the proprietary information of MINIpay
 * Use is subject to license terms.
 *
 */


package eu.minipay.common;


import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/**
 *
 * @author minipay.eu
 */
@Stateless
public class EmailBean {


    @Resource(name = "mail/supportMINIpay")
    private Session mailSession;


    public void sendMessage(String email, String subject, String bodyMessage) throws MessagingException {        
        
        Message message = new MimeMessage(mailSession);
        try {
            message.setSubject(subject);
            message.setRecipient(RecipientType.TO, new InternetAddress(email)); 
            // this is if you want message body as text
//            message.setText(bodyMessage); 
            
            // use this is if you want to send html based message
            message.setContent(bodyMessage, "text/html; charset=utf-8");


            // This is not mandatory, however, it is a good
            // practice to indicate the software which
            // constructed the message.
            message.setHeader("X-Mailer", "MINIpay mailer www.minipay.eu");


            // Adjust the date of sending the message
            Date timeStamp = new Date();
            message.setSentDate(timeStamp);


            Transport.send(message);
        } catch (MessagingException ex) {
            throw ex;
        }
    }
}
To send an email just call the method inside a  bean method. E.g. in my case when a user requests a new password first get the bean
@EJB
    private eu.minipay.common.EmailBean emailUtil;
then just
emailUtil.sendMessage("email@domain", "subject", "message");
This works for me and I hope it will save you hours of searching and testing.

No comments:

Post a Comment