邮件Demo(SSL加密传输)

简介: private final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; private String smtpServer; // SMTP服务器地址 private String port; // 端口 private String username; // 登录SMTP服务器的用户名 private String password; // 登录SMTP服务器的密码 private List<String> recipients = new ArrayList<String>(); // 收件人地址集合

package javamail.zifangsky.com;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Multipart;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimeUtility;

public class SendMailBySSL {

 private final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

 private String smtpServer; // SMTP服务器地址

 private String port; // 端口

 private String username; // 登录SMTP服务器的用户名

 private String password; // 登录SMTP服务器的密码

 private List<String> recipients = new ArrayList<String>(); // 收件人地址集合

 private String subject; // 邮件主题

 private String content; // 邮件正文

 private List<String> attachmentNames = new ArrayList<String>(); // 附件路径信息集合

 public SendMailBySSL() {

 }

 public SendMailBySSL(String smtpServer, String port, String username,

     String password, List<String> recipients, String subject,

     String content, List<String> attachmentNames) {

   this.smtpServer = smtpServer;

   this.port = port;

   this.username = username;

   this.password = password;

   this.recipients = recipients;

   this.subject = subject;

   this.content = content;

   this.attachmentNames = attachmentNames;

 }

 public void setSmtpServer(String smtpServer) {

   this.smtpServer = smtpServer;

 }

 public void setPort(String port) {

   this.port = port;

 }

 public void setUsername(String username) {

   this.username = username;

 }

 public void setPassword(String password) {

   this.password = password;

 }

 public void setRecipients(List<String> recipients) {

   this.recipients = recipients;

 }

 public void setSubject(String subject) {

   this.subject = subject;

 }

 public void setContent(String content) {

   this.content = content;

 }

 public void setAttachmentNames(List<String> attachmentNames) {

   this.attachmentNames = attachmentNames;

 }

 /**

  * 进行base64加密,防止中文乱码

  * */

 public String changeEncode(String str) {

   try {

     str = MimeUtility.encodeText(new String(str.getBytes(), "UTF-8"),

         "UTF-8", "B"); // "B"代表Base64

   } catch (UnsupportedEncodingException e) {

     e.printStackTrace();

   }

   return str;

 }

 /**

  * 正式发邮件

  * */

 public boolean sendMail() {

   Properties properties = new Properties();

   properties.put("mail.smtp.host", smtpServer);

   properties.put("mail.smtp.auth", "true");

   properties.put("mail.smtp.socketFactory.class", SSL_FACTORY); //使用JSSE的SSL socketfactory来取代默认的socketfactory

   properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理

                               

   properties.put("mail.smtp.port", port);

   properties.put("mail.smtp.socketFactory.port", port);

   Session session = Session.getInstance(properties);

   session.setDebug(true);

   MimeMessage message = new MimeMessage(session);

   try {

    // 发件人

     Address address = new InternetAddress(username);

     message.setFrom(address);

    // 收件人

     for (String recipient : recipients) {

       System.out.println("收件人:" + recipient);

       Address toAddress = new InternetAddress(recipient);

       message.setRecipient(MimeMessage.RecipientType.TO, toAddress); // 设置收件人,并设置其接收类型为TO

       /**

        * TO:代表有健的主要接收者。 CC:代表有健的抄送接收者。 BCC:代表邮件的暗送接收者。

        * */

     }

    // 主题

     message.setSubject(changeEncode(subject));

    // 时间

     message.setSentDate(new Date());

     Multipart multipart = new MimeMultipart();

    // 添加文本

     BodyPart text = new MimeBodyPart();

     text.setText(content);

     multipart.addBodyPart(text);

    // 添加附件

     for (String fileName : attachmentNames) {

       BodyPart adjunct = new MimeBodyPart();

       FileDataSource fileDataSource = new FileDataSource(fileName);

       adjunct.setDataHandler(new DataHandler(fileDataSource));

       adjunct.setFileName(changeEncode(fileDataSource.getName()));

       multipart.addBodyPart(adjunct);

     }

    // 清空收件人集合,附件集合

     recipients.clear();

     attachmentNames.clear();

     message.setContent(multipart);

     message.saveChanges();

   } catch (Exception e) {

     e.printStackTrace();

     return false;

   }

   try {

     Transport transport = session.getTransport("smtp");

     transport.connect(smtpServer, username, password);

     transport.sendMessage(message, message.getAllRecipients());

     transport.close();

   } catch (Exception e) {

     e.printStackTrace();

     return false;

   }

   return true;

 }

 public static void main(String[] args) {

   List<String> recipients = new ArrayList<String>();

//    recipients.add("123456789@qq.com");

   recipients.add("admin@zifangsky.cn");

   String subject = "这封邮件是为了测试SMTP的SSL加密传输";

   String content = "这是这封邮件的正文";

   List<String> attachmentNames = new ArrayList<String>();

   attachmentNames.add("C://Users//Administrator//Desktop//kali.txt");

   SendMailBySSL sendMailBySSL = new SendMailBySSL("smtp.163.com", "465",

       "youname@163.com", "youpassword", recipients, subject, content,

       attachmentNames);

   sendMailBySSL.sendMail();

 }

}


目录
相关文章
|
26天前
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
|
30天前
|
网络协议 安全 网络安全
DNS服务器加密传输
【8月更文挑战第18天】
56 15
|
1月前
|
安全 Nacos 数据安全/隐私保护
【技术干货】破解Nacos安全隐患:连接用户名与密码明文传输!掌握HTTPS、JWT与OAuth2.0加密秘籍,打造坚不可摧的微服务注册与配置中心!从原理到实践,全方位解析如何构建安全防护体系,让您从此告别数据泄露风险!
【8月更文挑战第15天】Nacos是一款广受好评的微服务注册与配置中心,但其连接用户名和密码的明文传输成为安全隐患。本文探讨加密策略提升安全性。首先介绍明文传输风险,随后对比三种加密方案:HTTPS简化数据保护;JWT令牌减少凭证传输,适配分布式环境;OAuth2.0增强安全,支持多授权模式。每种方案各有千秋,开发者需根据具体需求选择最佳实践,确保服务安全稳定运行。
79 0
|
1月前
|
SQL 安全 Java
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
174 0
|
2月前
|
安全 网络协议 网络安全
SSL(Secure Sockets Layer)是一种安全协议,用于在客户端和服务器之间建立加密的通信通道。
SSL(Secure Sockets Layer)是一种安全协议,用于在客户端和服务器之间建立加密的通信通道。
|
2月前
|
安全 算法 Java
Java中的数据加密与安全传输
Java中的数据加密与安全传输
|
3月前
|
存储 安全 算法
RSA非对称加密算法中的密钥对生成与传输
RSA非对称加密算法的密钥对生成与传输是信息安全领域的核心问题之一。密钥生成过程需要保证随机性和安全性,而密钥的传输则需要选择适当的方式来确保其保密性和完整性。通过合理的密钥管理和保护措施,可以有效地利用RSA算法保护通信安全,防止信息泄露和篡改。在实际应用中,用户和系统管理员需要结合具体情况选择最佳的密钥生成和传输策略,以达到最佳的安全性和效率。
|
3月前
|
安全 网络安全
如何给网站添加ssl安全证书
如何给网站添加ssl安全证书
81 1
|
3天前
|
运维 安全 数据建模
阿里云SSL证书收费版和免费版SSL有什么区别?全方位对比
阿里云提供免费和个人测试(Pro)及正式版SSL证书。免费版有效期仅3个月,适合个人网站或测试使用;个人测试(Pro)版68元/年,有效期12个月;正式版价格数百至数千元不等,有效期至少1年,支持DV、OV、EV证书类型,具有高安全等级、良好兼容性、稳定OCSP验证、SLA保障及安全保险赔付等优势,并提供最长3年的服务周期与人工客服支持。详情与报价参见SSL官方页面。
51 20
|
16天前
|
运维 安全 数据建模
阿里云免费SSL证书和收费版SSL证书有什么区别?
阿里云提供免费与收费SSL证书,前者有效期仅3个月,适合个人网站或测试使用;后者有效期至少1年,具备更高安全等级、良好兼容性及OCSP验证稳定性等优势,适用于企业网站,尤其政府、金融等领域建议选用OV或EV型证书以确保数据与身份认证安全。详细了解与报价请访问SSL证书官方页面。
116 2