简体   繁体   中英

Wildfly Mail-Session always uses localhost

I'm trying to setup some simple mail service on Wildfly 8.1 with Gmail(for testing).
For some reason, every time I transport the Mail, Wildfly tries to connect to localhost instead of the assigned outbound-socket !

Error:

13:22:34,164 ERROR [stderr] (default task-22) com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
13:22:34,164 ERROR [stderr] (default task-22)   nested exception is:
13:22:34,165 ERROR [stderr] (default task-22)   java.net.ConnectException: Connection refused: connect
13:22:34,165 ERROR [stderr] (default task-22)   at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)

standalone.xml

...
<mail-session name="java:jboss/mail/gmail" debug="true" jndi-name="java:jboss/mail/gmail">
    <smtp-server outbound-socket-binding-ref="mail-smtp" ssl="true" username="user@gmail.com" password="pass"/>
</mail-session>
...
<outbound-socket-binding name="mail-smtp">
    <remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding>

Then I just try and send a mail:

@Stateless
public class SendMail {

@Resource(mappedName = "java:jboss/mail/gmail")
private Session mailSession;

public String send() {
    MimeMessage m = new MimeMessage(mailSession);
    try {
        m.setRecipients(Message.RecipientType.TO, "test@mail.com");
        m.setContent("Test from Wildfly","text/plain");
        Transport.send(m);//throws exception
    } catch (MessagingException e) {
        e.printStackTrace();
    }
...

I've tried " name " & " lookup " in the @Resource annotation but it just keeps wanting to connect to localhost , for which there isn't even an outbound-socket .

What am I missing?

I was facing the same problem, but it seems that WildFly simply needs a reload after new Java Mail settings are applied. Try restarting WildFly, and it should use your new settings.

If reload didn't work.

Replace line

Transport.send(m);

with:

Transport transport = m.getTransport("smtp");
if (!transport.isConnected())
   transport.connect();
transport.sendMessage(m, m.getAllRecipients());
transport.close();

Hope this helps you as it helped me.

Your using the wrong port. It should be port 465 or 587. Your still using port 25 (from the error message)

Just adding my experience as I've just face the same issue: The reason for re-starting is because your Session attribute is null and if you are modifying it on the fly you need to restart the server to reload standalone.xml configuration file.

In my case, I was using a test class using final attribute, which makes the resource injection useless and it has been solved just by removing it.

BTW, as you're using an Stateless EJB, you might want to consider using Asynchronous notation while sending email. :)

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