JavaMail配置SSL服务器及安装证书

客户给的邮件服务器要SSL,使用Spring的JavaMailSenderImpl,Spring配置如下

 

<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
	<property name="host" value="${mail.smtp.host}" />
	<property name="username" value="${mail.smtp.username}" />
	<property name="password" value="${mail.smtp.password}" />
	<property name="port" value="${mail.smtp.port}" />
	<property name="javaMailProperties">
		<props>
			<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
			<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
		 	<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
			<prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
			<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
			<prop key="mail.smtp.socketFactory.fallback">false</prop>
		</props>
	</property>
</bean>
 

mail.properties内容如下

 

mail.smtp.host=smtp.xyz.com
mail.smtp.port=465
mail.smtp.username=alerts+abc.net
mail.smtp.password=12345678
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.timeout=50000
mail.from=alerts@abc.net
mail.replyTo=alerts@abc.net

 

发送邮件时得到异常如下

 

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
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:419)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:342)
Caused by: javax.mail.MessagingException: Exception reading response;
  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
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260)
    ... 4 more
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
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1611)
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)
    ... 8 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:285)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:191)
    at sun.security.validator.Validator.validate(Validator.java:218)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)
    ... 20 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:280)
    ... 26 more

 

Google一下,需要保存安装证书

 

官方的QA http://java.sun.com/products/javamail/FAQ.html#installcert

 

Q: When connecting to my mail server over SSL I get an exception like "unable to find valid certification path to requested target".
A: Your server is probably using a test certificate or self-signed certificate instead of a certificate signed by a commercial Certificate Authority. You'll need to install the server's certificate into your trust store. The InstallCert program will help.

 

在这里 http://blogs.sun.com/andreas/entry/no_more_unable_to_find 有解决方案

 

http://blogs.sun.com/andreas/resource/InstallCert.java 的代码来安装证书

 

运行 java InstallCert smtp.xyz.com:465
得到jssecacerts文件后复制到jdk1.6.0_14\jre\lib\security目录

 

然后再发送邮件就OK了

 

附件是 InstallCert.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中使用JavaMail库匿名发送邮件,你可以按照以下步骤进行操作: 1. 首先,确保你已经导入了javamail库。你可以在Maven项目中添加以下依赖项: ```xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> ``` 2. 创建一个`Properties`对象来配置SMTP服务器的连接属性。设置SMTP服务器的主机名和端口号,以及启用SSL加密(如果需要): ```java Properties properties = new Properties(); properties.put("mail.smtp.host", "your_smtp_host"); properties.put("mail.smtp.port", "your_smtp_port"); properties.put("mail.smtp.ssl.enable", "true"); // 如果需要加密,将其设置为"true" ``` 3. 创建一个`Session`对象,用于建立与SMTP服务器的连接。在创建`Session`对象时,可以通过传递`Authenticator`对象来实现匿名发送邮件: ```java Session session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("", ""); // 空的用户名和密码 } }); ``` 4. 创建一个`Message`对象,用于构造邮件。设置发件人、收件人、主题和内容等信息: ```java Message message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Hello, World!"); message.setText("This is the content of the email."); ``` 5. 使用`Transport`类发送邮件: ```java Transport.send(message); ``` 这样,你就可以使用JavaMail库在内网中匿名发送邮件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值