Sending mail in liferay


 In most of your project you may come across sending an mail to the user when ever updated.

In liferay its pretty easy to do with that

       InternetAddress fromAddress = new InternetAddress(user.getEmailAddress()) // from address
        InternetAddress toAddress = new InternetAddress(user.getEmailAddress())  // to address

            String body = ContentUtil.get("/templates/sample.tmpl", true);  // email body , here we are getting email structure creating the content folder in the src and create the file with the extension as tmpl.

Here i have created a folder called content and also created file as sample.tmpl

            String subject = "subject" // email subject
           
            body = StringUtil.replace(body, new String []{"[$NAME$]","[$DESC$]"}, new String []{"Name","Description"}); // replacing the body with our content.

            MailMessage mailMessage = new MailMessage();
            mailMessage.setTo(toAddress);
            mailMessage.setFrom(fromAddress);
            mailMessage.setSubject(subject);
            mailMessage.setBody(body);
            mailMessage.setHTMLFormat(true);
           
            MailServiceUtil.sendEmail(mailMessage); // Sending message

Template file is as follows (sample.tmpl)

Hi [$NAME$],<br/><br/>

  [$DESC$]. <br/><br/>
 
Best Regards,<br/>
VenkaReddy<br/>



Comments

  1. How can I add-display image inside body part of velocity file. I tried but in mail i can not see image which I add in velocity file.
    Help me.
    in gmail Google appended some extra url as prefix in my img src="" tag.

    ReplyDelete
    Replies
    1. Hi Pradip,

      Have you figure out the solution for this issue?

      Delete
  2. Hi,
    Where are the $NAME$ and $DESC$ variables being fetched from ?? And, if my mail description is " Welcome to liferay", where am I supposed to write it ?

    ReplyDelete
  3. Hi Prattyy,

    $NAME$ and $DESC$ are from the tmpl file(sample.tmpl).

    Below code is the replacing the $NAME$ and $DESC$ with the Name and Description.

    String Name= "Test User";
    String Description = "Welcome to liferay";

    StringUtil.replace(body, new String []{"[$NAME$]","[$DESC$]"}, new String []{Name,Description});

    Hopes this helps,

    Let me know if you need something else.

    ReplyDelete
  4. Good one information. I will update this one code on Liferay hosted website and will check it if everything works fine. Dissertation writing services

    ReplyDelete
  5. Good one guidelines. I believe that following all these guidelines, I will become able to send mail in liferay.

    ReplyDelete
  6. Lazaro Cardenas Street in Mexico City is the unofficial dividing line that separates the Historic Center from the extending modern part. If you travel to the west, you soon arrive in the broad and beautiful expanse of Reforma, Mexico City's Champs d'Elysees.
    open cpm

    ReplyDelete
  7. how can i send an Email in Liferay 7 GA4. i just need the steps for it!!

    ReplyDelete
  8. I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. sharing. 
    dissertation Writing Service

    ReplyDelete
  9. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
    custom suits woodbridge

    ReplyDelete
  10. Did somebody use services to get help with the homework? Like this?

    ReplyDelete
  11. Best Zoho software consulting in uae ksa and Bahrain.
    AL Fahad IT consulting is providing the best zoho training service.


    Zoho Training

    ReplyDelete

Post a Comment

Popular posts from this blog

Theme display in javascript

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate