mail No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.5</version>
</dependency>

oracle jdk_8u291 使用 mail.1.4.5 smtp 发送邮件 报错:No appropriate protocol (protocol is disabled or cipher suites are inappropriate),发送方式比较常规

private boolean sendMailSmtp(SendMailParam param) {
        // 返回值
        boolean result = true;

        try {
            // 服务器地址
            String mailServer = param.getMailServer();
            Long port = param.getPort();
            final String from = param.getFrom();
            final String authCode = param.getAuthCode();
            String subject = param.getSubject();
            String content = param.getContent();
            List<String> to = param.getTo();
            List<String> cc = param.getCc();
            List<String> bcc = param.getBcc();
            List<AttachFile> attachFiles = param.getAttachFiles();

            boolean ssl = param.isSsl();
            boolean debug = param.isDebug();

            Properties props = new Properties();
            props.setProperty("mail.smtp.host", mailServer);
            props.setProperty("mail.transport.protocol", AgreementType.SMTP.name().toLowerCase());
            props.setProperty("mail.smtp.auth", "true");
            if (port != null) {
                props.setProperty("mail.smtp.port", port.toString());
            }
            if (ssl) {
                // SSL加密
                MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);
                props.put("mail.smtp.ssl.enable", "true");
                props.put("mail.smtp.ssl.socketFactory", sf);
            }

            // 配置发送地址的授权码
            Authenticator authenticator = new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, authCode);
                }
            };
            // 设置收发地址
            Session session = Session.getDefaultInstance(props, authenticator);
            session.setDebug(debug);
            Message message = new MimeMessage(session);
            //发件人
            message.setFrom(new InternetAddress(from));
            //主题
            message.setSubject(subject);
            //收件人
            InternetAddress[] mailToAddrs = new InternetAddress[to.size()];
            for (int i = 0; i < to.size(); i++) {
                mailToAddrs[i] = new InternetAddress(to.get(i));
            }
            message.setRecipients(RecipientType.TO, mailToAddrs);
            //抄送人
            InternetAddress[] mailCcAddrs = new InternetAddress[cc.size()];
            for (int i = 0; i < cc.size(); i++) {
                mailCcAddrs[i] = new InternetAddress(cc.get(i));
            }
            if (mailCcAddrs.length > 0) {
                message.setRecipients(RecipientType.CC, mailCcAddrs);
            }
            //密送人
            InternetAddress[] mailBccAddrs = new InternetAddress[bcc.size()];
            for (int i = 0; i < bcc.size(); i++) {
                mailBccAddrs[i] = new InternetAddress(bcc.get(i));
            }
            if (mailBccAddrs.length > 0) {
                message.setRecipients(RecipientType.BCC, mailBccAddrs);
            }
            //内容
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(content, "text/html;charset=UTF-8");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            //附件
            for (AttachFile file : attachFiles) {
                MimeBodyPart attachPart = new MimeBodyPart();
                attachPart.attachFile(file.getAttachPath());
                multipart.addBodyPart(attachPart);
            }
            message.setContent(multipart);

            // 发送
            // Transport.send(message);
            Transport transport = session.getTransport();
            transport.connect();
            transport.sendMessage(message, message.getAllRecipients());
        } catch (Exception e) {
            result = false;
            logger.error("发送邮件出现错误:", e);
        }
        return result;
    }

当把mail依赖 切换完 commons-email-1.5 后 测试正常,而 commons-email 使用的是 javax.mail-1.5.6…

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.5</version>
</dependency>

原因: transport.connect(); jdk8 调用 https 代码

解决方式:找到jdk/jre/lib/security/java.security文件进行修改 ,去除 jdk.tls.disabledAlgorithmsSSLv3TLSv1 两种协议

# Example:
# jdk.tls.disabledAlgorithms=MD5, SSLv3, DSA, RSA keySize < 2048
# jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
jdk.tls.disabledAlgorithms=TLSv1.1, RC4, DES, MD5withRSA, \
    DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
    include jdk.disabled.namedCurves
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值