邮件辅助类
一、使用方法
在servlet层中调用UserService.java
1 public void regist(User user) { 2 //1. 数据的补齐 3 user.setUid(CommonUtils.uuid()); 4 user.setStatus(false); 5 user.setActivationCode(CommonUtils.uuid() + CommonUtils.uuid()); 6 7 //2. 向数据库插入 8 try { 9 userDao.add(user); 10 } catch (SQLException e) { 11 throw new RuntimeException(e); 12 } 13 14 //3. 发邮件 15 //3.1 把配置文件内容加载到prop中 16 Properties prop = new Properties(); 17 try { 18 prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties")); 19 } catch (IOException e) { 20 throw new RuntimeException(e); 21 } 22 //3.2 登录邮件服务器,得到session 23 String host = prop.getProperty("host"); 24 String username = prop.getProperty("username"); 25 String password = prop.getProperty("password"); 26 Session session = MailUtils.createSession(host, username, password); 27 28 //3.3 创建Mail对象 29 String from = prop.getProperty("from"); 30 String to = user.getEmail(); 31 String subject = prop.getProperty("subject"); 32 // MessageForm.format方法会把第一个参数中的{0},使用第二个参数来替换。 33 // 例如MessageFormat.format("你好{0}, 你{1}!", "张三", "去死吧"); 返回“你好张三,你去死吧!” 34 String content = MessageFormat.format(prop.getProperty("content"), user.getActivationCode()); 35 Mail mail = new Mail(from, to ,subject, content); 36 37 //3.4 发送邮件 38 try { 39 MailUtils.send(session, mail); 40 } catch (MessagingException e) { 41 throw new RuntimeException(e); 42 } catch (IOException e) { 43 throw new RuntimeException(e); 44 } 45 }
二、源代码
1.MailUtils.java
1 package cn.itcast.mail; 2 3 import java.io.IOException; 4 import java.util.List; 5 import java.util.Properties; 6 7 import javax.mail.Authenticator; 8 import javax.mail.Message.RecipientType; 9 import javax.mail.MessagingException; 10 import javax.mail.PasswordAuthentication; 11 import javax.mail.Session; 12 import javax.mail.Transport; 13 import javax.mail.internet.InternetAddress; 14 import javax.mail.internet.MimeBodyPart; 15 import javax.mail.internet.MimeMessage; 16 import javax.mail.internet.MimeMultipart; 17 import javax.mail.internet.MimeUtility; 18 19 /** 20 * 21 * @author itcast 本类只有这么一个方法,用来发邮件! 22 */ 23 public class MailUtils { 24 public static Session createSession(String host, final String username, final String password) { 25 Properties prop = new Properties(); 26 prop.setProperty("mail.host", host);// 指定主机 27 prop.setProperty("mail.smtp.auth", "true");// 指定验证为true 28 29 // 创建验证器 30 Authenticator auth = new Authenticator() { 31 public PasswordAuthentication getPasswordAuthentication() { 32 return new PasswordAuthentication(username, password); 33 } 34 }; 35 36 // 获取session对象 37 return Session.getInstance(prop, auth); 38 } 39 40 /** 41 * 发送指定的邮件 42 * 43 * @param mail 44 */ 45 public static void send(Session session, final Mail mail) throws MessagingException, 46 IOException { 47 48 MimeMessage msg = new MimeMessage(session);// 创建邮件对象 49 msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人 50 msg.addRecipients(RecipientType.TO, mail.getToAddress());// 设置收件人 51 52 // 设置抄送 53 String cc = mail.getCcAddress(); 54 if (!cc.isEmpty()) { 55 msg.addRecipients(RecipientType.CC, cc); 56 } 57 58 // 设置暗送 59 String bcc = mail.getBccAddress(); 60 if (!bcc.isEmpty()) { 61 msg.addRecipients(RecipientType.BCC, bcc); 62 } 63 64 msg.setSubject(mail.getSubject());// 设置主题 65 66 MimeMultipart parts = new MimeMultipart();// 创建部件集对象 67 68 MimeBodyPart part = new MimeBodyPart();// 创建一个部件 69 part.setContent(mail.getContent(), "text/html;charset=utf-8");// 设置邮件文本内容 70 parts.addBodyPart(part);// 把部件添加到部件集中 71 72 /////////////////////////////////////////// 73 74 // 添加附件 75 List<AttachBean> attachBeanList = mail.getAttachs();// 获取所有附件 76 if (attachBeanList != null) { 77 for (AttachBean attach : attachBeanList) { 78 MimeBodyPart attachPart = new MimeBodyPart();// 创建一个部件 79 attachPart.attachFile(attach.getFile());// 设置附件文件 80 attachPart.setFileName(MimeUtility.encodeText(attach 81 .getFileName()));// 设置附件文件名 82 String cid = attach.getCid(); 83 if(cid != null) { 84 attachPart.setContentID(cid); 85 } 86 parts.addBodyPart(attachPart); 87 } 88 } 89 90 msg.setContent(parts);// 给邮件设置内容 91 Transport.send(msg);// 发邮件 92 } 93 }
2.Mail.java
1 package cn.itcast.mail; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * 表示邮件类,你需要设置:账户名和密码、收件人、抄送(可选)、暗送(可选)、主题、内容,以及附件(可选) 8 * 9 * 在创建了Mail对象之后 10 * 可以调用它的setSubject()、setContent(),设置主题和正文 11 * 也可以调用setFrom()和 addToAddress(),设置发件人,和添加收件人。 12 * 也可以调用addAttch()添加附件 13 * 创建AttachBean:new AttachBean(new File("..."), "fileName"); 14 */ 15 public class Mail { 16 private String from;//发件人 17 private StringBuilder toAddress = new StringBuilder();//收件人 18 private StringBuilder ccAddress = new StringBuilder();//抄送 19 private StringBuilder bccAddress = new StringBuilder();//暗送 20 21 private String subject;//主题 22 private String content;//正文 23 24 // 附件列表 25 private List<AttachBean> attachList = new ArrayList<AttachBean>(); 26 27 public Mail() {} 28 29 public Mail(String from, String to) { 30 this(from, to, null, null); 31 } 32 33 public Mail(String from, String to, String subject, String content) { 34 this.from = from; 35 this.toAddress.append(to); 36 this.subject = subject; 37 this.content = content; 38 } 39 40 /** 41 * 返回发件人 42 * @return 43 */ 44 public void setFrom(String from) { 45 this.from = from; 46 } 47 48 /** 49 * 返回发件人 50 * @return 51 */ 52 public String getFrom() { 53 return from; 54 } 55 56 /** 57 * 返回主题 58 */ 59 public String getSubject() { 60 return subject; 61 } 62 63 /** 64 * 设置主题 65 */ 66 public void setSubject(String subject) { 67 this.subject = subject; 68 } 69 70 /** 71 * 获取主题内容 72 */ 73 public String getContent() { 74 return content; 75 } 76 77 /** 78 * 设置主题内容 79 * @param content 80 */ 81 public void setContent(String content) { 82 this.content = content; 83 } 84 85 /** 86 * 获取收件人 87 * @return 88 */ 89 public String getToAddress() { 90 return toAddress.toString(); 91 } 92 93 /** 94 * 获取抄送 95 * @return 96 */ 97 public String getCcAddress() { 98 return ccAddress.toString(); 99 } 100 101 /** 102 * 获取暗送 103 * @return 104 */ 105 public String getBccAddress() { 106 return bccAddress.toString(); 107 } 108 109 /** 110 * 添加收件人,可以是多个收件人 111 * @param to 112 */ 113 public void addToAddress(String to) { 114 if(this.toAddress.length() > 0) { 115 this.toAddress.append(","); 116 } 117 this.toAddress.append(to); 118 } 119 120 /** 121 * 添加抄送人,可以是多个抄送人 122 * @param cc 123 */ 124 public void addCcAddress(String cc) { 125 if(this.ccAddress.length() > 0) { 126 this.ccAddress.append(","); 127 } 128 this.ccAddress.append(cc); 129 } 130 131 /** 132 * 添加暗送人,可以是多个暗送人 133 * @param bcc 134 */ 135 public void addBccAddress(String bcc) { 136 if(this.bccAddress.length() > 0) { 137 this.bccAddress.append(","); 138 } 139 this.bccAddress.append(bcc); 140 } 141 142 /** 143 * 添加附件,可以添加多个附件 144 * @param attachBean 145 */ 146 public void addAttach(AttachBean attachBean) { 147 this.attachList.add(attachBean); 148 } 149 150 /** 151 * 获取所有附件 152 * @return 153 */ 154 public List<AttachBean> getAttachs() { 155 return this.attachList; 156 } 157 }
3.AttachBean.java
1 package cn.itcast.mail; 2 3 import java.io.File; 4 5 /* 6 * 附件类,只有文件,即附件才文件名 7 */ 8 public class AttachBean { 9 private String cid; 10 private File file; 11 private String fileName; 12 13 public String getCid() { 14 return cid; 15 } 16 17 public void setCid(String cid) { 18 this.cid = cid; 19 } 20 21 public File getFile() { 22 return file; 23 } 24 25 public void setFile(File file) { 26 this.file = file; 27 } 28 29 public String getFileName() { 30 return fileName; 31 } 32 33 public void setFileName(String fileName) { 34 this.fileName = fileName; 35 } 36 37 public AttachBean() { 38 39 } 40 41 public AttachBean(File file, String fileName) { 42 super(); 43 this.file = file; 44 this.fileName = fileName; 45 } 46 }