Created
July 23, 2025 07:13
-
-
Save 000benniu/fc0adddf3272ffc905927620e5ca4485 to your computer and use it in GitHub Desktop.
Liferay Email Send test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Liferay Groovy Script to send a test email. | |
| // This script demonstrates how to use Liferay's MailServiceUtil to send an email. | |
| // Before running, ensure your Liferay instance's SMTP settings are correctly configured | |
| // in Control Panel -> System Settings -> Mail. | |
| import com.liferay.mail.kernel.model.MailMessage; | |
| import com.liferay.mail.kernel.service.MailServiceUtil; | |
| import com.liferay.portal.kernel.log.Log; | |
| import com.liferay.portal.kernel.log.LogFactoryUtil; | |
| import javax.mail.internet.InternetAddress; | |
| // Initialize Liferay's logger for output in the Script Console | |
| Log _log = LogFactoryUtil.getLog("EmailTestGroovyScript"); | |
| _log.info("Starting email test script..."); | |
| try { | |
| // 1. Define Sender's Email Address | |
| // IMPORTANT: Ensure this address is NOT in your mail.send.blacklist | |
| // If you're testing the "Review Date" issue, use the same sender you configured in Instance Settings. | |
| String fromAddress = "[email protected]"; // <-- REPLACE WITH YOUR SENDER EMAIL | |
| String fromName = "Liferay Test Sender"; // <-- REPLACE WITH YOUR SENDER NAME | |
| // 2. Define Recipient's Email Address | |
| String toAddress = "[email protected]"; // <-- REPLACE WITH YOUR RECIPIENT EMAIL (a real one!) | |
| String toName = "Test Recipient"; // <-- REPLACE WITH YOUR RECIPIENT NAME | |
| // 3. Define Email Subject and Body | |
| String subject = "Liferay Groovy Email Test - " + new Date(); | |
| String body = """ | |
| Hello ${toName}, | |
| This is a test email sent from a Liferay Groovy script. | |
| If you received this, your Liferay mail configuration is working! | |
| Timestamp: ${new Date()} | |
| Regards, | |
| Liferay System | |
| """; | |
| // 4. Create InternetAddress objects for sender and recipient | |
| InternetAddress from = new InternetAddress(fromAddress, fromName); | |
| InternetAddress to = new InternetAddress(toAddress, toName); | |
| // 5. Create a MailMessage object | |
| // The constructor takes: from, to, subject, body, HTML format (true/false) | |
| MailMessage mailMessage = new MailMessage(); | |
| mailMessage.setFrom(from); | |
| mailMessage.setTo(to); | |
| mailMessage.setSubject(subject); | |
| mailMessage.setBody(body); | |
| mailMessage.setHTMLFormat(false); // Set to true if your body contains HTML | |
| // 6. Send the email using MailServiceUtil | |
| _log.info("Attempting to send email from " + fromAddress + " to " + toAddress + " with subject: " + subject); | |
| MailServiceUtil.sendEmail(mailMessage); | |
| _log.info("Email sent successfully (or queued for sending). Check recipient's inbox."); | |
| } catch (Exception e) { | |
| _log.error("Error sending email: " + e.getMessage(), e); | |
| // You can also print to the console for immediate feedback in the Script Console | |
| out.println("Error sending email: " + e.getMessage()); | |
| } | |
| _log.info("Email test script finished."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment