javax.mail 邮件unable to find valid certification path to requested target


javax.mail.MessagingException: Could not connect to SMTP host: xxxxx, port: 465;
  nested exception is:
	javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target




Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 代码如下
        Properties properties = System.getProperties();
        properties.put("mail.transport.protocol","smtp");
        properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.fallback","false");
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.socketFactory.port","465");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory",new MailSSLSocketFactory());
        properties.put("mail.smtp.ssl.checkserveridentity", true);
        Session session = Session.getInstance(properties);
        session.setDebug(true);

这里报错时

MailSSLSocketFactory引用的为
com.sun.mail.util.MailSSLSocketFactory

解决问题方案替换MailSSLSocketFactory为下面的类,

import lombok.extern.slf4j.Slf4j;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;


@Slf4j
public class MailSSLSocketFactory extends SSLSocketFactory {
    private SSLSocketFactory factory;

    public MailSSLSocketFactory() {
        try {
            SSLContext sslcontext = SSLContext.getInstance("TLSv1.2");
            sslcontext.init(null, new TrustManager[] { new MailTrustManager() }, null);
            factory = sslcontext.getSocketFactory();
        } catch (Exception ex) {
            // ignore
            log.info(ex.getMessage(),ex);
        }
    }

    public static SocketFactory getDefault() {
        return new MailSSLSocketFactory();
    }

    @Override
    public Socket createSocket() throws IOException {
        return factory.createSocket();
    }

    @Override
    public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
        return factory.createSocket(socket, s, i, flag);
    }

    @Override
    public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {
        return factory.createSocket(inaddr, i, inaddr1, j);
    }

    @Override
    public Socket createSocket(InetAddress inaddr, int i) throws IOException {
        return factory.createSocket(inaddr, i);
    }

    @Override
    public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
        return factory.createSocket(s, i, inaddr, j);
    }

    @Override
    public Socket createSocket(String s, int i) throws IOException {
        return factory.createSocket(s, i);
    }

    @Override
    public String[] getDefaultCipherSuites() {
        return factory.getDefaultCipherSuites();
    }

    @Override
    public String[] getSupportedCipherSuites() {
        return factory.getSupportedCipherSuites();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot项目中,如果使用RestTemplate调用https接口时出现"unable to find valid certification path to requested target"的错误,可能是由于缺少证书导致的。这个错误通常发生在RestTemplate在尝试建立与目标服务器的安全连接时无法验证服务器的证书。 要解决这个问题,可以按照以下步骤进行操作: 1. 首先,确保你的项目中已经添加了Lombok依赖。Lombok是一个Java库,可以通过简化一些常见的任务来减少代码量。 2. 确保你的项目中的pom.xml文件中已经添加了Lombok依赖项。可以在pom.xml中添加以下代码来引入Lombok依赖: ```xml <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> ``` 3. 当使用RestTemplate调用https接口时,可以尝试在代码中使用静态方法的形式去调用RestTemplate,并且设置合适的请求头和请求体。具体代码如下所示: ```java HttpHeaders headers = new HttpHeaders(); headers.set("xxx", "123"); headers.set("xxx","456"); headers.set("Content-Type","text/plain"); HttpEntity<String> body = new HttpEntity<>("请求体字符串", headers); ResponseEntity<String> response = RestTemplateUtil.getRestTemplate(timeout).postForEntity(sendUrl, body, String.class); ``` 通过以上步骤,你可以解决创建Lombok依赖时出现的"unable to find valid certification path to requested target"错误。这样你就可以在Spring Boot项目中正常使用RestTemplate来调用https接口了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [unable to find valid certification path to requested target](https://blog.csdn.net/y_bccl27/article/details/111358091)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [javax.mail 邮件unable to find valid certification path to requested target](https://blog.csdn.net/wnsh1990/article/details/128875221)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值