2 caimengyuan caimengyuan 于 2016.04.22 18:01 提问

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

CSDNXIAOD
CSDNXIAOD   2016.04.22 18:12

rails使用QQ邮箱发送邮件蛋疼的经历
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

caimengyuan
caimengyuan   2016.04.22 19:19

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

 senderImpl.setPort(587);//587/465
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!