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端口屏蔽 ...
随机推荐
- element中日期时间插件(DateTimePicke) el-date 开始时间大于等于当前时间小于结束时间,结束时间大于开始时间且大于当前时间
pickerOptions1: { disabledDate: time => { if (this.endTime) { return ( time.getTime() > new Da ...
- MYSQL使用source命令,导入SQL文件
命令 source D:/student.sql
- Kali下的内网劫持(一)
ettercap利用计算机在局域网内进行通信的ARP协议的缺陷进行攻击,在目标主机与服务器之间充当中间人,嗅探两者之间的数据流量,从中窃取用户的数据信息,那么接下来我就给大家演示一下客户端的图片是怎么 ...
- 关于srping的AOP事务管理问题,自定义切面是否导致事务控制失效
applicationContext.xml: <!-- 方法调用时间记录 --> <bean id="methodExecuteTime" class=&quo ...
- AT2000 Leftmost Ball
设\(f[i][j]\)表示当前有\(i\)个白球,一共放完了\(j\)种球 显然有\(j <= i\) 对于每个状态目前已经放下去的球是固定了的,那么考虑转移 放白球 从\(f[i - 1][ ...
- vue cli 框架搭建
=============== 通知: 博主已迁至<掘金>码字,博客园可能以后不再更新,掘金地址:https://juejin.im/post/5a1a6a6551882534af25a8 ...
- MERGE引擎 分表后 快速查询所有数据
MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询.构成一个MERGE数据表结构的各成员MyISAM数据表必须具有完全一样的结构.每一个成员数据表的数据列必 ...
- Tortoise Git 安装 及报错处理
TortoiseGit安装详解: https://www.cnblogs.com/xinlj/p/5978730.html Tortoise Git 错误处理 disconnected no supp ...
- VS2012 VS2010 VTK引入设置
1.C/C++ ---> 附加包含的目录 F:/VTK61/VTK-6.1.0/SLN/Filters/Sources F:/VTK61/VTK-6.1.0/VTK-6.1.0/Filters/ ...
- 机房断电,导致xfs文件系统损坏
记一次机房断电,导致xfs文件系统损坏处理方法 挂载时报以下错误: mount: mount /dev/sdb on /dev/sdb failed: Structure needs cleaning ...