Java 項目中常常回遇到發送郵件
Java 發送郵件有幾種,今天先給大家介紹用 HtmlEmail 來發送郵件,我這里是用 Maven 來搭建的
HtmlEmail 可以抄帶HTML
首先 需要導入jar 包
1
2
3
4
5
|
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version> 1.4 </version> </dependency> |
然后我們來建立一個發送郵件的 Mail 類 JavaBean
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
|
public class Mail implements Serializable{ private static final long serialVersionUID = -6390720891150157552L; public static final String ENCODEING = "UTF-8" ; // 服務器地址 private String host; // 發件人的郵箱 private String sender; // 發件人昵稱 private String name; // 賬號 private String username; // 密碼 private String password; // 收件人的郵箱 private String receiver; // 收件人的名稱 private String receiverName; // 收件人的郵箱(key)和名稱(value) private Map<String, String> to; // 抄送人的郵箱(key)和名稱(value) private Map<String, String> cc; // 秘密抄送人的郵箱(key)和名稱(value) private Map<String, String> bcc; // 主題 private String subject; // 信息(支持HTML) private String message; public String getHost() { return host; } public void setHost(String host) { this .host = host; } public String getSender() { return sender; } public void setSender(String sender) { this .sender = sender; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this .receiver = receiver; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this .subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public String getReceiverName() { return receiverName; } public void setReceiverName(String receiverName) { this .receiverName = receiverName; } public Map<String, String> getTo() { return to; } public void setTo(Map<String, String> to) { this .to = to; } public Map<String, String> getCc() { return cc; } public void setCc(Map<String, String> cc) { this .cc = cc; } public Map<String, String> getBcc() { return bcc; } public void setBcc(Map<String, String> bcc) { this .bcc = bcc; } } |
然后再來創建一個發送郵件的工具類 MailUtil
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
106
107
108
109
|
public class MailUtil { public Boolean send(Mail mail){ HtmlEmail email = new HtmlEmail(); try { // 這里是SMTP發送服務器的名字:163的如下:"smtp.163.com" email.setHostName(mail.getHost()); // 字符編碼集的設置 email.setCharset(Mail.ENCODEING); // 發送人的郵箱 email.setFrom(mail.getSender(), mail.getName()); // 如果需要認證信息的話,設置認證:用戶名-密碼。分別為發件人在郵件服務器上的注冊名稱和密碼 email.setAuthentication(mail.getUsername(), mail.getPassword()); // 設置收件人信息 setTo(email, mail); // 設置抄送人信息 setCc(email, mail); // 設置密送人信息 setBcc(email, mail); // 要發送的郵件主題 email.setSubject(mail.getSubject()); // 要發送的信息,由于使用了HtmlEmail,可以在郵件內容中使用HTML標簽 email.setHtmlMsg(mail.getMessage()); // 發送 email.send(); if (Log.isDebugEnabled()) { Log.info(mail.getSender() + " 發送郵件到 " + mail.getReceiver()); } return true ; } catch (Exception e) { e.printStackTrace(); Log.info(mail.getSender() + " 發送郵件到 " + mail.getReceiver() + " 失敗" ); return false ; } } /** * 設置收件人信息 * * @param email * @param mail * @throws EmailException */ private void setTo(HtmlEmail email, Mail mail) throws EmailException{ // 收件人不為空 if (StringUtils.isNotEmpty(mail.getReceiver())) { // 收件人名稱不為空 if (StringUtils.isNotEmpty(mail.getReceiverName())) { email.addTo(mail.getReceiver(), mail.getReceiverName()); } else { email.addTo(mail.getReceiver()); } } // 收件人 Map 不為空 if (mail.getTo() != null ) { for (Map.Entry<String, String> entry : mail.getTo().entrySet()) { // 收件人名稱不為空 if (StringUtils.isNotEmpty(entry.getValue())) { email.addTo(entry.getKey(), entry.getValue()); } else { email.addTo(entry.getKey()); } } } } /** * 設置抄送人信息 * * @param email * @param mail * @throws EmailException */ private void setCc(HtmlEmail email, Mail mail) throws EmailException{ // 抄送人 Map 不為空 if (mail.getCc() != null ) { for (Map.Entry<String, String> entry : mail.getCc().entrySet()) { // 抄送人名稱不為空 if (StringUtils.isNotEmpty(entry.getValue())) { email.addCc(entry.getKey(), entry.getValue()); } else { email.addCc(entry.getKey()); } } } } /** * 設置密送人信息 * * @param email * @param mail * @throws EmailException */ private void setBcc(HtmlEmail email, Mail mail) throws EmailException{ // 密送人 Map 不為空 if (mail.getBcc() != null ) { for (Map.Entry<String, String> entry : mail.getBcc().entrySet()) { // 密送人名稱不為空 if (StringUtils.isNotEmpty(entry.getValue())) { email.addBcc(entry.getKey(), entry.getValue()); } else { email.addBcc(entry.getKey()); } } } } } |
寫到這里,大概也就差不多了,萬里長征只有最后一步了
測試,測試,測試
這里編寫一個 junit 方法來測試
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
|
@Test public void sendMail(){ Mail mail = new Mail(); // 設置郵件服務器 mail.setHost( "smtp.exmail.qq.com" ); // 發件人郵件地址 // 發件人名稱 mail.setName( "Java.小學生" ); // 登錄賬號,一般都是和郵箱名一樣吧 // 發件人郵箱的登錄密碼 mail.setPassword( "xxxxxxxx" ); // 接收人 mail.setReceiverName( "我要女票" ); mail.setSubject( "萬里長征最后一步測試" ); String html = "<!DOCTYPE html>" ; html += "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" ; html += "<title>Insert title here</title>" ; html += "</head><body>" ; html += "<div style=\"width:600px;height:400px;margin:50px auto;\">" ; html += "<h1>我來看看郵件是否發送成功呢</h1><br/><br/>" ; html += "<p>下面是通過該協議可以創建一個指向電子郵件地址的超級鏈接,通過該鏈接可以在Internet中發送電子郵件</p><br/>" ; html += "<a href=\"mailto:[email protected]?subject=test&[email protected]&body=use mailto sample\">send mail</a>" ; html += "</div>" ; html += "</body></html>" ; mail.setMessage(html); new MailUtil().send(mail); } |
趕緊登錄郵箱看看吧
來看看這里 主題 內容,發送人名稱和收件人名稱是否和代碼寫的一樣呢,如果一樣的話,恭喜你成功啦
到這,其實發送郵件就算完了!!!
這里在啰嗦最后一個小知識點 mailto
mailto: 通過該協議可以創建一個指向電子郵件地址的超級鏈接,通過該鏈接可以在Internet中發送電子郵件
我們點擊 send mail 會打開你郵件客戶端一個發送界面,如果你有寫抄帶信息,也會給你帶上
以上這篇Java HtmlEmail 郵件發送的簡單實現代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。