javax.mail.MessagingException: Exception reading response

javax.mail.MessagingException: Exception reading response

Java、Spring:邮件发送不成功,会是什么问题呢?
QQ邮箱的相关服务均已开启:
在这里插入图片描述
在这里插入图片描述
代码如下:

 public class Demo {
    public static void main(String[] args) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();

        sender.setHost("smtp.qq.com");
        sender.setPort(465);
        sender.setUsername("********@qq.com");
        sender.setPassword("********");    //邮箱密码不填邮箱密码,而是填分配的授权码

        Properties pro = System.getProperties();
        pro.put("mail.smtp.auth", "true");
        pro.put("mail.smtp.timeout", "25000");

        sender.setJavaMailProperties(pro);

        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("********@qq.com"); // 发送人 
            helper.setTo("********@126.com"); // 收件人  
            helper.setSubject("Title"); // 标题
            helper.setText("Content"); // 内容
            sender.send(message);
            System.out.println("发送完毕!");
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

报错如下:

javax.mail.MessagingException: Exception reading response

或者

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:899)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:158)
    at javax.mail.Service.connect(Service.java:233)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)
    at Demo.main(Demo.java:31)

--------------------已解决,完整代码如下-----------------

public class Demo {
    public static void main(String[] args) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();

        sender.setHost("smtp.qq.com");
        sender.setPort(465);
        sender.setUsername("*********@qq.com");
        sender.setPassword("xqbhcaptnzurzbhef"); // 这里要用邀请码,不是你登录邮箱的密码

        Properties pro = System.getProperties(); // 下面各项缺一不可
        pro.put("mail.smtp.auth", "true");
        pro.put("mail.smtp.ssl.enable", "true");
        pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        sender.setJavaMailProperties(pro);

        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("*********@qq.com"); // 发送人 
            helper.setTo("*********@126.com"); // 收件人  
            helper.setSubject("Title"); // 标题
            helper.setText("Content"); // 内容
            sender.send(message);
            System.out.println("发送完毕!");
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值