用MailUtils发送邮件

MailUtils,作用是发邮件.底层依赖的是javamail:mail.jar,activation.jars
前提:需要导入封装好的包。最好将信息配置成一个mail信息的配置文件,增加保密性。
   
 1  @Test
 2     public void fun() throws MessagingException, IOException
 3     {
 4         /**
 5          * 1. 登陆右键服务器
 6          *    MailUtils.createSession(服务器地址,登录名,密码)//密码是各户端授权密码
 7          * 2.创建邮件对象
 8          *    发件人
 9          *    收件人
10          *    主题
11          *    正文
12          * 3.发邮件
13          *    需要第一步得到的session、和第二步得到的右键对象
14          */
15         Session session =MailUtils.createSession("smtp.163.com", "******", "******");
16         Mail mail=new Mail("******@163.com", "******@qq.com", "测试邮件一封", "<a href='http://www.baidu.com'>百度</a>");
17         MailUtils.send(session, mail);
18     }

Mail类代码:

  1 /**
  2  * 表示邮件类,你需要设置:账户名和密码、收件人、抄送(可选)、暗送(可选)、主题、内容,以及附件(可选)
  3  * 
  4  * 在创建了Mail对象之后
  5  * 可以调用它的setSubject()、setContent(),设置主题和正文
  6  * 也可以调用setFrom()和 addToAddress(),设置发件人,和添加收件人。
  7  * 也可以调用addAttch()添加附件
  8  * 创建AttachBean:new AttachBean(new File("..."), "fileName");
  9  */
 10 public class Mail {
 11     private String from;//发件人
 12     private StringBuilder toAddress = new StringBuilder();//收件人
 13     private StringBuilder ccAddress = new StringBuilder();//抄送
 14     private StringBuilder bccAddress = new StringBuilder();//暗送
 15     
 16     private String subject;//主题
 17     private String content;//正文
 18     
 19     // 附件列表
 20     private List<AttachBean> attachList = new ArrayList<AttachBean>();
 21     
 22     public Mail() {}
 23     
 24     public Mail(String from, String to) {
 25         this(from, to, null, null);
 26     }
 27     
 28     public Mail(String from, String to, String subject, String content) {
 29         this.from = from;
 30         this.toAddress.append(to);
 31         this.subject = subject;
 32         this.content = content;
 33     }
 34     
 35     /**
 36      * 返回发件人
 37      */
 38     public void setFrom(String from) {
 39         this.from = from;
 40     }
 41     
 42     /**
 43      * 返回发件人
 44      */
 45     public String getFrom() {
 46         return from;
 47     }
 48     
 49     /**
 50      * 返回主题
 51      */
 52     public String getSubject() {
 53         return subject;
 54     }
 55 
 56     /**
 57      * 设置主题
 58      */
 59     public void setSubject(String subject) {
 60         this.subject = subject;
 61     }
 62 
 63     /**
 64      * 获取主题内容
 65      */
 66     public String getContent() {
 67         return content;
 68     }
 69 
 70     /**
 71      * 设置主题内容
 72      */
 73     public void setContent(String content) {
 74         this.content = content;
 75     }
 76 
 77     /**
 78      * 获取收件人
 79      */
 80     public String getToAddress() {
 81         return toAddress.toString();
 82     }
 83 
 84     /**
 85      * 获取抄送
 86      */
 87     public String getCcAddress() {
 88         return ccAddress.toString();
 89     }
 90 
 91     /**
 92      * 获取暗送
 93      */
 94     public String getBccAddress() {
 95         return bccAddress.toString();
 96     }
 97 
 98     /**
 99      * 添加收件人,可以是多个收件人
100      */
101     public void addToAddress(String to) {
102         if(this.toAddress.length() > 0) {
103             this.toAddress.append(",");
104         }
105         this.toAddress.append(to);
106     }
107 
108     /**
109      * 添加抄送人,可以是多个抄送人
110      */
111     public void addCcAddress(String cc) {
112         if(this.ccAddress.length() > 0) {
113             this.ccAddress.append(",");
114         }
115         this.ccAddress.append(cc);
116     }
117 
118     /**
119      * 添加暗送人,可以是多个暗送人
120      */
121     public void addBccAddress(String bcc) {
122         if(this.bccAddress.length() > 0) {
123             this.bccAddress.append(",");
124         }
125         this.bccAddress.append(bcc);
126     }
127     
128     /**
129      * 添加附件,可以添加多个附件
130      */
131     public void addAttach(AttachBean attachBean) {
132         this.attachList.add(attachBean);
133     }
134     
135     /**
136      * 获取所有附件
137      */
138     public List<AttachBean> getAttachs() {
139         return this.attachList;
140     }
141 }

MailUtil邮件工具类:

 1 /*
 2  * 发送邮件
 3  */
 4 public class MailUtils {
 5     public static Session createSession(String host, final String username, final String password) {
 6         Properties prop = new Properties();
 7         prop.setProperty("mail.host", host);// 指定主机
 8         prop.setProperty("mail.smtp.auth", "true");// 指定验证为true
 9 
10         // 创建验证器
11         Authenticator auth = new Authenticator() {
12             public PasswordAuthentication getPasswordAuthentication() {
13                 return new PasswordAuthentication(username, password);
14             }
15         };
16         
17         // 获取session对象
18         return Session.getInstance(prop, auth);
19     }
20     
21     /**
22      * 发送指定的邮件
23      * 
24      * @param mail
25      */
26     public static void send(Session session, final Mail mail) throws MessagingException,
27             IOException {
28 
29         MimeMessage msg = new MimeMessage(session);// 创建邮件对象
30         msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人
31         msg.addRecipients(RecipientType.TO, mail.getToAddress());// 设置收件人
32 
33         // 设置抄送
34         String cc = mail.getCcAddress();
35         if (!cc.isEmpty()) {
36             msg.addRecipients(RecipientType.CC, cc);
37         }
38 
39         // 设置暗送
40         String bcc = mail.getBccAddress();
41         if (!bcc.isEmpty()) {
42             msg.addRecipients(RecipientType.BCC, bcc);
43         }
44 
45         msg.setSubject(mail.getSubject());// 设置主题
46 
47         MimeMultipart parts = new MimeMultipart();// 创建部件集对象
48 
49         MimeBodyPart part = new MimeBodyPart();// 创建一个部件
50         part.setContent(mail.getContent(), "text/html;charset=utf-8");// 设置邮件文本内容
51         parts.addBodyPart(part);// 把部件添加到部件集中
52 
53         // 添加附件
54         List<AttachBean> attachBeanList = mail.getAttachs();// 获取所有附件
55         if (attachBeanList != null) {
56             for (AttachBean attach : attachBeanList) {
57                 MimeBodyPart attachPart = new MimeBodyPart();// 创建一个部件
58                 attachPart.attachFile(attach.getFile());// 设置附件文件
59                 attachPart.setFileName(MimeUtility.encodeText(attach
60                         .getFileName()));// 设置附件文件名
61                 String cid = attach.getCid();
62                 if(cid != null) {
63                     attachPart.setContentID(cid);
64                 }
65                 parts.addBodyPart(attachPart); 
66             }
67         }
68 
69         msg.setContent(parts);// 给邮件设置内容
70         Transport.send(msg);// 发邮件
71     }
72 }

邮件中的附件类:

 1 /*
 2  * 附件类,只有文件,即附件才文件名
 3  */
 4 public class AttachBean {
 5     private String cid;
 6     private File file;
 7     private String fileName;
 8 
 9     public String getCid() {
10         return cid;
11     }
12 
13     public void setCid(String cid) {
14         this.cid = cid;
15     }
16     
17     public File getFile() {
18         return file;
19     }
20 
21     public void setFile(File file) {
22         this.file = file;
23     }
24 
25     public String getFileName() {
26         return fileName;
27     }
28 
29     public void setFileName(String fileName) {
30         this.fileName = fileName;
31     }
32 
33     public AttachBean() {
34 
35     }
36 
37     public AttachBean(File file, String fileName) {
38         super();
39         this.file = file;
40         this.fileName = fileName;
41     }
42 }
原文地址:https://www.cnblogs.com/zou-zou/p/6129530.html