简体   繁体   中英

JavaMail always uses localhost in Liferay

I am facing a strange problem and I can't figure out the problem. I have a web application/portlet deployed in Liferay portal which should be sending an email. I have the configuration correct and the code looks like this:

Session mailSession = mailSender.getSession();
Transport transport = mailSession.getTransport("smtp");

Message message = buildMessage(); // placeholder for building the message
try {
    transport.sendMessage(msg, msg.getAllRecipients());
} finaly {
    transport.close();
}

This, however, always tries to connecto to localhost:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
liferay_1      |   nested exception is:
liferay_1      |        java.net.ConnectException: Connection refused (Connection refused)

Now, I though that the connection settings are not correct, so I decided to log them:

LOG.debug("Host:" + mailSender.getHost() + " --- Port: " + mailSender.getPort());
LOG.debug("Password:" + mailSender.getPassword() + " --- User: " + mailSender.getUsername());
mailSession.getProperties().entrySet().forEach(p -> LOG.debug("---" + p.getKey() + "-" + p.getValue()));

And they are indeed correct in the session:

liferay_1      | DEBUG [http-nio-8080-exec-1] (MailSenderService:92) - Host:test-host --- Port: 5678
liferay_1      | DEBUG [http-nio-8080-exec-1] (MailSenderService:93) - Password:$test --- User: test
liferay_1      | DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
liferay_1      | DEBUG [http-nio-8080-exec-1] (MailSenderService:97) - ---mail.transport.protocol-smtp
liferay_1      | DEBUG [http-nio-8080-exec-1] (MailSenderService:97) - ---mail.debug-true
liferay_1      | DEBUG [http-nio-8080-exec-1] (MailSenderService:97) - ---mail.smtp.auth-test
liferay_1      | DEBUG [http-nio-8080-exec-1] (MailSenderService:97) - ---mail.smtp.starttls.enable-true
liferay_1      | DEBUG [http-nio-8080-exec-1] (MailSenderService:99) - Transport:smtp://liferay@

I can't figure out what else I can do and I don't know from where does the transport get localhost and port 25.

After further investigation, I discovered that there is a method for connecting to a specified host.

/**
 * Similar to connect(host, user, password) except a specific port
 * can be specified.
 *
 * @param host  the host to connect to
 * @param port  the port to connect to (-1 means the default port)
 * @param user  the user name
 * @param password  this user's password
 * @exception AuthenticationFailedException for authentication failures
 * @exception MessagingException        for other failures
 * @exception IllegalStateException if the service is already connected
 * @see #connect(java.lang.String, java.lang.String, java.lang.String)
 * @see javax.mail.event.ConnectionEvent
 */
public synchronized void connect(String host, int port,
    String user, String password) throws MessagingException

Using this one, you can specify the exact host, port and credentials. Why the Session does not contain this information and why it is not being used, even though the Transport and Session are obtained fro the original JavaMailSenderImpl is still a mystery to me.

What version of Liferay are you on?

First thing first, you will need to check if your credentials are correctly set up in Control Panel > Server Administration > Mail.

Check your:

  • Outgoing SMTP Server and
  • Outgoing Port

if they are still pointing to localhost and 25 respectively.

Second, the implementation looks unfamiliar to me. Can you try this way?

import com.liferay.mail.kernel.service.MailServiceUtil;
...

Message message = buildMessage();
MailServiceUtil.sendEmail(message); 

or if you want to get a session

MailServiceUtil.getSession()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM