java使用smtp协议发送邮件,使用ssl加密协议,端口为465,发送失败

问题描述:
java使用smtp协议发送邮件,使用ssl加密协议,端口为465,发送失败,报错:javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

代码如下:

public boolean sendMail3() {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.qq.com");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  //使用JSSE的SSL socketfactory来取代默认的socketfactory
            properties.put("mail.smtp.socketFactory.fallback", "false");  // 只处理SSL的连接,对于非SSL的连接不做处理
            properties.put("mail.smtp.ssl.enable", "true");                            
            properties.put("mail.smtp.port", "465");
            properties.put("mail.smtp.socketFactory.port", "465");

            Session session = Session.getInstance(properties);
            session.setDebug(true);
            MimeMessage message = new MimeMessage(session);

            try {
                // 发件人
                Address address = new InternetAddress("发件人邮箱");
                message.setFrom(address);
                
                    
                    Address toAddress = new InternetAddress("接收人邮箱");
                    message.setRecipient(MimeMessage.RecipientType.TO, toAddress); // 设置收件人,并设置其接收类型为TO
                    /**
                     * TO:代表有健的主要接收者。 CC:代表有健的抄送接收者。 BCC:代表邮件的暗送接收者。
                     * */
                

                // 主题
                message.setSubject("测试邮件");

                // 时间
                message.setSentDate(new Date());

                Multipart multipart = new MimeMultipart();
                // 添加文本
                BodyPart text = new MimeBodyPart();
                text.setText("测试内容123");
                multipart.addBodyPart(text);
                // 添加附件
//                for (String fileName : p_w_uploadNames) {
//                    BodyPart adjunct = new MimeBodyPart();
//                    FileDataSource fileDataSource = new FileDataSource(fileName);
//                    adjunct.setDataHandler(new DataHandler(fileDataSource));
//                    adjunct.setFileName(changeEncode(fileDataSource.getName()));
//                    multipart.addBodyPart(adjunct);
//                }
//                // 清空收件人集合,附件集合
//                recipients.clear();
//                p_w_uploadNames.clear();

                message.setContent(multipart);
                message.saveChanges();

            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            try {
                Transport transport = session.getTransport("smtp");
                transport.connect("smtp.qq.com", "发件人邮箱", "发件人邮箱授权码");
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            return true;
        }

![img](

我的解答思路和尝https://img-mid.csdnimg.cn/release/static/image/mid/ask/180747092736175.png "#left")

试过的方法

想要正常发送ssl加密协议使用465端口邮件

受累各位大佬帮忙支支招,实在不行了

img

这代码都是大同小异,邮箱账户你需要自己去开启smtp服务
这是某企鹅邮箱,默认可是关闭的,确认打开的话,还是报错,果断使用JavaMailSender

img

你上面的“发件人邮箱”,”发件人邮箱授权码“,"接收人邮箱",这些是你发上来故意改成这样的,还是原来就是这样的?

https://hgg923.blog.csdn.net/article/details/116520994

我之前用163的发送,好像没有端口呢,你可以借鉴一下

你确定你账户搞了?

img


img


img


img

看下我的文章