« Back to Mail Integration

Mail Setup for External Portlets - JavaMail

(Redirected from Mail (JavaMail) Setup For External .WAR Portlets)

Introduction #

Liferay Portal provides the ability to configure mail server settings, so it would be redundant to configure these settings in each external portlet (.war).

In addition, the com.liferay.util.mail.MailEngine class provides convenience routines for sending email messages. In order to discover mail server settings, it performs a JNDI lookup for "java:comp/env/mail/MailSession" which is defined in a file determined by the servlet container / app server. In order for external portlets to be able to access the "mail/MailSession" resource, it must be available as a global JNDI lookup. For security purposes, Liferay does not make these settings globally availabe the out-of-the-box.

Ensuring Global JNDI Lookup #

For Tomcat:

  • Move the "MailSession" <Resource> from
    $CATALINA_HOME/conf/Catalina/localhost/ROOT.xml
    to
    $CATALINA_HOME/conf/context.xml

For Geronimo:

  • Using the console, un-deploy the
    liferay-portal-tomcat
    web application
  • Edit the mail server configuration settings of the
    LiferayMailSession
    <gbean>
    within
    $GERONIMO_HOME/deploy/liferay-portal.war/WEB-INF/geronimo-web.xml
  • Restart Geronimo, so that the portal will be re-deployed to the application server

For JBoss AS:

  • Edit the settings in
    $JBOSS_HOME/server/default/deploy/mail-service.xml

Java Source Code Example: #

import com.liferay.util.mail.MailEngine;
import com.liferay.util.mail.MailEngineException;

...

String attachment = "/home/mail/attachments/thankyou.doc";
InternetAddress fromInternetAddress = new InternetAddress(emailAddress);
InternetAddress[] toInternetAddresses = new InternetAddress[] {new InternetAddress(toEmailAddress)};
String subject = "My subject";
String body = "My message body";
MailEngine.send(fromInternetAddress, toInternetAddresses, null, null, subject, body, false, null, null, null, new File[] {_file});
0 Attachments
36936 Views
Average (5 Votes)
The average rating is 3.2 stars out of 5.
Comments
Threaded Replies Author Date
Good Puneet Upadhyay November 29, 2011 9:26 PM

Posted on 11/29/11 9:26 PM.