邮件发送工具:

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);
}

通过腾讯邮件服务器发送HTML邮件的更多相关文章

  1. 用 Apache James 搭建邮件服务器来收发邮件实践(一)(转)

    Apache James 简称 James, 是 Java Apache Mail Enterprise Server的缩写.James 是100%基于Java的电子邮件服务器.它是一种独立的邮件服务 ...

  2. Java远程调用邮件服务器,实现邮件发送

    写这篇文章的背景是公司Android客户端需要实现一个功能,实现类似于密码找回或者用户注册完发送一个邮件给用户的功能,当然这些逻辑客户端只负责请求自己的服务端,自己的服务端再去请求邮件服务器. 邮件服 ...

  3. 手动搭建apache james邮件服务器,实现邮件功能

    最近一直在搞邮件这块,本来我们邮件发送是用的腾讯免费的企业邮箱,邮件功能没有问题,但是由于邮件的限制,如下: 这些限制导致我们的部分客户是收不到邮件的,哪怕付费,这样的固定频率限制也是无法解决的,可以 ...

  4. zabbix配置发送报警邮件

    配置邮件分为两种情况: 第一种:使用远端邮件服务器发送报警邮件 Linux系统版本:CentOS6.5-64 下载mailx: http://nchc.dl.sourceforge.net/proje ...

  5. centos7搭建postfix邮件服务器

    在使用qq等邮件服务器厂商提供的邮件服务后,发现他们的邮件发送数量是有限制的,随着公司的业务的需求下,我们需要搭建一个邮件服务器,邮件服务器可以帮助我们在一些提醒方面和消息推送方面起到帮助. 理论性语 ...

  6. JavaWeb网上商城项目中用户注册,使用MailServer和FoxMail搭建本地邮件服务器

    下载并安装易邮邮件服务器MailServer和腾讯邮箱FoxMail,下载地址  https://download.csdn.net/download/checkerror2/10130538 具体步 ...

  7. 戴尔dell服务器硬件故障SMTP邮件报警idrac配置

    上次公司有台戴尔的服务器硬盘故障了却没有及时发现,后面就研究了一下看到戴尔的idrac有硬件SMTP报警功能 然后自己折腾了一下,一开始配置不成功,后面问了一下戴尔官方的最好自己再摸索了一下解决了,做 ...

  8. 烂泥:Postfix邮件服务器搭建之软件安装与配置

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb Postfix邮件服务器的搭建需要使用到几个软件,分别是cyrus-sasl.postf ...

  9. 转-阿里云CentOS Linux服务器上用postfix搭建邮件服务器

    http://www.cnblogs.com/dudu/archive/2012/12/12/linux-postfix-mailserver.html 注:本文的邮件服务器只用于发送邮件,也就是ST ...

随机推荐

  1. C++2.0新特性(三)——<=default,=delete、alias(别名)、noexcept、override、final、以及和const对比>

    一.=default,=delete 1.首先我们要回顾一下类默认函数的概念: C++中,当我们设计与编写一个类时,若不显著申明,则类会默认为我们提供如下几个函数: (1)构造函数(A()).(2)析 ...

  2. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

  3. DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比

    DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比 参考:https://www.cnblogs.com/xishuai/p/36702 ...

  4. linux lnmp安装2个版本PHP教程

    linux lnmp安装2个版本PHP教程我原先装了5.6版本的PHP 后来想装个PHP7.0.14版本 一方面看看稳定性 另一方面看看性能怎么样 其实原理很简单 php-fpm开启了1个端口来管理P ...

  5. ubuntu 17.04 下搭建深度学习环境

    .目前使用CPU即可,先不需要显卡配置 .使用pip3 安装深度学习框架 .要先安装pip3 #sudo apt install python3-pip https://blog.csdn.net/b ...

  6. Python3版本号比较代码实现

    一.版本号比较的困难 不能直接以字符串形式进行比较:对于1.3和1.4直接以字符串进行比较是可以正确得出1.4比1.3大:但如果是1.3和1.14还直接进字符串比较那就是1.3比1.14大那就不对了. ...

  7. 【leet-code】接雨水

    给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 ...

  8. Sitecore 8.2 防火墙规则的权威指南

    如今,使用多层安全保护您的数据不再是奢侈品; 这是不容谈判的.此外,您需要确保Sitecore解决方案保持运行并与集成服务(例如SQL,Mongo,Solr)通信,同时保持相同的安全级别. 让我们假设 ...

  9. zk脑裂

    一.为什么zookeeper要部署基数台服务器?二.zookeeper脑裂(Split-Brain)问题2.1.什么是脑裂?2.2.什么原因导致的?2.2.zookeeper是如何解决的?一.为什么z ...

  10. Spring Security 解析(四) ——短信登录开发

    Spring Security 解析(四) -- 短信登录开发   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security ...