java 465端口发送邮件
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端口发送邮件的更多相关文章
- jumpservice使用465端口发送邮件
阿里云.华为云等云服务器默认屏蔽掉了25端口后,内网服务器如何通过跳板机发送邮件到外网邮箱. 如果是可联网的阿里云机器,可以直接配置mailx使用465端口发送邮件.需要进行ssl验证配置. Cent ...
- 阿里云 Linux 启用465端口发送邮件
阿里云 Linux 启用465端口发送邮件 环境:阿里云 Linux Centos 7.4 x64 注:阿里云默认禁用25邮件端口,需要启动465端口加密进行邮件发送. 注:确保邮箱开启SMTP服务, ...
- centos上mailx通过465端口发送邮件
最近在看zabbix发送邮件的时候,发现自己的邮件总是无法发送,这里可能是外网防火墙禁止25端口,那么如何绕过25端口呢? 我使用的是163邮箱的TSL加密协议465端口 由于mailx基本配置很简 ...
- Spring Boot 使用465端口发送邮件
2017年10月27日 15:04:24 伊宇紫 阅读数:2710 标签: 465端口邮件springboot 更多 个人分类: Java 版权声明:本文为博主原创文章,未经博主允许不得转载. h ...
- CentOS使用465端口发送邮件
1)邮件发送示例 方法1:echo "This is a test mail" | mail -s '邮件测试' 452666750@qq.com 方法2:mail -s '服务运 ...
- python3使用465端口发送邮件来解决阿里云封闭25端口问题
import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddr #发件人邮箱账号my_send ...
- mailx使用465端口发邮件
centos上mailx通过465端口发送邮件 最近在看zabbix发送邮件的时候,发现自己的邮件总是无法发送,这里可能是外网防火墙禁止25端口,那么如何绕过25端口呢? 我使用的是163邮箱的 ...
- 使用JavaMail发送邮件,465端口开启ssl加密传输
package com.wangxin.test; import java.security.Security; import java.util.Date; import java.util.Pro ...
- 阿里云ECS屏蔽25端口,官方建议使用465 SSL端口发送邮件
阿里云ECS VPC网络,搭建了zabbix,想通过三方邮件系统发送邮件,本机开虚拟机测试发邮件一切正常,到阿里ECS的时候邮件各种发不出去,到处找原因,最后度娘告诉了我真想,原来阿里把25端口屏蔽 ...
随机推荐
- mysql主从一致性校验工具-pt
一.环境 1.系统环境 系统 IP 主机名 说明 server_id centos6.7 MasterIP master 数据库:主 177 centos6.7 SlaveIP slave 数据库: ...
- SQL SERVER- waitresource解读
例如: OBJECT: 18:1769220894:8 第一个是dbid:18,第二个是objectid:1769220894,第三个是indexID:8 SELECT DB_NAME(18)SELE ...
- 定时任务模块——APScheduler
一.概念: python定时任务框架,基于日期,固定时间间隔,crontab类型的任务,并且可以持久化任务,并能以deamon守护方式运行任务 二.简介: 安装:pip install apsched ...
- ArcGIS pro 发布地图服务(一)动态地图服务
1.软件:arcgis pro 2.4 数据:.mxd文档. 2.导入mxd文档. 3.登录portal账号 4.分析—发布 5.在server中的地图服务 JavaScript api 查看 6. ...
- JQuery DOM操作(属性操作/样式操作/文档过滤)
jQuery——入门(三)JQuery DOM操作(属性操作/样式操作/文档过滤) 一.DOM属性操作 1.属性 (1).attr() 方法 语法:$(selector).attr(name|prop ...
- Python爬虫入门——使用requests爬取python岗位招聘数据
爬虫目的 使用requests库和BeautifulSoup4库来爬取拉勾网Python相关岗位数据 爬虫工具 使用Requests库发送http请求,然后用BeautifulSoup库解析HTML文 ...
- 获取国定字符的内容split
a="Time:20190822_111655_554 Start Cloud new case, Num=1, Input=/data/voice/20190725_035326_2_vo ...
- GSM/GPRS模块 AT指令集C语言编程——基于有方M660+和MSP430单片机
GSM/GPRS芯片是手机中负责收发短信.拨打电话以及访问GPRS网络的核心器件.有方M660+为深圳有方公司生产的一款超小封装的GSM/GPRS工业无线模块,可以提供高品质的语音.短信.数据业务等功 ...
- docker容器中oracle数据库导出dmp文件
Oracle数据库安装在docker容器中 1首先查看容器 docker ps 2进入oracle容器 docker exec -it 7f0f3f2d4f88 /bin/bash 3导出整个库:这个 ...
- 09-Flutter移动电商实战-移动商城数据请求实战
1.URL接口管理文件建立 第一步需要在建立一个URL的管理文件,因为课程的接口会一直进行变化,所以单独拿出来会非常方便变化接口.当然工作中的URL管理也是需要这样配置的,以为我们会不断的切换好几个服 ...