邮件发送工具:

private static String host = "smtp.exmail.qq.com";// 服务器地址
private static String port = "465";// 端口
private static String protocol = "smtp";// 协议

public static Session initProperties(String account, String password) {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", protocol);
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
// 使用smtp身份验证
properties.put("mail.smtp.auth", "true");
// 开启安全协议
MailSSLSocketFactory mailSSLSocketFactory = null;
try {
mailSSLSocketFactory = new MailSSLSocketFactory();
mailSSLSocketFactory.setTrustAllHosts(true);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
properties.put("mail.smtp.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
properties.put("mail.smtp.socketFactory.port", port);
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(account, password);
}
});
session.setDebug(true);
return session;
}
/**
* @Description: 发送邮件
* @Title: send
* @date 2019-10-08 17:12
* @param @param sender发件人别名
* @param @param subject邮件主题
* @param @param content接收者列表,多个接收者之间用","隔开
* @param @param receiverList
* @param @param fileSrc 附件地址
* @return void 返回类型
* @throws @return void
* @param sender
* @param subject
* @param content
* @param receiverList
* @param fileSrc
*/
public void send(String sender, String subject, String content, String receiverList, String fileSrc, String account,
String password) {
try {
Session session = initProperties(account, password);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(account, sender));// 发件人,可以设置发件人的别名
// 收件人,多人接收
InternetAddress[] internetAddressTo = new InternetAddress().parse(receiverList);
mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo);
// 主题
mimeMessage.setSubject(subject);
// 时间
mimeMessage.setSentDate(new Date());
// 容器类 附件
MimeMultipart mimeMultipart = new MimeMultipart();
// 附件
MimeBodyPart bodyPart = new MimeBodyPart();
// 设置内容
bodyPart.setContent(content, "text/html; charset=UTF-8");
mimeMultipart.addBodyPart(bodyPart);
// 添加图片&附件
if(fileSrc!=null){

bodyPart = new MimeBodyPart();
bodyPart.attachFile(fileSrc);

}
mimeMultipart.addBodyPart(bodyPart);
mimeMessage.setContent(mimeMultipart);
mimeMessage.saveChanges();
Transport.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

测试类:

public static void main(String[] args) throws Exception {
String account = "xxxxxxxx@xxxxxx.cn";// 登录账户
String password = "xxxxxxxxxxxxxxx";// 登录密码

SendEmailUtil tenMailUtil = new SendEmailUtil();
tenMailUtil.send("发件人别名", "邮件主题", "<h1 style='color:red'>HhhhHHHHHHH</h1>",
"xxxxxx@foxmail.com,xxxxx@xxxxx.cn", "C:/Users/Administrator/Desktop/index.html   as  附件", account,
password);
}

最新文章

  1. 虚函数的使用 以及虚函数与重载的关系, 空虚函数的作用,纯虚函数-&gt;抽象类,基类虚析构函数使释放对象更彻底
  2. 文件处理命令:awk
  3. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
  4. [terry笔记]ora-00904 invalid identifier—同义词
  5. z-index无效
  6. 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求
  7. chrome的timeline的问题?
  8. Android Dialog触摸对话框外部让其消失的实现方法
  9. MySQL行列转换
  10. 利用java反射机制对方法进行调用
  11. ZOJ 3080 ChiBi(spfa)
  12. Spring Boot使用自定义的properties
  13. 大数据学习(6)MapReduce应用
  14. 自学Linux Shell14.2-在脚本中使用其他文件描述符
  15. lnmp环境一键搭建及卸载
  16. 京东原来你运用的这玩意,不错,我也要!! ContainerDNS
  17. JanusGraph Server配置
  18. 杭州优步uber司机第二组奖励政策
  19. Visual studio 2010 TFS地址解析,让团队资源管理器不再显示IP地址
  20. darknet 识别获取结果

热门文章

  1. 【BigData】Java基础_创建一个订单类
  2. 牛逼哄哄的 Lambda 表达式,简洁优雅就是生产力!
  3. window 运行spark报错
  4. cad快捷图标中的启动参数
  5. centos配置chrome+selenium
  6. JWT与RBAC权限模型
  7. 3DESC加密算法
  8. quota - linux磁盘配额管理
  9. pacman 命令详解
  10. 微服务(Microservices)——Martin Flower