Spring MVC邮件连接超时异常

时间:2015-05-07 11:57:46

标签: spring email model-view-controller connection timeout

我在eclipse IDE中使用Spring Mail尝试发送电子邮件,但控制台总是打印出"连接超时"例外。我已确认通过telnet可以连接到邮件服务器。我是Spring MVC框架的新手。也许是因为我没有配置spring-mvc.xml对吗?我没有在我的代码中使用依赖注入。下面附有代码和例外。谢谢您的任何见解。

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!");
 }
}

在控制台中打印出异常:

消息请求处理失败;嵌套异常是org.springframework.mail.MailSendException:邮件服务器连接失败;嵌套异常是com.sun.mail.util.MailConnectException:**

  

无法连接到主机,端口:smtp.sina.com,25;超时-1;

**

说明服务器遇到内部错误,导致无法完成此请求。

例外

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.mail.MailSendException:邮件服务器连接失败;嵌套异常是com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.sina.com,25;超时-1;   嵌套异常是:     java.net.ConnectException:连接超时:连接。失败的消息:com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.sina.com,25;超时-1;   嵌套异常是:     java.net.ConnectException:连接超时:connect;消息异常(1)是: 失败的消息1:com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.sina.com,25;超时-1;   嵌套异常是:     java.net.ConnectException:连接超时:连接     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 个答案:

答案 0 :(得分:0)

首先,您必须确保您的连接正常;为了检查它,打开命令行并执行以下命令:

$("div[questionId]").each(function(){
$(this).animate({'opacity':3},300);
});

如果一切正常,那么它是Spring问题,或者您需要在邮件服务器上进行身份验证(或者更简单地说,您的邮件服务器不允许在自己的网络之外进行连接)

安吉洛