Sending email in Liferay Portal

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 :


    <!-- 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"
      />

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 :


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);

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

send( InternetAddress from, InternetAddress[] to, InternetAddress[] cc,
            InternetAddress[] bcc, String subject, String body,
            boolean htmlFormat, InternetAddress[] replyTo, String messageId,
            String inReplyTo, File[] attachments);

Done. Tested on Liferay 4.3.3

14 comments so far

  1. Suhan on

    Hi Sahat

    i am sending an email from my liferay portal to my personal email id, it includes some text field and drop-down and one save button.

    When i am clicking on save button from my personal email id. the drop-down and text field values what i entered it should save in my database.

    Please Help me out

    • Johnd800 on

      Thanks so much for sharing this excellent info! I’m seeking forward to see much more posts! kdcfaegfeage

  2. Vinh Son on

    when I use MailEngine.send, I has problem

    com.liferay.util.mail.MailEngineException: javax.naming.NameNotFoundException: Name mail is not bound in this Context
    at com.liferay.util.mail.MailEngine.send(MailEngine.java:245)

    I use Lifery 4.2

  3. gio on

    arte contemporanea italia

  4. […] in portal-ext.properties and restart the server.and In custom portlet mail config check this link Flag Please sign in to flag this as […]

  5. […] B:Please someone suggest how to configure email details with liferayHi Bhaskar,Kindly visit this link it has all sufficient steps to configure email details with liferay.Thanks & Regards,Vishal R. […]

  6. Amol on

    i want to send mail to our smtp office 365 server. what should be need to change?

  7. patio door on

    Having read this I thought it was very enlightening. I appreciate you spending some time and energy
    to put this content together. I once again find myself spending a significant amount of time both reading and posting comments.
    But so what, it was still worthwhile!

  8. toddler beds on

    If you re in a metropolitan area, you may also try calling them.
    The first time should be after breakfast to get rid
    of them. If you are looking for a pediatric dentist only after an unsuccessful visit to a general dentist.

    If, however, becomes trickier as access to family dental insurance plans.
    It is fortunate that dog s have a natural tendency to chew and this acts as a Bunk Beds mechanism.

  9. nursery wall stickers on

    Have you ever thought about including a little bit more than just
    your articles? I mean, what you say is valuable and all.
    Nevertheless think about if you added some great pictures or video clips to give your posts more,
    “pop”! Your content is excellent but with pics and
    video clips, this blog could definitely be one
    of the very best in its field. Good blog!

  10. Dentist in Bothell on

    Dental problem is extremely vry common condition among develope solid relationships .
    people. Foor example brown teeth might lead to people losing confidence iin themselves.
    In reality individuals wit brown teeth may struggle to address people.
    Brown teeth are simply among very many problems that people experience.
    This consists of tooth aching, sharp paiins when eating and tooth sensitivity.

    But a times you coukd possibly battle to look for a dentist who can help you wjth your dental issues.
    However will allow you to on tips on howw tto find an instat dentist listing.
    You can visit dentistlisting.biz to day and rest you be confident that
    you will locate an excellent dentist who will help you using your dental problem.
    dentistlisting.biz which is probably tthe instant dentist listings.
    It is possible to find specialists from the following areas:
    Teeth whitening
    As being a person ages, he become vunerable to mineral
    exposure that mmay cause gradual darkening of teeth.
    Dentists allow us ways of taking out the discoloration which
    alolow patiients tto regain their once very bright teeth.
    You will find techniques that were developed they include chemical whitening,
    micro abrasion and laser tooth whitening.
    Veneers
    Veneers aree customs that are made oof shells which can be
    fixed on the top of the front teeth to avoid teeth against coffee or tobacco stains.

    Visit this web today plus your desntal problem would hae
    been a thing of the past.

  11. Minda on

    I’m not sure where you are getting your info, but good topic.

    I needs to spend some time learning much more
    or understanding more. Thanks for wonderful info I was looking for this information for my mission.

  12. technology blog 654 on

    I’m looking joomla template to design site for one hindu temple.? Any suggestion for template and components for Joomla ?. Mainly looking for event details , picture gallery and blogs..

  13. Investigations Toronto Inc. on

    Blogging is definately not my day job. Im trying to promote my
    business by wanting to write about it. But crap is it ever
    difficult. I really do admire your posts, and I had formed to comment to just
    offer you kuddos on wonderful content and information.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: