Pages

Hot News

Wednesday 5 August 2015

Email send in Liferay through JavaMail API?

Hi Guys,

Let's get a very brief idea about mail send in Liferay.

Email is an integral part of business communication.
Java offers the JavaMail API to make possible email communication from our applications easily.
It is a platform- and protocol-independent framework built as a part of the Java EE spectrum of web/enterprise application development.

The JavaMail API is a set of abstract classes defining the objects that comprise a mail system.

The Protocol of JavaMail

Protocol literally means maintaining certain norms, code of conduct, and so on.

Creating an application with email support is as simple as any Java programming, but the technical aspects behind the scene are quite intriguing and require some understanding of network protocols.

Let's get an idea of protocols supported by the JavaMail API.

SMTP - To supports the mechanism to deliver email.
POP - Mailbox mechanism use on the Internet to get mails.
IMAP - An advanced protocol for receiving messages, multiple mailbox support, mailbox sharing, and so forth.
MIME -It's concerned with the content of the message transfer such as message format, attachments, and so on.

 In liferay its pretty easy to do configuration.

1. GMAIL Configuration,

mail.session.mail.store.protocol=imap
mail.session.mail.transport.protocol=smtp

#smtp properties
mail.session.mail.smtp.host=smtp.gmail.com
mail.session.mail.smtp.password=_PASSWORD
mail.session.mail.smtp.user=_USEREMAILADDRESS

mail.session.mail.smtp.port=465
mail.session.mail.smtp.auth=true
mail.session.mail.smtp.starttls.enable=true
mail.session.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

mail.session.mail.smtp.socketFactory.fallback=false
mail.session.mail.smtp.socketFactory.port=465
# Other protocols mail.session.mail.imap.host=localhost
#mail.session.mail.pop3.host=localhost
#mail.session.mail.pop.host=smtp.gmail.com

2. Office 365 Configuration,

portal.host=_USEREMAILADDRESS
portal.password=_PASSWORD
mail.smtp.host=smtp.office365.com
mail.smtp.socketFactory.port=587
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=587
mail.smtp.starttls.enable=true

You can read these properties via portal-ext.properties in your class through PropsUtil.get() Method.

Please take note as,  I am showcase demo only for Office365 Mail.

Let's Create Utility class and define method.

MailSendHelper.java

public final class MailSendHelper {

private MailSendHelper(){}

public static void sendEmail(InternetAddress from,InternetAddress to, String subject, String bodyContent) {
final String userEmail = PropsUtil.get("portal.host");
final String password = PropsUtil.get("portal.password");
Session mailSession = null;
Properties props = new Properties();
props.put("mail.smtp.host","smtp.office365.com");
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", true);
props.put("mail.smtp.ssl.trust", "smtp.office365.com");
props.put("mail.debug", true);

mailSession = Session.getInstance(props,
     new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(userEmail ,password );
     }
});  try {
Message message = new MimeMessage(session);
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO, to);
message.setSubject(subject);
message.setContent(bodyContent, "text/html; charset=utf-8");
Transport.send(message);
LOG.info("DONE");
} catch (MessagingException e) {
LOG.error("Error occured in MailSendHelper",e);
}
}
private static final Log LOG = LogFactoryUtil.getLog(MailSendHelper.class);
}

JAVAMail API Download from this location: http://www.oracle.com/technetwork/java/javamail/javamail145-1904579.html

Hope this article will help you. :)



No comments:

Post a Comment