java mail 签名,将JavaMail与自签名证书一起使用

在尝试使用JavaMail发送邮件时,遇到使用自签名证书导致的SSL连接问题。错误提示为必须先发出STARTTLS命令。尝试导入Google SMTP证书到keystore并修改属性,但出现新的SSLHandshakeException,原因是找不到有效的认证路径。解决方案是移除过时的socketFactory属性,只需设置"mail.smtp.ssl.enable"为"true",并将Gmail证书添加到信任存储库。
摘要由CSDN通过智能技术生成

I have a servlet that contains the following code:

System.setProperty("javax.net.ssl.keyStore", getServletContext().getRealPath("keystore.jks"));

System.setProperty("javax.net.ssl.keyStorePassword", "123456");

System.setProperty("javax.net.ssl.trustStore",getServletContext().getRealPath("keystore.jks"));

System.setProperty("javax.net.ssl.trustStorePassword","123456");

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();

SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("127.0.0.1", 606);

With this code I'm able to use SSL (what is mandatory for my case). Later in the same servlet I need to send an e-mail using JavaMail. When I try to send this e-mail without using my certificate in the other code, it works fine, but when I try using it the following message is returned:

Using port 465 ->

DEBUG: setDebug: JavaMail version 1.4.7

DEBUG: setDebug: JavaMail version 1.4.7

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false

Aaand get stuck on this =p.

Using port 587 ->

DEBUG: setDebug: JavaMail version 1.4.7

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false

220 mx.google.com ESMTP g66sm55412571yhd.11 - gsmtp

DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO RAI-PC

250-mx.google.com at your service, [201.82.118.174]

250-SIZE 35882577

250-8BITMIME

250-STARTTLS

250 ENHANCEDSTATUSCODES

DEBUG SMTP: Found extension "SIZE", arg "35882577"

DEBUG SMTP: Found extension "8BITMIME", arg ""

DEBUG SMTP: Found extension "STARTTLS", arg ""

DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""

DEBUG SMTP: use8bit false

MAIL FROM:

530 5.7.0 Must issue a STARTTLS command first. g66sm55412571yhd.11 - gsmtp

DEBUG SMTP: got response code 530, with response: 530 5.7.0 Must issue a STARTTLS command first. g66sm55412571yhd.11 - gsmtp

RSET

250 2.1.5 Flushed g66sm55412571yhd.11 - gsmtp

DEBUG SMTP: MessagingException while sending, THROW:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. g66sm55412571yhd.11 - gsmtp

a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值