通过腾讯邮件服务器发送HTML邮件

邮件发送工具:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知青先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值