写定时任务发送邮件报错(Could not connect to SMTP host:smtp.exmail.qq.com,port:465)

需求阐述

在写完定时任务发送邮件之后,调试的时候发现报错:Could not connect to SMTP host:smtp.exmail.qq.com,port:465。一开始我以为是在for循环里发送邮件导致的,把循环禁用掉之后,发现还是这个报错。说明不是for循环的问题。之前定时任务发送邮件没问题,只有我写的这个定时任务循环发送邮件报这个错。

解决方式

在设置发送邮件的邮件服务器的属性时,添加这样一行代码:

props.put(“mail.smtp.ssl.protocols”, “TLSv1.2”);
    public getEmailSession(final String host, final String port, final String username, final String password) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
        Properties props = null;
        props = new Properties();
        // 设置发送邮件的邮件服务器的属性
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.ssl.enable", "true");
        **props.put("mail.smtp.ssl.protocols", "TLSv1.2");**
        session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值