spring中javaMail使用ssl的方法发送邮件,javaMail导入证书,忽略证书的方法

41 篇文章 1 订阅
38 篇文章 0 订阅

使用配置

## 基础邮箱配置(每个环境可以覆盖配置)
# 发送者的邮箱地址
spring.mail.username=xxxx
# 发送者的邮箱密码
spring.mail.password=xxx
# 发送邮箱对应的SMTP地址
spring.mail.host=xxxx
spring.mail.port=465

发送邮件是出现报错

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

是java使用ssl时证书信任问题。

解决办法这里有两这种:

1)将证书导入到jdk的信任证书中:  JAVA 导入信任证书 (Keytool 的使用)

2)忽略证书信任问题: 添加相应配置:

## 基础邮箱配置(每个环境可以覆盖配置)
# 发送者的邮箱地址
spring.mail.username=xxxx
# 发送者的邮箱密码
spring.mail.password=xxxx
# 发送邮箱对应的SMTP地址
spring.mail.host=xxxx
spring.mail.port=465
spring.mail.default-encoding=UTF-8
# 这里填发送邮箱对应的SMTP地址 ,忽略证书,信任域名
spring.mail.properties.mail.smtp.ssl.trust=xxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
是的,Spring Boot 可以使用 JavaMail API 来读取 IMAP 邮件的附件并保存附件。JavaMail API 是 Java EE 平台用于发送和接收电子邮件的标准 API,它提供了一组类和接口,使开发人员可以轻松地编写邮件客户端程序。 以下是一个示例代码,演示如何在 Spring Boot 使用 JavaMail API 来读取 IMAP 邮件的附件并保存附件: ```java import java.io.File; import java.io.IOException; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.MimeBodyPart; import javax.mail.search.FlagTerm; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MailReader { @Value("${mail.username}") private String username; @Value("${mail.password}") private String password; @Value("${mail.imap.host}") private String imapHost; @Value("${mail.imap.port}") private int imapPort; public void readAndSaveAttachments() { try { Properties properties = new Properties(); properties.put("mail.imap.host", imapHost); properties.put("mail.imap.port", imapPort); properties.put("mail.imap.ssl.enable", "true"); Session session = Session.getDefaultInstance(properties); Store store = session.getStore("imap"); store.connect(username, password); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); // Search for unread messages FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false); Message[] messages = inbox.search(flagTerm); for (Message message : messages) { Multipart multipart = (Multipart) message.getContent(); // Get all the attachments for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i); // If the bodypart is an attachment if (bodyPart.getDisposition() != null && bodyPart.getDisposition().equalsIgnoreCase("attachment")) { MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart; File file = new File(mimeBodyPart.getFileName()); // Save the attachment to a file mimeBodyPart.saveFile(file); } } // Mark the message as read message.setFlag(Flags.Flag.SEEN, true); } inbox.close(false); store.close(); } catch (MessagingException | IOException e) { e.printStackTrace(); } } } ``` 在上面的代码,我们首先创建了一个 Properties 对象,用于配置连接到 IMAP 服务器的参数。然后,我们使用该 Properties 对象创建一个 Session 对象,并使用 Session 对象创建一个 Store 对象来连接到 IMAP 服务器。接下来,我们打开收件箱 Folder 对象,并使用 FlagTerm 对象搜索未读邮件。对于每个未读邮件,我们获取其内容并将其转换为 Multipart 对象。然后,我们遍历 Multipart 对象的所有 BodyPart 对象,并找到所有附件。对于每个附件,我们将其保存到一个文件,并将消息标记为已读。 请注意,上面的代码的用户名、密码、IMAP 主机和端口都是从应用程序的配置文件读取的。在使用此代码时,请确保将这些值替换为您自己的值。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值