
package org.opencps.kernel.message.email;

import com.liferay.mail.kernel.model.MailMessage;
import com.liferay.mail.kernel.service.MailService;
import com.liferay.mail.kernel.service.MailServiceUtil;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.json.JSONFactory;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.service.ServiceContext;

import org.opencps.kernel.message.MBMessageEntry;
import org.osgi.service.component.annotations.Component;

/**
 * @author trungnt
 */
@Component(immediate = true, service = MBEmailSenderImpl.class)
public class MBEmailSenderImpl implements MBEmailSender {

	@Override
	public void send(
		MBMessageEntry messageEntry, String portletId,
		ServiceContext... serviceContexts) {

		if (messageEntry != null && messageEntry.isSendEmail() && messageEntry.getToAddress().length > 0) {
			System.out.println("===SEND_MAIL_TO_ADD=======" + messageEntry.getToAddress()[0].getAddress());
			MailMessage mailMessage = new MailMessage();
			mailMessage.setSubject(messageEntry.getEmailSubject());
			mailMessage.setTo(messageEntry.getToAddress());
			mailMessage.setBody(messageEntry.getEmailBody());
			mailMessage.setHTMLFormat(true);
			mailMessage.setFrom(messageEntry.getFrom());

			MailServiceUtil.sendEmail(mailMessage);

		}


	}
	
	@BeanReference(type = MailService.class)
	protected MailService mailService;

}

@@ -191,24 +191,68 @@
 erviceUtil;%0A
+import com.liferay.petra.string.StringPool;%0A
 import com.l
@@ -329,79 +329,30 @@
 nel.
-json.JSONFactory;%0Aimport com.liferay.portal.kernel.json.JSONFactoryUtil
+service.ServiceContext
 ;%0Aim
@@ -382,23 +382,35 @@
 .kernel.
-log.Log
+util.PrefsPropsUtil
 ;%0Aimport
@@ -440,26 +440,22 @@
 nel.
-log.LogFactoryUtil
+util.PropsKeys
 ;%0Aim
@@ -485,38 +485,30 @@
 .kernel.
-service.ServiceContext
+util.Validator
 ;%0A%0Aimpor
@@ -1334,53 +1334,336 @@
 %0A%09%09%09
-mailMessage.setFrom(messageEntry.getFrom());%0A
+String smtpUser = PrefsPropsUtil.getString(%0A%09%09%09%09%09PropsKeys.MAIL_SESSION_MAIL_SMTP_USER,%0A%09%09%09%09%09StringPool.BLANK);%0A%09%09%09if (Validator.isNotNull(smtpUser)) %7B%0A%09%09%09%09messageEntry.getFrom().setAddress(smtpUser);%09%09%09%09%0A%09%09%09%09mailMessage.setFrom(messageEntry.getFrom());%0A%09%09%09%09System.out.println(%22SEND EMAIL FROM: %22 + messageEntry.getFrom());%0A%09%09%09%7D
 %0A%09%09%09

Update send mail from stmp user