Martin Erlic
Martin Erlic

Reputation: 5665

Cannot send mail over SMTP from server but I can from localhost

I get the following when I try to send an email over SMTP in my Java application. I'm running a Springboot app from a fat JAR on my Windows Server 2012 R2.

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Apr 09 22:16:57 CEST 2017
There was an unexpected error (type=Internal Server Error, status=500).
Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: internetsmtp.test.com, 25; timeout -1; nested exception is: java.net.UnknownHostException: internetsmtp.test.com. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: internetsmtp.test.com, 25; timeout -1; nested exception is: java.net.UnknownHostException: internetsmtp.test.com

Code:

public void sendEmail(String from, String replyTo, String to, String subject, String body) {
        final Email email;
        try {
            email = DefaultEmail.builder()
                    .from(new InternetAddress(from))
                    .replyTo(new InternetAddress(replyTo))
                    .to(Lists.newArrayList(new InternetAddress(to)))
                    .subject(subject)
                    .body(body)
                    .encoding("UTF-8").build();
            emailService.send(email);
        } catch (AddressException e) {
            log.error("MailUtil got exception while sending mail = " + e.getMessage());
        }
    }

application.properties:

# Server
server.port=8080

# Mail Config
spring.mail.host: internetsmtp.test.com
spring.mail.port: 25
spring.mail.username: user123
spring.mail.password: test123
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: false
spring.mail.properties.mail.smtp.starttls.required: false
spring.mail.persistence.enabled: false
spring.mail.persistence.redis.embedded: false
spring.mail.persistence.redis.enabled: false
spring.mail.scheduler.priorityLevels=10

I even tried trusting the certificate:

keytool -import -file "C:\mail.cer" -keystore "C:\Program Files\Java\jre.1.8.0_121\lib\security\cacerts" -alias mail

I'm able to do all of this from localhost with no problem whatsoever. Is it possible that the issue is with my server blocking a connection to the SMTP server?

Upvotes: 0

Views: 2063

Answers (2)

Bill Shannon
Bill Shannon

Reputation: 29961

Most likely there's a firewall blocking direct connections. The JavaMail FAQ has more tips for debugging connection problems.

Upvotes: 1

Sheinbergon
Sheinbergon

Reputation: 3063

Your exception clearly states - java.net.UnknownHostException: internetsmtp.test.com . That means ( in this case) the DNS name is not resolvable by your application from the server . Make sure you are able to ping that domain from the server itself and that DNS access (port 53) is not blocked for that server

Upvotes: 1

Related Questions