Sometime in your portlet you want to have sending email capability.
You can use JavaMail API, but actually Liferay already provide utility
class to send email, so we don’t have to play with direct JavaMail API.
Here are the steps to send email in Liferay :
1. Configure your SMTP provider, user id, and password on your application server
2. Use the MailEngine utility class to send email.
In this article, we’ll use Tomcat as our application server, and use Gmail as SMTP. You have to add information about SMTP host, user id, and password on resource configuration file under $tomcat_home\conf\Catalina\localhost\ROOT.xml.
Comment out resource name “mail/MailSession”, and add your mailsession configuration. So the ROOT.xml would be like this :
Here are the steps to send email in Liferay :
1. Configure your SMTP provider, user id, and password on your application server
2. Use the MailEngine utility class to send email.
In this article, we’ll use Tomcat as our application server, and use Gmail as SMTP. You have to add information about SMTP host, user id, and password on resource configuration file under $tomcat_home\conf\Catalina\localhost\ROOT.xml.
Comment out resource name “mail/MailSession”, and add your mailsession configuration. So the ROOT.xml would be like this :
Now we go to step 2. Class com.liferay.util.mail.MailEngine has send method, which is overrided with a few arguments. If you just want to send email with body, subject, and 1 recipient, you can just use MailEngine.send(String from, String to, String subject, String body) like this :<!-- Commented out
<Resource
name="mail/MailSession"
auth="Container"
type="javax.mail.Session"
mail.imap.host="localhost"
mail.pop3.host="localhost"
mail.smtp.host="localhost"
mail.store.protocol="imap"
mail.transport.protocol="smtp"
/>
-->
<
Resource
name
=
"mail/MailSession"
auth
=
"Container"
type
=
"javax.mail.Session"
mail.imap.host
=
"localhost"
mail.pop.host
=
"localhost"
mail.store.protocol
=
"imap"
mail.transport.protocol
=
"smtp"
mail.smtp.host
=
"smtp.gmail.com"
mail.smtp.port
=
"465"
mail.smtp.auth
=
"true"
mail.smtp.starttls.enable
=
"true"
mail.smtp.user
=
"your_user_id"
password
=
"your_password"
mail.smtp.socketFactory.class
=
"javax.net.ssl.SSLSocketFactory"
/>
If you want to add attachment to your email, or you want to add Cc, or send email in html format, you can use more complex send method from MailEngine class, such as
String from =
"sender@host.com"
;
String to =
"recipient@host.com"
;
String subject=
"This is email title"
;
String body=
"Hello World, this is my first email"
;
MailEngine.send(from, to, subject, body);
send( InternetAddress from, InternetAddress[] to, InternetAddress[] cc,
InternetAddress[] bcc, String subject, String body,
boolean
htmlFormat, InternetAddress[] replyTo, String messageId,
String inReplyTo, File[] attachments);
No comments:
Post a Comment