posts - 710,  comments - 81,  views - 260万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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端口失败原因,注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

posted on   itprobie-菜鸟程序员  阅读(492)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2017-11-28 Windows系统崩溃后快速恢复Oracle数据库的妙招
2017-11-28 Oracle管理表空间和数据文件详解
2017-11-28 Oracle 表空间和数据文件之间的关系
2017-11-28 Oracle Telnet 1521 失败
2015-11-28 SQL Server 问题之 排序规则(collation)冲突
点击右上角即可分享
微信分享提示