1 package com.feihe.util.mail;2
3 import java.util.ArrayList;4 import java.util.Date;5 import java.util.List;6 import java.util.Properties;7 import java.util.regex.Matcher;8 import java.util.regex.Pattern;9
10 import javax.activation.DataHandler;11 import javax.activation.DataSource;12 import javax.activation.FileDataSource;13 import javax.mail.Address;14 import javax.mail.Authenticator;15 import javax.mail.BodyPart;16 import javax.mail.Message;17 import javax.mail.PasswordAuthentication;18 import javax.mail.Session;19 import javax.mail.Transport;20 import javax.mail.internet.InternetAddress;21 import javax.mail.internet.MimeBodyPart;22 import javax.mail.internet.MimeMessage;23 import javax.mail.internet.MimeMultipart;24 import javax.mail.internet.MimeUtility;25
26 import com.sun.mail.util.MailSSLSocketFactory;27
28 public classsendMailTest {29 public static voidmain(String[] args) throws Exception {30 //配置信息
31 Properties pro = newProperties();32 pro.put("mail.smtp.host", "smtp.163.com");33 pro.put("mail.smtp.auth", "true");34 //SSL加密
35 MailSSLSocketFactory sf = null;36 sf = newMailSSLSocketFactory();37 //设置信任所有的主机
38 sf.setTrustAllHosts(true);39 pro.put("mail.smtp.ssl.enable", "true");40 pro.put("mail.smtp.ssl.socketFactory", sf);41 //根据邮件的会话属性构造一个发送邮件的Session,这里需要注意的是用户名那里不能加后缀,否则便不是用户名了42 //还需要注意的是,这里的密码不是正常使用邮箱的登陆密码,而是客户端生成的另一个专门的授权码
43 MailAuthenticator0 authenticator = new MailAuthenticator0("13691209103", "客户端授权码");44 Session session =Session.getInstance(pro, authenticator);45 //根据Session 构建邮件信息
46 Message message = newMimeMessage(session);47 //创建邮件发送者地址
48 Address from = new InternetAddress("13691209103@163.com");49 //设置邮件消息的发送者
50 message.setFrom(from);51 //验证收件人邮箱地址
52 List toAddressList = new ArrayList<>();53 toAddressList.add("liuruiting@feihe.com");54 StringBuffer buffer = newStringBuffer();55 if (!toAddressList.isEmpty()) {56 String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";57 Pattern p =Pattern.compile(regEx);58 for (int i = 0; i < toAddressList.size(); i++) {59 Matcher match = p.matcher(toAddressList.get(i));60 if(match.matches()) {61 buffer.append(toAddressList.get(i));62 if (i < toAddressList.size() - 1) {63 buffer.append(",");64 }65 }66 }67 }68 String toAddress =buffer.toString();69 if (!toAddress.isEmpty()) {70 //创建邮件的接收者地址
71 Address[] to =InternetAddress.parse(toAddress);72 //设置邮件接收人地址
73 message.setRecipients(Message.RecipientType.TO, to);74 //邮件主题75 //message.setSubject("java邮件测试");
76 message.setSubject("为什么错了");77 //邮件容器
78 MimeMultipart mimeMultiPart = newMimeMultipart();79 //设置HTML
80 BodyPart bodyPart = newMimeBodyPart();81 //邮件内容82 //String htmlText = "java邮件测试111";
83 String htmlText = "为什么错了";84 bodyPart.setContent(htmlText, "text/html;charset=utf-8");85 mimeMultiPart.addBodyPart(bodyPart);86 //添加附件
87 List fileAddressList = new ArrayList();88 fileAddressList.add("C:\\Users\\haw2106\\Desktop\\123.jpg");89 if (fileAddressList != null) {90 BodyPart attchPart = null;91 for (int i = 0; i < fileAddressList.size(); i++) {92 if (!fileAddressList.get(i).isEmpty()) {93 attchPart = newMimeBodyPart();94 //附件数据源
95 DataSource source = new FileDataSource(fileAddressList.get(i));96 //将附件数据源添加到邮件体
97 attchPart.setDataHandler(newDataHandler(source));98 //设置附件名称为原文件名
99 attchPart.setFileName(MimeUtility.encodeText(source.getName()));100 mimeMultiPart.addBodyPart(attchPart);101 }102 }103 }104 message.setContent(mimeMultiPart);105 message.setSentDate(newDate());106 //保存邮件
107 message.saveChanges();108 //发送邮件
109 Transport.send(message);110 }111 }112 }113
114 classMailAuthenticator0 extends Authenticator {115 /**116 * 用户名117 */
118 privateString username;119 /**120 * 密码121 */
122 privateString password;123
124 /**125 * 创建一个新的实例 MailAuthenticator.126 *127 * @param username128 * @param password129 */
130 publicMailAuthenticator0(String username, String password) {131 this.username =username;132 this.password =password;133 }134
135 publicString getPassword() {136 returnpassword;137 }138
139 @Override140 protectedPasswordAuthentication getPasswordAuthentication() {141 return newPasswordAuthentication(username, password);142 }143
144 publicString getUsername() {145 returnusername;146 }147
148 public voidsetPassword(String password) {149 this.password =password;150 }151
152 public voidsetUsername(String username) {153 this.username =username;154 }155
156 }