利用hutool配置发送邮件MessagingException异常处理

时间:2025-03-10 14:14:37

问题描述

:5.7.9
:1.5.0-b01
利用hutool配置发送邮件,ssl认证也打开了,jdk下面的jre/lib/security/文件中相关的ssl信息也去掉了,但是还是报错:
MessagingException: Could not connect to SMTP host: , port: 465

网上大部分解决办法就是上面两种,下面分享第三种情况:

解决方案:

提示:MailSSLSocketFactory sf = new MailSSLSocketFactory();
(true);
(“”,sf);

// 完整代码

    public static void send(EmailVo emailVo, EmailConfig emailConfig){
    if(() == null){
        throw new BadRequestException("请先配置,再操作");
    } // 封装
    MailAccount account = new MailAccount();
    (());
    (());
    (());
    ((()));
    (()+"<"+()+">");
    //启动调试
    (true);
    // 开启SSL安全连接
    (true);
    (true);
    String content = ();
    // 发送
    try {
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        (true);
        ("",sf);
        //指定实现接口的类的名称,这个类将被用于创建SMTP的套接字
        ("");
        //如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用创建的套接字类, 默认值为true
        (true);
        // 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口456
        (465);
        int size = ().size();
        Mail mail = (account)
                .setTos(().toArray(new String[size]))
                .setTitle(())
                .setContent(content)
                .setHtml(true)
                //关闭session
                .setUseGlobalSession(false);

        if (() != null)
            (());

        ();

    }catch (Exception e){
        (());
        throw new RuntimeException(());
    }
}