I've just run into very odd bug. I was setting up a verify simple test case for sending gmail with Javamail
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.debug", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxx", "xxx");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xxx@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("xxx@gmail.com"));
message.setSubject("Test Subject");
message.setText("Test message");
Transport.send(message);
System.out.println("Sent");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
Despite the "mail.smtp.host" setting, the thing decided to connect to "localhost" instead
java.lang.RuntimeException: javax.mail.SendFailedException:
Send failure
(javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 465
(java.net.ConnectException: Connection refused: connect))
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:254)
at javax.mail.Service.connect(Service.java:85)
at javax.mail.Service.connect(Service.java:70)
at javax.mail.Transport.send(Transport.java:94)
at javax.mail.Transport.send(Transport.java:48)
at org.quartz.jobs.SendMailJobTest.testSendMail(SendMailJobTest.java:71)
My classpath had both org.apache.openejb:javaee-api:5.0-2 and javax.mail:mail:1.4.7 on it. The javaee-api was there as provided scope for compilation and the java.mail was there with test scope. Searching the web for answers didn't come up with anything so I decided to remove javaee-api and voila, everything worked from then on. That was a wild goose chase. I have to note that the latest javaee-api:5.0-3 or javaee-api:6.0-5 don't seem to contain that bug. It's good be up to date.