javamail发送qq邮箱失败案例分析

javaMail报错:Unsupported or unrecognized SSL message

	c.n.m.service.impl.EmailServiceImpl      : 邮件发送异常, Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
	javax.net.ssl.SSLException: Unsupported or unrecognized SSL message. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
	javax.net.ssl.SSLException: Unsupported or unrecognized SSL message

原因分析: ssl与tls端口

坑点:官方文档发送邮件服务器: smtp.qq.com,使用SSL,端口号465或587,其实是使用SSL用465,使用TLS使用587,不能混用,特别是JavaMail在设置Properties属性的时候需要指定使用哪一种协议,不能用混,否则报错
QQ邮箱SMTP/IMAP服务

在这里插入图片描述
比如说我配置的是邮箱服务器是 smtp.qq.com:587,但是JavaMail的Properties设置的是
javaMailProperties.put(“mail.smtp.starttls.enable”, “true”);则会报上述错,因为协议不匹配,必须使用465端口
在这里插入图片描述

总结

“mail.smtp.ssl.enable”和“mail.smtp.starttls.enable”是JavaMail邮件发送配置中的两个不同参数,它们的区别主要在于使用的加密协议

mail.smtp.ssl.enable:这个参数用于启用SSL(Secure Sockets Layer)加密协议。当设置为true时,邮件传输将通过一个SSL连接进行,这通常意味着使用465端口。SSL协议在建立连接后立即加密数据流,适用于那些要求全加密通信的SMTP服务器。

mail.smtp.starttls.enable:这个参数用于启用TLS(Transport Layer Security)加密协议。当设置为true时,邮件传输将通过一个开始时未加密的连接进行,然后在传输过程中升级为TLS加密,这通常意味着使用587端口。TLS协议是在数据传输之前建立一个加密层,适用于那些提供明文连接然后升级到加密通信的SMTP服务器。

总的来说,mail.smtp.ssl.enable和mail.smtp.starttls.enable都是用于邮件加密的配置项,但它们分别对应不同的加密方式和端口。如果SMTP服务器支持SSL,则应配置mail.smtp.ssl.enable;如果SMTP服务器支持TLS,则应配置mail.smtp.starttls.enable。

 private JavaMailSenderImpl generateMailSender(EmailSendDto emailSendDto) {
        JavaMailSenderImpl currentMailSender = new JavaMailSenderImpl();
        currentMailSender.setHost(emailSendDto.getHost());
        currentMailSender.setPort(Integer.parseInt(emailSendDto.getPort()));
        currentMailSender.setUsername(emailSendDto.getUsername());
        currentMailSender.setPassword(emailSendDto.getPassword());
        currentMailSender.setDefaultEncoding("UTF-8");
        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailProperties.put("mail.smtp.auth", "true");
        javaMailProperties.put("mail.smtp.ssl.enable", "true");        // 465端口
//        javaMailProperties.put("mail.smtp.starttls.enable", "true"); // 587端口
        javaMailProperties.put("mail.debug", "true");
        currentMailSender.setJavaMailProperties(javaMailProperties);
        return currentMailSender;
    }
  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用JavaMail下载QQ发送的附件,您需要进行以下步骤: 1. 首先,您需要获得QQ的POP3和SMTP服务器地址。您可以在QQ邮箱的设置中找到这些信息。 2. 然后,您需要使用JavaMail API连接到QQ邮箱的POP3服务器。这将允许您检索邮件和附件。以下是一个示例代码片段,演示如何连接到QQ邮箱的POP3服务器: ``` Properties props = new Properties(); props.setProperty("mail.store.protocol", "pop3"); props.setProperty("mail.pop3.host", "pop.qq.com"); props.setProperty("mail.pop3.port", "995"); props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getDefaultInstance(props); Store store = session.getStore(); store.connect("username@qq.com", "password"); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); ``` 3. 接下来,您需要从邮件中提取附件。以下是一个示例代码片段,演示如何提取邮件中的附件: ``` for (Message message : messages) { Multipart multipart = (Multipart) message.getContent(); for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i); if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { InputStream inputStream = bodyPart.getInputStream(); // 将 inputStream 写入文件或进行其他操作 } } } ``` 请注意,此示例代码仅提供参考。具体实现可能因您的具体情况而异。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr朱墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值