Java发送QQ邮件问题Could not connect to SMTP host: smtp.qq.com, port: 465(内附完整代码)

邮件发送失败,本地测试可以,部署到服务器就不行。

之前就碰到这个问题,解决办法就是把port端口改成587,然后注释这一段

MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);

之前这样改还可行,但是最近又不行了,报错

Could not connect to SMTP host: smtp.qq.com, port: 587;

试了重新生成授权码也不行,最后改好了,加上了这一句 ,指定协议。也可以去jdk配置里面删除关于这个协议的配置。

properties.put("mail.smtp.ssl.protocols", "TLSv1.2");

 用了好几年了,最近换服务器才出现这个问题的。

Java发送QQ邮件完整代码

package zjhuiwan.cn.helper;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SendEmail implements Runnable{
    private String email;// 收件人邮箱
    private String userName;// 用户名
    private Long id; // 用户id
    private String detail; //留言内容
    private int type; //留言/回复
    private String pageUrl; //页面路径
    private String articleTitle; //文章标题


    public SendEmail(String email, String detail,  String userName, int type, String pageUrl, String articleTitle) {
        this.email = email;
        this.userName = userName;
        this.detail=detail;
        this.type = type;
        this.pageUrl=pageUrl;
        this.articleTitle=articleTitle;
    }

    public void run() {
        // 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件
        String from = "z*******n@qq.com";// 发件人电子邮箱
        String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
        String code = "h******i"; // 发件人邮箱账号、授权码 

        Properties properties = System.getProperties();// 获取系统属性

        properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
        properties.setProperty("mail.transport.protocol","smtp");
      //  properties.setProperty("mail.smtp.port", "587");  //不需要 默认端口
        properties.setProperty("mail.smtp.auth", "true");// 打开认证
      //  properties.put("mail.smtp.socketFactory", "javax.net.ssl.SSLSocketFactory");

        try {
            // QQ邮箱需要下面这段代码,163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
            properties.put("mail.smtp.ssl.protocols", "TLSv1.2"); //加上这句解决问题

            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, code);
                }
            });

            session.setDebug(true);

            //2、通过session得到transport对象
            Transport transport = session.getTransport();
            //3、使用用户名和授权码连上邮件服务器
            transport.connect(host,from,code);


            // 4.创建邮件对象
            Message message = new MimeMessage(session);
            // 4.1设置发件人
            message.setFrom(new InternetAddress(from));
            // 4.2设置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 4.3设置邮件主题
            String title = "ZJBLOG留言回复通知";
            if(type==0){
                title = "亲爱的主人,ZJBLOG有新留言了";
            }else if(type ==1){
                title = "ZJBLOG留言回复通知";
            }else if(type ==2){
                title = "亲爱的主人,ZJBLOG有新评论了";
            }else if(type ==3){
                title = "ZJBLOG评论回复通知";
            }
            message.setSubject(title);
            // 2.4设置邮件内容
            String content = "";
            if(type==0){
                content = "<html><head></head><body>******</body></html>";
            }else if(type==3){
                content = "<html><head></head><body>*****</body></html>";
            }
            message.setContent(content, "text/html;charset=UTF-8");


            //5、发送邮件
            transport.sendMessage(message,message.getAllRecipients());
            //6、关闭连接
            transport.close();
            System.out.println("邮件成功发送!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

调用测试


    public static void main(String[] args) {
        new Thread(new SendEmail("17***93@qq.com", "测试邮件内容",
                "用户名zj",0,"","")).start();
    }

结果

 

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值