3

I am trying to connect to my email server using my Spring Boot app. When I telnet localhost 25 from command prompt , I see the below -

220 Microsoft ESMTP MAIL Service ready at Wed , 18 May 2016 11:09:30 -0400

However when i try to connect from my program , I see the below error

Caused by: org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.SocketException: Permission denied: connect. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.SocketException: Permission denied: connect at

Code

public class MailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${email.from}")
    private String from;

    @Value("${email.subject}")
    private String subject;


    public void send() {
        MimeMessage mail = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);            
            helper.setTo(emailAddress);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText("Lorem ipsum dolor sit amet [...]");
        } catch (MessagingException e) {
            e.printStackTrace();
        } finally {}
        javaMailSender.send(mail);
    }



}

Config

#Email Settings
spring.mail.host=localhost
spring.mail.port=25
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315

1 Answers1

4

Try this:

  1. Open the port(25) in your firewall.
  2. Turn off the antivirus.
  • Thanks doge. if the firewall is blocking , I shouldn't be able to telnet from my command prompt as well. So i believe the port is open. – Punter Vicky May 18 '16 at 15:27
  • There is a similar question http://stackoverflow.com/questions/36301545/how-to-solve-sending-the-email-to-the-following-server-failed-socketexception – Abylay Sabirgaliyev May 18 '16 at 15:34
  • Thanks Doge! I think my problem was due to #2. I exposed the mail service on port 8081 instead of 25 and I was able to send the email using my java program. – Punter Vicky May 18 '16 at 15:58