使用qq邮箱发送邮件出错Mail server connection failed

发送邮件失败,求解答

 Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketTimeoutException: Read timed out; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketTimeoutException: Read timed out
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2202)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1939)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:501)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
    at cn.cherish.common.MailUtil.main(MailUtil.java:44)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2182)
    ... 8 more

代码如下:

 public class MailUtil {
    public static void main(String args[]) {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost("smtp.qq.com");
        senderImpl.setProtocol("smtp");
        senderImpl.setUsername("785427346@qq.com");
        senderImpl.setPassword("xxxxxxxxx");
        senderImpl.setPort(465);
        senderImpl.setDefaultEncoding("UTF-8");

        // 建立邮件消息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 设置收件人,寄件人 用数组发送多个邮件
        String[] array = new String[] {"xxxx@qq.com","yyyy@qq.com"};
        mailMessage.setTo(array);
        mailMessage.setFrom("785427346@qq.com");

        mailMessage.setSubject("测试简单文本邮件发送!");
        mailMessage.setText("测试我的简单邮件发送机制!!");

        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
        // 发送邮件
        senderImpl.send(mailMessage);

        System.out.println(" 邮件发送成功.. ");
    }
}

2个回答

自己回复我改用587端口就成功了

 senderImpl.setPort(587);//587/465
it_chenfanglin
it_chenfanglin 这个可以
2 年多之前 回复

发送的端口从465改为587,亲测可行,挺坑啊官方明明说写465端口却不行

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐