packagecommon;/*** Created by zipon on 2017/4/21.*/
importjava.io.File;importjava.io.UnsupportedEncodingException;importjava.security.GeneralSecurityException;importjava.util.ArrayList;importjava.util.Date;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.Address;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;/*** 邮件发送
*@authorxdemo.org
**/
public classMailSender {/*** 以文本格式发送邮件
*
*@parammail
* 待发送的邮件的信息
*@throwsGeneralSecurityException*/
public boolean sendTextMail(Email mail) throwsGeneralSecurityException {//判断是否需要身份认证
MailAuthenticator authenticator = null;
Properties pro=mail.getProperties();if(mail.isValidate()) {//如果需要身份认证,则创建一个密码验证器
authenticator = newMailAuthenticator(mail.getUserName(), mail.getPassword());
}//根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession =Session.getDefaultInstance(pro, authenticator);try{//根据session创建一个邮件消息
Message mailMessage = newMimeMessage(sendMailSession);//创建邮件发送者地址
Address from = newInternetAddress(mail.getFromAddress());//设置邮件消息的发送者
mailMessage.setFrom(from);//创建邮件的接收者地址,并设置到邮件消息中
Address to = newInternetAddress(mail.getToAddress());
mailMessage.setRecipients(Message.RecipientType.TO, to);//创建邮件抄送者地址
if(mail.getCCAddress()!=null) {//Address cc = new InternetAddress(mail.getCCAddress());
mailMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(mail.getCCAddress()));
}//创建邮件暗抄送者地址
if(mail.getBCCAddress()!=null) {//Address bcc = new InternetAddress(mail.getBCCAddress());
mailMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mail.getBCCAddress()));
}//设置邮件消息的主题
mailMessage.setSubject(mail.getSubject());//设置邮件消息发送的时间s
mailMessage.setSentDate(newDate());//设置邮件消息的主要内容
String mailContent =mail.getContent();
mailMessage.setText(mailContent);//发送邮件
Transport.send(mailMessage);return true;
}catch(MessagingException ex) {
ex.printStackTrace();
}return false;
}/*** 以HTML格式发送邮件
*
*@parammail
* 待发送的邮件信息
*@throwsGeneralSecurityException
*@throwsUnsupportedEncodingException*/
public boolean sendHtmlMail(Email mail) throwsGeneralSecurityException, UnsupportedEncodingException {//判断是否需要身份认证
MailAuthenticator authenticator = null;
Properties props=mail.getProperties();//如果需要身份认证,则创建一个密码验证器
if(mail.isValidate()) {
authenticator= newMailAuthenticator(mail.getUserName(), mail.getPassword());
}//根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession =Session.getDefaultInstance(props, authenticator);
sendMailSession.setDebug(true);try{//根据session创建一个邮件消息
Message mailMessage = newMimeMessage(sendMailSession);//创建邮件发送者地址
Address from = new InternetAddress(mail.getFromAddress(),mail.getFromNickName()==null?"":mail.getFromNickName());//设置邮件消息的发送者
mailMessage.setFrom(from);//创建邮件的接收者地址,并设置到邮件消息中,可以设置多个收件人,逗号隔开//Message.RecipientType.TO属性表示接收者的类型为TO,CC表示抄送,BCC暗送
mailMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail.getToAddress()));//创建邮件抄送者地址
if(mail.getCCAddress()!=null) {//Address cc = new InternetAddress(mail.getCCAddress());
mailMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(mail.getCCAddress()));
}//创建邮件暗抄送者地址s
if(mail.getBCCAddress()!=null) {//Address bcc = new InternetAddress(mail.getBCCAddress());
mailMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mail.getBCCAddress()));
}//设置邮件消息的主题
mailMessage.setSubject(mail.getSubject());//设置邮件消息发送的时间
mailMessage.setSentDate(newDate());//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = newMimeMultipart();//创建一个包含HTML内容的MimeBodyPart
MimeBodyPart html = newMimeBodyPart();//设置HTML内容
html.setContent(mail.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);//设置信件的附件(用本地上的文件作为附件)
FileDataSource fds=null;
DataHandler dh=null;if (mail.getAttachments()!=null) {for(File file : mail.getAttachments()) {
html= newMimeBodyPart();
fds= newFileDataSource(file);
dh= newDataHandler(fds);
html.setFileName(file.getName());
html.setDataHandler(dh);
mainPart.addBodyPart(html);
}
}//将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
mailMessage.saveChanges();//发送邮件
Transport.send(mailMessage);return true;
}catch(MessagingException ex) {
ex.printStackTrace();
}return false;
}public static void main(String[] args) throwsGeneralSecurityException, UnsupportedEncodingException {//final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";//Properties props = System.getProperties();//props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);//
//props.setProperty("mail.smtp.socketFactory.fallback", "false");//props.setProperty("mail.smtp.socketFactory.port", "465");//这个类主要是设置邮件
Email mailInfo = newEmail();
mailInfo.setCCAddress("xxxx@qq.com");
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setFromNickName("xxx");
mailInfo.setUserName("xxx@163.com"); //实际发送者
mailInfo.setPassword("xxx");//您的邮箱密码/授权码
mailInfo.setFromAddress("xxx@163.com"); //设置发送人邮箱地址
mailInfo.setToAddress("xxx@qq.com,xxx@qq.com"); //设置接受者邮箱地址
mailInfo.setSubject("【镁锭项目计划回归】按照这次的来。反馈");
mailInfo.setContent("已完成!反馈。这次一定行!
第一行第一列 | 第二行第二列 |
MailSender sms = newMailSender();//sms.sendTextMail(mailInfo);//发送文体格式
sms.sendHtmlMail(mailInfo); //发送html格式,需要发送附件或者html(一般是table可用,链接好像不行)时,选择这个
}
}