Mails

Necessary classes

com.liferay.portal.util.SubscriptionSender
You can send mails programmatically using Liferay-Services. Here is one example:
private void sendMail( long fromUserId, long toUserId, String comment) throws SystemException, PortalException {
User fromUser = UserLocalServiceUtil.getUser(fromUserId);
User toUser = UserLocalServiceUtil.getUser(toUserId);

String fromName = fromUser.getFullName();
String fromAddress = fromUser.getEmailAddress()

String toName = toUser.getFullName();
String toAddress = toUser.getEmailAddress();

String subject = "Example mail-subject";
String body = "Example mail-body. Here is some comment: [$COMMENTS$]";

SubscriptionSender subscriptionSender = new SubscriptionSender();

subscriptionSender.setBody(body);
subscriptionSender.setCompanyId(toUser .getCompanyId());
subscriptionSender.setContextAttributes( "[$COMMENTS$]", comment);
subscriptionSender.setFrom(fromAddress, fromName);
subscriptionSender.setHtmlFormat(true);
subscriptionSender.setMailId("userId", toUserId);
subscriptionSender.setServiceContext(serviceContext);
subscriptionSender.setSubject(subject);
subscriptionSender.setUserId(userId);
subscriptionSender.addRuntimeSubscribers(toAddress, toName);
subscriptionSender.flushNotificationsAsync();
}
As you can see you can use
subscriptionSender.setContextAttributes(Obect...values);
to replace placeholders in your mail content.

Leave a Reply