erictamlam 2015-05-07 07:10 采纳率: 0%
浏览 20811

Spring Mail用QQ邮箱测试发送邮件失败【连接超时】

求大神解答

Java代码:

 public class SendMailController {

    //@Autowired
    private JavaMailSenderImpl mailSender;

    @RequestMapping(value ="/sendMail", method = RequestMethod.GET) 
    public void sendMail(HttpServletRequest request) throws MessagingException {
        mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.qq.com");  //设置邮件服务器
        mailSender.setUsername("XXXX@qq.com");
        mailSender.setPassword("**********");

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

        msgHelper.setTo("YYYYYYYYY@qq.com");
        msgHelper.setFrom("XXXXXXX@qq.com");
        msgHelper.setSubject("测试发送带附件的邮件");
        msgHelper.setText("测试邮件");

        FileSystemResource file = new FileSystemResource(new File("D:/test.png"));
        msgHelper.addAttachment("test.png", file); //添加附件

        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("邮件发送成功!");
    }
}

错误:
type Exception report

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.qq.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.qq.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.qq.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.qq.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)

  • 写回答

3条回答 默认 最新

  • jayriver 2015-06-04 03:22
    关注

    悬赏多少啊?第一次回答问题。从你的描述来看,大概是这种情况:

    java默认会优先使用ipv6,但是你检查一下你的电脑,是不是ipv6那里没有获得ip?只有ipv4那里是有的。

    2种办法:
    1.运行你的程序的时候,跟上-Djava.net.preferIPv4Stack=true
    2.在你电脑上禁用ipv6,就是在网络连接状态-属性里去掉勾选。

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值