首页 归档 关于 文件 Github
×

Java发送邮件

2021-01-29 18:26:49
J-Tools
  • Email
本文总阅读量(次):
本文字数统计(字):613
本文阅读时长(分):3

本文以 Java 发送邮件功能,采用commons-email模式执行

依赖

1
2
3
4
5
6
<!-- 发送邮件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>

代码

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import java.util.List;

/**
* @className: MailUtil.java
* @description: 邮件工具类
* @author: Demon
* @date 2020/10/27 17:09
*/
public class MailUtil {

/**
* 构建器
*/
public static class EmailInfo {

/**
* SMTP 服务器 端口
*/
private String mailServer;
private Integer mailPort;

/**
* 发件人 账号
*/
private String mailNick;

/**
* 发件人 账号
*/
private String mailUsername;

/**
* 发件人 密码
*/
private String mailPassword;

/**
* 邮件标题
*/
private String title;

/**
* 邮件内容
*/
private String content;

/**
* 收件人
*/
private List<String> receivers;

boolean ssl;

public EmailInfo(String mailServer,Integer mailPort,String mailNick,String mailUsername,
String mailPassword,String title,String content,List<String> receivers,boolean ssl) {
this.mailServer = mailServer;
this.mailPort = mailPort;
this.mailNick = mailNick;
this.mailUsername = mailUsername;
this.mailPassword = mailPassword;
this.title = title;
this.content = content;
this.receivers = receivers;
this.ssl = ssl;
}

public MailUtil send() throws Exception {
return new MailUtil(this);
}
}

private MailUtil(EmailInfo emailInfo) throws Exception {
HtmlEmail email = new HtmlEmail();
// 通过Server 发送邮件
email.setHostName(emailInfo.mailServer);
email.setSmtpPort(emailInfo.mailPort);
//设定smtp服务器的认证资料信息
email.setAuthentication(emailInfo.mailUsername, emailInfo.mailPassword);
email.setStartTLSEnabled(false);
email.setSSLOnConnect(emailInfo.ssl);
try {
// 设定发件人
email.setFrom(emailInfo.mailUsername,emailInfo.mailNick);
// 设定收件人 (多人)
int size = emailInfo.receivers.size();
email.addTo(emailInfo.receivers.toArray(new String[size]));
// 设定内容的语言集
email.setCharset("UTF-8");
email.setDebug(false);
// 设定主题
email.setSubject(emailInfo.title);
// 设定邮件内容
email.setHtmlMsg(emailInfo.content);
// 发送邮件
email.send();
System.out.println("邮件发送成功!");
} catch (EmailException e) {
System.out.println("邮件发送失败!");
throw new Exception(e);
}
}
}

设置

新浪

20210129184031
String mailServer = smtp.sinanet.com
Integer mailPort = 25
boolean ssl = false

网易

20210129184347
20210129184412
如若开启了授权密码,请以授权密码当做用户密码使用
String mailServer = smtp.163.com
Integer mailPort = 25
boolean ssl = false

QQ

20210129190030
如若开启了授权密码,请以授权密码当做用户密码使用
String mailServer = smtp.qq.com
Integer mailPort = 465
boolean ssl = true

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Test
public void 测试邮件() {
try {
String content = "<!doctypehtml><html lang=en><meta charset=UTF-8><title>Title</title><h1>测试</h1>";
// 发送邮件信息
String mailServer = "smtp.sinanet.com";
Integer mailPort = 25;
String mailNick = "demon";
String mailUsername = "demon@xxxxxxxxx";
String mailPassword = "123456789";
String title = "本地测试";
List<String> receivers = new ArrayList<>();
receivers.add("1097305826@qq.com");
MailUtil.EmailInfo emailInfo = new MailUtil.EmailInfo(mailServer,mailPort,
mailNick,mailUsername,mailPassword,title, content,receivers,false);
// 发送邮件
emailInfo.send();
} catch (Exception e) {
e.printStackTrace();
}
}
完
Java原生URL模拟工具
Zip4j - 压缩/解压(可设置密码)

本文标题:Java发送邮件

文章作者:十二

发布时间:2021-01-29 18:26:49

最后更新:2021-11-06 17:44:37

原始链接:https://www.zhuqiaolun.com/2021/01/1611916009068/1611916009068/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx6
  • Kafka4
  • Linux2
  • Logger6
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot16
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 一月 20261
  • 十二月 20253
  • 八月 20252
  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:103.2k
本站文章数量统计:139
© 2026 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github