RE: [Liferay 7.4.3] Error when sending a mail message with attachment

Olaf Kock, modified 2 Years ago. New Member Posts: 2 Join Date: 1/21/21 Recent Posts

In previous versions, MailEngine.send(message) was used to send mail. But in version 7.4.3 this class is no longer available. If use MailServiceUtil.sendmail(message) to send a message with an attachment. That message is not sent because the attachments are passed as a File object ( contents in a temporary directory). And when the message is sent, the main stream has already been completed and the attachments from the temporary folder have already been deleted. How to correctly send messages with an attachment?

Jamie Sammons, modified 2 Years ago. Expert Posts: 327 Join Date: 12/20/10 Recent Posts
import com.liferay.mail.kernel.model.MailMessage;
import com.liferay.petra.mail.MailEngine;

MailMessage mailMessage = new MailMessage();
mailMessage.setHTMLFormat(true);
mailMessage.addFileAttachment(attachment);
MailEngine.send(mailMessage);

it works for me.

good luck