Sending Mail in Liferay Programmatically


In the Previous blog we see how to we Configure mail in Liferay.In this blog we will see how to send mail programmatically . Sending mail in Liferay is very simple we just need to use MailServiceUtil class that is provided by Liferay .Here we Consider 3 Scenario:-

So lets start :-


Scenario 1:-Send Mail With Plain Text
Create a method in your class as:-

public void sendMailWithPlainText() {
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress("aa665845@gmail.com");
toAddress = new InternetAddress("xxx@gmail.com");
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject("Testing mail with Plain Text");
mailMessage.setBody("This Mail Comes From Liferay Is Easy");
MailServiceUtil.sendEmail(mailMessage);
System.out.println("Send mail with Plain Text");
} catch (AddressException e) {
e.printStackTrace();
}
}
view raw Plain.java hosted with ❤ by GitHub

Explanation:-

Here FromAddress should be same email address that is used in Configuration.

Output:-







Scenario 2:-Send Mail With HTML Text
Create a method in your class as:-

public void sendMailWithHTMLFormat() {
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress("aa665845@gmail.com");
toAddress = new InternetAddress("xxx@gmail.com");
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject("Testing mail with HTML Text");
mailMessage.setBody("<h3><font color = RED>This Mail Comes From Liferay Is Easy</font></h3>");
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage);
System.out.println("Send mail with HTML Format");
} catch (AddressException e) {
e.printStackTrace();
}
}
view raw HTML.java hosted with ❤ by GitHub

Explanation:-
Here we use two things:-

  • In mailMessage.setBody("") we use HTML tags.
  • And setHTMLFormat(true).

Output:-


Note:-  If you setHTMLFormat(false) than mail is sent successfully but the HTML tags are print as normal Text.

Scenario 3:-Send Mail With Attachment
Create a method in your class as:-

public void sendMailWithAttachment() {
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
File file = new File("H:\\Certificate.pdf");
String fileName = "Chess Certificate.pdf";
try {
fromAddress = new InternetAddress("aa665845@gmail.com");
toAddress = new InternetAddress("adit2787@gmail.com");
MailMessage mailMessage = new MailMessage();
mailMessage.setTo(toAddress);
mailMessage.setFrom(fromAddress);
mailMessage.setSubject("Sending mail with Attachment");
mailMessage.setBody("Sending a File by Liferay Is Easy that Contain PDF file in Attachment");
mailMessage.addFileAttachment(file, fileName);
MailServiceUtil.sendEmail(mailMessage);
System.out.println("Send mail with Attachment");
} catch (AddressException e) {
e.printStackTrace();
}
}
view raw Attachment.java hosted with ❤ by GitHub

Explanation:-
Here we are using addition method addFileAttachment that take two arguments first is File object and second is Name that you want to given to attachment.

Output:-







Hope this will Help....

Related Post:-