将JavaMail与TLS一起使用 [英] Using JavaMail with TLS

查看:245
本文介绍了将JavaMail与TLS一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上发现了有关JavaMail API和通过SMTP服务器发送邮件的其他几个问题,但没有人讨论过使用TLS安全性。我正在尝试使用JavaMail通过我的工作SMTP邮件服务器向我自己发送状态更新,但它需要TLS,我无法在线找到有关如何使用JavaMail访问需要TLS加密的SMTP服务器的任何示例。任何人都可以帮忙解决这个问题吗?

I found several other questions on SO regarding the JavaMail API and sending mail through an SMTP server, but none of them discussed using TLS security. I'm trying to use JavaMail to send status updates to myself through my work SMTP mail server, but it requires TLS, and I can't find any examples online of how to use JavaMail to access an SMTP server that requires TLS encryption. Can anyone help with this?

推荐答案

我们的产品实际上有一些通知代码,如果可用,我们会使用TLS发送邮件。

We actually have some notification code in our product that uses TLS to send mail if it is available.

您需要设置Java Mail属性。您只需要TLS,但如果您的SMTP服务器使用SSL,则可能需要SSL。

You will need to set the Java Mail properties. You only need the TLS one but you might need SSL if your SMTP server uses SSL.

Properties props = new Properties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");  // If you need to authenticate
// Use the following if you need SSL
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

然后,您可以将其传递给JavaMail会话或任何其他会话实例化器,如 Session.getDefaultInstance(道具)

You can then either pass this to a JavaMail Session or any other session instantiator like Session.getDefaultInstance(props).

这篇关于将JavaMail与TLS一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆