1
votes

I am using Spring Mail in eclipse IDE to try to send an email, but the console always prints out "connection timeout" exception. I have confirmed that connection to the mail server is available by telnet. I am pretty new to Spring MVC framework. Maybe it is because I did not configure the spring-mvc.xml right? I did not use dependency injection in my code though. Attached below are the code and the exception. thank you for any insightful comments.

package xxx.web.controller;
import java.io.File; 
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl; 
import org.springframework.mail.javamail.MimeMessageHelper;

@Controller
public class SendMailController {

 private JavaMailSenderImpl mailSender;

 @RequestMapping(value ="/sendMail", method = RequestMethod.GET) 
 public void sendMail(HttpServletRequest request) throws MessagingException{
  mailSender = new JavaMailSenderImpl();
  mailSender.setHost("smtp.sina.com");  //configure mail server
  mailSender.setUsername("**********@sina.com");
  mailSender.setPassword("*******");

  MimeMessage msg = mailSender.createMimeMessage();
  MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "utf-8");

  msgHelper.setTo("********@qq.com");
  msgHelper.setFrom("*********@sina.com");
  msgHelper.setSubject("Testing Subject");
  msgHelper.setText("This is a test mail!");

  FileSystemResource file = new FileSystemResource(new File("D:/test.png"));
  msgHelper.addAttachment("test.png", file); //add an attachment

  Properties prop = new Properties();
  prop.put("mail.smtp.auth", "true");
  prop.put("mail.smtp.timeout", "25000"); 
  mailSender.setJavaMailProperties(prop);
  mailSender.send(msg);

  System.out.println("Email sent successfully!");
 }
}

The exception printed out in console:

message Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: **

Couldn't connect to host, port: smtp.sina.com, 25; timeout -1;

**

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.sina.com, 25; timeout -1; nested exception is: java.net.ConnectException: Connection timed out: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.sina.com, 25; timeout -1; nested exception is: java.net.ConnectException: Connection timed out: connect; message exceptions (1) are: Failed message 1: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.sina.com, 25; timeout -1; nested exception is: java.net.ConnectException: Connection timed out: connect org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) javax.servlet.http.HttpServlet.service(HttpServlet.java:620) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

1
Don't create an instance of the JavaMailSenderImpl configure it in your spring config and inject it into your controller. Are you sure you need port 25 or do you use another port. Make sure that you can access it from the server you are running the project onM. Deinum
I am pretty sure the port number is 25. I tried other port numbers using telnet and the connections were unsuccessful. Is it a must to use IoC and DI under the Spring MVC framework? I've seen some posts where explicit instantiating of JavaMailSenderImpl has been used in Spring MVC.erictamlam
You can of course use new instances but that isn't really how things are meant to be done. Also why would you want to create an instance each time yo need, it is quite a heavy operation..M. Deinum
But this should not be the cause of the connection timeout exception, right? I am trying to fix this exception.erictamlam
As I stated make sure you can connect to the server from the actual machine not any other just this instance.M. Deinum

1 Answers

0
votes

First of all you have to be sure your connection is OK; in order to check it, open a command line and execute the following commands:

telnet <yourservername> smtp

HELO <yourservername and then enter>

MAIL from: <mailaddr and then enter>

RCPT to: <mailaddr and then enter>
DATA
354 End data with <CR><LF>.<CR><LF>
From: <mailaddr and then enter>
To: <mailaddr and then enter>
Subject: test

This is test
.
250 2.0.0 Ok: queued as 4C67244003
QUIT  <and then enter>
221 2.0.0 Bye
Connection closed by foreign host.

If it's all OK, then it's Spring problem or you need to authenticate on the mail server (or more simply your mail server doesn't allow connection outside its own network)

Angelo