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

java 465端口发送邮件的更多相关文章

  1. jumpservice使用465端口发送邮件

    阿里云.华为云等云服务器默认屏蔽掉了25端口后,内网服务器如何通过跳板机发送邮件到外网邮箱. 如果是可联网的阿里云机器,可以直接配置mailx使用465端口发送邮件.需要进行ssl验证配置. Cent ...

  2. 阿里云 Linux 启用465端口发送邮件

    阿里云 Linux 启用465端口发送邮件 环境:阿里云 Linux Centos 7.4 x64 注:阿里云默认禁用25邮件端口,需要启动465端口加密进行邮件发送. 注:确保邮箱开启SMTP服务, ...

  3. centos上mailx通过465端口发送邮件

    最近在看zabbix发送邮件的时候,发现自己的邮件总是无法发送,这里可能是外网防火墙禁止25端口,那么如何绕过25端口呢?  我使用的是163邮箱的TSL加密协议465端口 由于mailx基本配置很简 ...

  4. Spring Boot 使用465端口发送邮件

    2017年10月27日 15:04:24 伊宇紫 阅读数:2710 标签: 465端口邮件springboot 更多 个人分类: Java   版权声明:本文为博主原创文章,未经博主允许不得转载. h ...

  5. CentOS使用465端口发送邮件

    1)邮件发送示例 方法1:echo "This is a test mail" | mail -s '邮件测试' 452666750@qq.com 方法2:mail -s '服务运 ...

  6. python3使用465端口发送邮件来解决阿里云封闭25端口问题

    import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddr #发件人邮箱账号my_send ...

  7. mailx使用465端口发邮件

    centos上mailx通过465端口发送邮件   最近在看zabbix发送邮件的时候,发现自己的邮件总是无法发送,这里可能是外网防火墙禁止25端口,那么如何绕过25端口呢?  我使用的是163邮箱的 ...

  8. 使用JavaMail发送邮件,465端口开启ssl加密传输

    package com.wangxin.test; import java.security.Security; import java.util.Date; import java.util.Pro ...

  9. 阿里云ECS屏蔽25端口,官方建议使用465 SSL端口发送邮件

    阿里云ECS  VPC网络,搭建了zabbix,想通过三方邮件系统发送邮件,本机开虚拟机测试发邮件一切正常,到阿里ECS的时候邮件各种发不出去,到处找原因,最后度娘告诉了我真想,原来阿里把25端口屏蔽 ...

随机推荐

  1. Maven nexus 安装nexus : wrapper | OpenSCManager failed - 拒绝访问。 (0x5)

    在win7中安装nexus时提示:wrapper | OpenSCManager failed - 拒绝访问. (0x5) 主要是没有权限.需要以管理员的身份运行 如果你是直接点击 start-nex ...

  2. 你真的理解CountDownLatch与CyclicBarrier使用场景吗?

    原文:https://blog.csdn.net/zzg1229059735/article/details/61191679 相信每个想深入了解多线程开发的Java开发者都会遇到CountDownL ...

  3. 图森未来一道笔试题-迷宫难题【BFS找S->E的最短步数】

    时间限制:3秒 空间限制:262144K 图森未来的自动驾驶小卡车今天被派到了一个陌生的迷宫内部运输一些货物. 工程师小图已经提前拿到了这个迷宫的地图,地图是一个n*m的字符矩阵,上面包含四种不同的字 ...

  4. Django之路——11 Django用户认证组件 auth

    用户认证 auth模块 1 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1.1 .authenti ...

  5. test20190909 Gluttony

    0+0+0+0+0+0=0.毒瘤出题人. BJOI2019 勘破神机 地灾军团的军师黑袍从潜伏在精灵高层的密探手中得知了神杖的情报,他对奥术宝石中蕴含的远古神秘力量十分感兴趣.他设计夺取了数块奥术宝石 ...

  6. 检测并修改linux服务器日期

    公司的一个应用服务器license到期了,商务上短时间解决不了.只好将服务器的时间调到去年,临时将就一下. 服务器是vmware虚拟机装的centos,日期每隔一段时间会自动同步,百度了好久,也关闭不 ...

  7. 自然数幂和——第一类Stirling数和第二类Stirling数

    第一类Stirling数 首先设 $$S_k(n)=\sum_{i=0}^ni^k$$ 根据第一类斯特林数的定义(P是排列数,C是组合数,s是Stirling) $$C_n^k={P_n^k\over ...

  8. Node.js是什么?提供了哪些内容?

    什么是Node.js? Node.js是基于Chrome V8 引擎的 JavaScript运行时(运行环境). Node.js提供了哪些内容? Node.js运行时,JavaScript代码运行时的 ...

  9. 数据库服务器和web服务器磁盘占用查询

    对于Oracle数据库而言磁盘空间主要体现在表空间上,可使用sql语句进行查看Oracle 表空间的大小及使用情况: select sum(bytes)/1024/1024/1024 "Gb ...

  10. 《OKR工作法》——打造一支专一的团队

    <OKR工作法>在最开始讲了这样一个故事,阿塔兰忒是斯巴达跑的最快的人,她的父亲为了将她嫁出去举办了一场跑步比赛并许诺冠军可以娶自己的女儿,阿塔兰忒为了不结婚决定参加比赛自己拿冠军.然而在 ...