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)
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 on – M. DeinumJavaMailSenderImpl
has been used in Spring MVC. – erictamlam