Search code examples
emailsmtpportletliferay-7

Liferay 7 MailService.sendMail from a custom portlet doesn't work only on HOST SERVER


I have a strange problem with a custom portlet on Liferay 7 to solve:

  1. MailService.sendMail is working from MY COMPUTER with google smtp and a personal account: this means that the code is working...
  2. On my HOST SERVER sending emails with the final-smtp works correctly: I tried both root and liferay user with telnet final-smtp port.
  3. On the Liferay server on HOST SERVER, send e-mails works correctly: if I forget the password, Liferay send me the e-mail.
  4. But ... if I try to send email with my portlet from Liferay on HOST SERVER it doesn't work without any error. I'm using Liferay MailService.sendMail. I post the code but it works (on MY COMPUTER).

I get the service in this way:

    @Reference(unbind = "-")
    protected void setMailService(MailService mailService) {
       _mailService = mailService;
    }

And the calling code is in the following:

    InternetAddress fromAddress = null;
    String newsletterPrefix = null;
    InternetAddress toAddress = null;

    try {
        String smtpUser = PropsUtil.get(
        "newsletter.send.mail.smtp.user");

        String smtpToUser = PropsUtil.get(
        "newsletter.send.mail.smtp.to.user");

        if (Validator.isNotNull(smtpUser)) {
            fromAddress = new InternetAddress(smtpUser);
        }
        if (Validator.isNotNull(smtpToUser)) {
            toAddress = new InternetAddress(smtpToUser);
        }
    }
    catch (Exception e) {
        _log.error(e, e);
        result = false;
    }



    MailMessage mailMessage = new MailMessage(
        fromAddress, toAddress, subject, body, true);
        mailMessage.setBCC(addressList);


    _mailService.sendEmail(mailMessage);

Solution

  • There could be several reasons behind this, some not even code related. I see you are using this code for a newsletter, which suggests you are using a smtp service that is meant for this.

    It could be that your server is in fact sending the email, with success, but the smtp server is simply blocking, rejecting or marking to resend later. Moreover, that server might be configured to not send an error message, or sending an error message in the form of a successful delivery, but the data contains the error.

    I would start checking you mail server configuration, and the accounts permissions, then its logs. Also, you might consider using plugins for mass mail delivery, like this one: https://www.e-systems.tech/blog/-/blogs/connecting-liferay-to-mailgun