java mail ssl方式,Java mail ssl并存的一个问题

最近在做java mail ssl遇到一个问题:使用JavaMail收取邮件在系统运行一定时间后,无法成功.

出错信息是这样的:

javax.mail.MessagingException: Connect failed;

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.pop3.POP3Store.protocolConnect(POP3Store.java:148)

at javax.mail.Service.connect(Service.java:275)

at javax.mail.Service.connect(Service.java:156)

at javax.mail.Service.connect(Service.java:105)

或者未启用SSL的mail服务器出错如下:

nested exception is:

java.net.ConnectException: Connection refused

at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148)

telnet到邮件服务器,用户名密码都没有问题,看来只有程序问题了。

创建MailSession时用到Properties props = System.getProperties();把这个打印出来的时候发现错误了,本来不应该使用SSL验证的,却出现了验证信息不应该出现这个,所以现在在每次POP3链接的时候如果不需要SSL链接都remove这些属性,问题没有了。

终于知道谁改动了System.getProperties();原来在同一个Tomcat中还运行了另外一个系统,那个系统使用的邮件服务器是采用SSL验证发送接收邮件的,而且30分钟会收取一次邮件,由于程序写的不够严谨,就出现以上的错误,改进了程序后,两个系统终于可以并存于一个TOMCAT中。[@more@]Java mail ssl并存的一个问题

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21107256/viewspace-1018785/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Mail SSL 是用于在 Java 程序中发送和接收电子邮件的安全传输协议。它使用 SSL(Secure Sockets Layer)协议来加密和保护邮件的传输,确保邮件在传输过程中不被窃取或篡改。Java Mail SSL 可以与各种邮件服务提供商的 SMTP 和 IMAP 服务器一起使用,例如 Gmail、Outlook 等。要使用 Java Mail SSL,您需要在 Java 程序中添加相应的库和配置文件,并设置正确的 SMTP 和 IMAP 服务器地址、端口、用户名和密码等信息。以下是一个简单的 Java Mail SSL 示例代码,演示如何使用 Gmail SMTP 服务器发送电子邮件: ```java import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class JavaMailSSLExample { public static void main(String[] args) { final String username = "your-email@gmail.com"; final String password = "your-password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your-email@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email@example.com")); message.setSubject("Java Mail SSL Example"); message.setText("This is an example email sent using Java Mail SSL."); Transport.send(message); System.out.println("Email sent successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例代码中,我们使用 Gmail SMTP 服务器发送电子邮件。您需要将 `your-email@gmail.com` 和 `your-password` 替换为您自己的 Gmail 邮箱地址和密码,将 `recipient-email@example.com` 替换为您要发送电子邮件的收件人地址。运行代码后,您应该能在控制台中看到输出 `Email sent successfully.` 表示电子邮件已成功发送。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值