How to check whether E-Mail was successfully sent in Liferay 6.1.20EE via MailServiceUtil

I'm using Liferay 6.1.20 and my Portlet is using the Liferay Mail Service to send an E-Mail via the in Liferay configured SMTP-Server.

My Question is now: is it possible to check whether the SMTP-Server is available or is it possbile to check whether the E-Mail was successfully sent? If the SMTP-Server is not available I want to give an output to the User. Actually I only see the following line in the server console:

14:06:47,679 WARN  [liferay/mail-1][MailEngine:560] Failed to connect to a valid mail server. Please make sure one is properly configured. Could not connect to SMTP host: localhost, port: 25

I hope someone can give me a solution for that. Thanks!


Solution 1:

basically there are two problems which prevent you to discover if any failure occurred when sending e-mails with MailServiceUtil service.

Firstly messages are sent asynchronously over Liferay message bus, so the user gets faster response but you never know if anything failed on the way.

Secondly messages eventually get to MailEngine where all exceptions get caught and are only written to the log.

So, I suggest you to override MailEngine class with ext-plugin (it's in util-java) to handle exceptions differently and then use it directly from your plugin instead of MailServiceUtil which only pushes all to the message bus.

Solution 2:

Since Liferay 6.2, if you set mail.throws.exception.on.failure=true in your portal-ext.properties file, the MailEngine class will also throw a com.liferay.util.mail.MailEngineException on errors, instead of just logging.