Java - HtmlEmail 郵件發送


Java 項目中常常回遇到發送郵件

Java 發送郵件有幾種,今天先給大家介紹用 HtmlEmail 來發送郵件,我這里是用 Maven 來搭建的

HtmlEmail 可以抄帶HTML

 

首先 需要導入jar 包

1 <dependency>
2     <groupId>org.apache.commons</groupId>
3     <artifactId>commons-email</artifactId>
4     <version>1.4</version>
5 </dependency>

 

然后我們來建立一個發送郵件的 Mail 類 JavaBean

  1 public class Mail implements Serializable{
  2 
  3     private static final long serialVersionUID = -6390720891150157552L;
  4     public static final String ENCODEING = "UTF-8"; 
  5 
  6     // 服務器地址  
  7     private String host;
  8     // 發件人的郵箱  
  9     private String sender; 
 10     // 發件人昵稱  
 11     private String name; 
 12     // 賬號  
 13     private String username; 
 14     // 密碼  
 15     private String password; 
 16     // 收件人的郵箱  
 17     private String receiver;
 18     // 收件人的名稱
 19     private String receiverName;
 20     // 收件人的郵箱(key)和名稱(value)
 21     private Map<String, String> to;
 22     // 抄送人的郵箱(key)和名稱(value)
 23     private Map<String, String> cc;
 24     // 秘密抄送人的郵箱(key)和名稱(value)
 25     private Map<String, String> bcc;
 26     // 主題  
 27     private String subject; 
 28     // 信息(支持HTML)
 29     private String message;
 30 
 31     public String getHost() {
 32         return host;
 33     }
 34     public void setHost(String host) {
 35         this.host = host;
 36     }
 37     public String getSender() {
 38         return sender;
 39     }
 40     public void setSender(String sender) {
 41         this.sender = sender;
 42     }
 43     public String getReceiver() {
 44         return receiver;
 45     }
 46     public void setReceiver(String receiver) {
 47         this.receiver = receiver;
 48     }
 49     public String getName() {
 50         return name;
 51     }
 52     public void setName(String name) {
 53         this.name = name;
 54     }
 55     public String getUsername() {
 56         return username;
 57     }
 58     public void setUsername(String username) {
 59         this.username = username;
 60     }
 61     public String getPassword() {
 62         return password;
 63     }
 64     public void setPassword(String password) {
 65         this.password = password;
 66     }
 67     public String getSubject() {
 68         return subject;
 69     }
 70     public void setSubject(String subject) {
 71         this.subject = subject;
 72     }
 73     public String getMessage() {
 74         return message;
 75     }
 76     public void setMessage(String message) {
 77         this.message = message;
 78     }
 79     public String getReceiverName() {
 80         return receiverName;
 81     }
 82     public void setReceiverName(String receiverName) {
 83         this.receiverName = receiverName;
 84     }
 85     public Map<String, String> getTo() {
 86         return to;
 87     }
 88     public void setTo(Map<String, String> to) {
 89         this.to = to;
 90     }
 91     public Map<String, String> getCc() {
 92         return cc;
 93     }
 94     public void setCc(Map<String, String> cc) {
 95         this.cc = cc;
 96     }
 97     public Map<String, String> getBcc() {
 98         return bcc;
 99     }
100     public void setBcc(Map<String, String> bcc) {
101         this.bcc = bcc;
102     }
103 }

 

然后再來創建一個發送郵件的工具類 MailUtil

  1 public class MailUtil {
  2 
  3     public Boolean send(Mail mail){
  4         HtmlEmail email = new HtmlEmail();
  5         try {
  6             // 這里是SMTP發送服務器的名字:163的如下:"smtp.163.com"  
  7             email.setHostName(mail.getHost());
  8             // 字符編碼集的設置  
  9             email.setCharset(Mail.ENCODEING);
 10             // 發送人的郵箱  
 11             email.setFrom(mail.getSender(), mail.getName());  
 12             // 如果需要認證信息的話,設置認證:用戶名-密碼。分別為發件人在郵件服務器上的注冊名稱和密碼  
 13             email.setAuthentication(mail.getUsername(), mail.getPassword());
 14 
 15             // 設置收件人信息
 16             setTo(email, mail);
 17             // 設置抄送人信息
 18             setCc(email, mail);
 19             // 設置密送人信息
 20             setBcc(email, mail);
 21             // 要發送的郵件主題  
 22             email.setSubject(mail.getSubject());
 23             // 要發送的信息,由於使用了HtmlEmail,可以在郵件內容中使用HTML標簽  
 24             email.setHtmlMsg(mail.getMessage());
 25             // 發送  
 26             email.send();
 27             if (Log.isDebugEnabled()) {
 28                 Log.info(mail.getSender() + " 發送郵件到 " + mail.getReceiver());
 29             }
 30             return true;
 31         } catch (Exception e) {
 32             e.printStackTrace();
 33             Log.info(mail.getSender() + " 發送郵件到 " + mail.getReceiver() + " 失敗");
 34             return false;
 35         }
 36     }
 37 
 38     /**
 39      * 設置收件人信息
 40      * 
 41      * @param email 
 42      * @param mail
 43      * @throws EmailException
 44      */
 45     private void setTo(HtmlEmail email, Mail mail) throws EmailException{
 46         // 收件人不為空
 47         if (StringUtils.isNotEmpty(mail.getReceiver())) {
 48             // 收件人名稱不為空
 49             if (StringUtils.isNotEmpty(mail.getReceiverName())) {
 50                 email.addTo(mail.getReceiver(), mail.getReceiverName());
 51             } else {
 52                 email.addTo(mail.getReceiver());
 53             }
 54         }
 55         // 收件人 Map 不為空
 56         if (mail.getTo() != null) {
 57             for (Map.Entry<String, String> entry : mail.getTo().entrySet()) {
 58                 // 收件人名稱不為空
 59                 if (StringUtils.isNotEmpty(entry.getValue())) {
 60                     email.addTo(entry.getKey(), entry.getValue());
 61                 } else {
 62                     email.addTo(entry.getKey());
 63                 }
 64             }
 65         }
 66     }
 67 
 68     /**
 69      * 設置抄送人信息
 70      * 
 71      * @param email 
 72      * @param mail
 73      * @throws EmailException
 74      */
 75     private void setCc(HtmlEmail email, Mail mail) throws EmailException{
 76         // 抄送人 Map 不為空
 77         if (mail.getCc() != null) {
 78             for (Map.Entry<String, String> entry : mail.getCc().entrySet()) {
 79                 // 抄送人名稱不為空
 80                 if (StringUtils.isNotEmpty(entry.getValue())) {
 81                     email.addCc(entry.getKey(), entry.getValue());
 82                 } else {
 83                     email.addCc(entry.getKey());
 84                 }
 85             }
 86         }
 87     }
 88 
 89     /**
 90      * 設置密送人信息
 91      * 
 92      * @param email 
 93      * @param mail
 94      * @throws EmailException
 95      */
 96     private void setBcc(HtmlEmail email, Mail mail) throws EmailException{
 97         // 密送人 Map 不為空
 98         if (mail.getBcc() != null) {
 99             for (Map.Entry<String, String> entry : mail.getBcc().entrySet()) {
100                 // 密送人名稱不為空
101                 if (StringUtils.isNotEmpty(entry.getValue())) {
102                     email.addBcc(entry.getKey(), entry.getValue());
103                 } else {
104                     email.addBcc(entry.getKey());
105                 }
106             }
107         }
108     }
109 }

 

寫到這里,大概也就差不多了,萬里長征只有最后一步了

測試,測試,測試

 

這里編寫一個 junit 方法來測試

 1     @Test
 2     public void sendMail(){
 3         Mail mail = new Mail();
 4         // 設置郵件服務器 
 5         mail.setHost("smtp.exmail.qq.com");
 6         // 發件人郵件地址
 7         mail.setSender("chenhao@kezhanbang.cn");
 8         // 發件人名稱
 9         mail.setName("Java.小學生");
10         // 登錄賬號,一般都是和郵箱名一樣吧  
11         mail.setUsername("chenhao@kezhanbang.cn");
12         // 發件人郵箱的登錄密碼  
13         mail.setPassword("xxxxxxxx");
14         // 接收人  
15         mail.setReceiver("huntertochen@163.com");
16         mail.setReceiverName("我要女票");
17         mail.setSubject("萬里長征最后一步測試");
18         String html = "<!DOCTYPE html>";
19         html += "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">";
20         html += "<title>Insert title here</title>";
21         html += "</head><body>";
22         html += "<div style=\"width:600px;height:400px;margin:50px auto;\">";
23         html += "<h1>我來看看郵件是否發送成功呢</h1><br/><br/>";
24         html += "<p>下面是通過該協議可以創建一個指向電子郵件地址的超級鏈接,通過該鏈接可以在Internet中發送電子郵件</p><br/>";
25         html += "<a href=\"mailto:huntereagle@foxmail.com?subject=test&cc=huntertochen@163.com&body=use mailto sample\">send mail</a>";
26         html += "</div>";
27         html += "</body></html>";
28         mail.setMessage(html);
29         
30         new MailUtil().send(mail);
31     }

 

趕緊登錄郵箱看看吧

來看看這里 主題 內容,發送人名稱和收件人名稱是否和代碼寫的一樣呢,如果一樣的話,恭喜你成功啦

到這,其實發送郵件就算完了!!!

 

這里在啰嗦最后一個小知識點 mailto

mailto: 通過該協議可以創建一個指向電子郵件地址的超級鏈接,通過該鏈接可以在Internet中發送電子郵件

我們點擊 send mail 會打開你郵件客戶端一個發送界面,如果你有寫抄帶信息,也會給你帶上

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM