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个回答

哥们,我遇到同样的问题,你的问题解决掉了吗

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

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

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

端口错了,qq邮箱的端口不是25,是465

qq_42215960
qq_42215960 回复Shiny_boy_: 587也不可以
一年多之前 回复
Shiny_boy_
Shiny_boy_ 465也不行
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问