Liferay mail is sending hostname and user information with header

342 Views Asked by At

I am using Liferay mail (MailServiceUtil/MailEngine) to send mails to users. I have configured mail in ServerAdministration > Mail . My code is as below:

    InternetAddress toAddress = new InternetAddress(emailTo);            
    InternetAddress fromAddress = new InternetAddress("XXXXXXX","XX");
    MailMessage mailMessage = new MailMessage();
    mailMessage.setTo(toAddress);
    mailMessage.setFrom(fromAddress);
    mailMessage.setSubject(subject);
    mailMessage.setBody(emailbody);
    mailMessage.setHTMLFormat(true);
    MailServiceUtil.sendEmail(mailMessage);

Below information is sent by above code:

Received: from hostname ([UNAVAILABLE]. [XXX.XXX.XXX])

And

Message-ID: <1567646772.21541142172709.JavaMail.user@hostname>

I got method to change message id but i am not able to change hostname (hostname to localhost).

I have tried to set mail.session.mail.smtp.localhost=localhost by adding Manually specify additional JavaMail properties to override the above configuration or portal-ext.propeties. But no success. Please help to set this as localhost.

2

There are 2 best solutions below

5
Olaf Kock On

I believe you're speaking of mail headers like this:

Received: from host.example.com ([x.x.x.x]) by anotherhost.example.com
 (anotherhost [y.y.y.y]) with ESMTPS (Nemesis) id blablablabla;
 Thu, 01 Nov 2018 11:07:15 +0100

AFAIK, they're added by the next one up in the chain - e.g. your Liferay server is the originator, and didn't receive the mail from anyone, but it forwards to the next hop in the list of mail handlers. That server, the next hop, adds information about the origin of this email - so it's safely out of your control on the Liferay or Javamail side.

The message id is something that's generated to be unique, and it's possible to override the default (that's what you got). The protocol of systems that have forwarded this mail is built by the next system, thus not overridable on the originating system.

Edit (reaction to your comment):

Even if there's a way to have the last machine in the list identify as "localhost", what good is it really? The next hop up will correctly give the machine's IP address and reverse-lookup hostname. And you'll only make debugging harder, if you need to figure out, which "localhost" a mail has originated on. Remember: You'll have at least as many localhosts in your network as you have machines (in total, the sum of VMs, Containers, bare metal)

0
Bill Shannon On

The Message-ID is derived from the results of InternetAddress.getLocalAddress, which you can completely control by setting the mail.from property.