package com.fr.function;

import java.io.IOException;
import java.security.Security;
import java.util.Date;
import java.util.Properties; import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory; public class MailTest {
public static void main(String[] args) throws Exception {
MailUtil.sendEmil465("*****@qq.com", "国家能源集团邮箱测试4");
} /**
* 使用加密的方式,利用465端口进行传输邮件,开启ssl
* @param to 为收件人邮箱
* @param message 发送的消息
*/
public static void sendEmil465(String to, String message) {
try {
final String smtpHost="mail.chnenergy.com.cn";
final String smtpPort="465";
final String username = "****@chnenergy.com.cn";
final String password = "****";
final String subject="国家能源集团邮件发送测试5"; Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); //设置邮件会话参数
Properties props = new Properties();
//邮箱的发送服务器地址
props.setProperty("mail.smtp.host", smtpHost);
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false"); //SSL认证,注意腾讯邮箱是基于SSL加密的,所有需要开启才可以使用,很多人不成功是因为漏写了下面的代码
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sf);
props.setProperty("mail.smtp.ssl.enable", "true"); //邮箱发送服务器端口,这里设置为465端口
props.setProperty("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
props.put("mail.smtp.auth", "true"); //获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
//通过会话,得到一个邮件,用于发送
Message msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(username));
//设置收件人,to为收件人,cc为抄送,bcc为密送
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
//msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
//msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
msg.setSubject(subject);
//设置邮件消息
msg.setText(message);
//设置发送的日期
msg.setSentDate(new Date()); //调用Transport的send方法去发送邮件
Transport.send(msg); } catch (Exception e) {
e.printStackTrace();
} }
}

  

465端口失败原因,注释

In earlier releases it was necessary to explicitly set a socket
factory property to enable use of SSL. In almost all cases, this
is no longer necessary. SSL support is built in. However, there
is one case where a special socket factory may be needed. JavaMail now includes a special SSL socket factory that can simplify
dealing with servers with self-signed certificates. While the
recommended approach is to include the certificate in your keystore
as described above, the following approach may be simpler in some cases. The class com.sun.mail.util.MailSSLSocketFactory can be used as a
simple socket factory that allows trusting all hosts or a specific set
of hosts. For example: MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
// or
// sf.setTrustedHosts(new String[] { "my-server" });
props.put("mail.smtp.ssl.enable", "true");
// also use following for additional safety
//props.put("mail.smtp.ssl.checkserveridentity", "true");
props.put("mail.smtp.ssl.socketFactory", sf); Use of MailSSLSocketFactory avoids the need to add the certificate to
your keystore as described above, or configure your own TrustManager
as described below.(使用MailSSLSocketFactory避免了需要添加证书,你的密钥库如上所述,或配置自己的TrustManager。如下所述。

 转载自:https://blog.csdn.net/allen_zs/article/details/50753311

最新文章

  1. git无法定位程序输入点libiconv
  2. nginx性能优化之线程池
  3. LC.exe exited with code -1 报错
  4. linux 安装sysstat使用iostat、mpstat、sar、sa(转载)
  5. jquery中 cache: true和false的区别
  6. Java-如何去掉JFrame上的最大化最小化和关闭按钮(转)
  7. md5校验问题
  8. 离线安装 Cloudera Manager 5 和 CDH5.10
  9. Oracle存储过程跨用户执行查询报错
  10. 001_JS基础_JavaScript简介
  11. Linq 生成运算符 Empty,Range,Repeat
  12. struts2 令牌 实现源代码 JSP
  13. Kubernetes 笔记 05 yaml 配置文件详解
  14. Mouse Without Borders软件,主要功能备忘录
  15. JAVA数组与List相互转换
  16. linux常用命令:/etc/group文件详解
  17. uDig配图与GeoServer添加Style
  18. Vmstat主要关注哪些数据?
  19. 安装Telerik JustMock插件后启动不成功
  20. python编程os、os.path 模块中关于文件、目录常用的函数使用方法

热门文章

  1. 【函数】wm_concat包的订制
  2. 关于git报 warning: LF will be replaced by CRLF in README.md.的警告的解决办法
  3. mysql多实例启动过程
  4. 【Code Tools】AB性能测试工具(一)
  5. linux下给U盘分区&制作文件系统
  6. ThreadLocal(在一个线程中共享数据)
  7. Python应用之-file 方法
  8. 【Selenium-WebDriver实战篇】ScreenRecorder的实际输出路径设置(转)
  9. Hive中的SQL执行计划--几乎所有的SQL都有
  10. 10 分钟了解 Actor 模型