1 服务器邮箱登录验证类
package com.sunrise.jop.common.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 服务器邮箱登录验证
* */
public class MailAuthenticator extends Authenticator{
private String userName = null;//用于发送邮件的邮箱
private String password = null;//邮箱密码
public MailAuthenticator() {
}
public MailAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.sunrise.jop.common.mail;
import java.io.File;
import java.util.List;
import java.util.Properties;
/**
* 邮件信息bean
* */
public class MailSendeBean {
// 邮箱地址
private String emailAddress;
// 主题
private String subject;
// 内容
private String content;
// 附件
private List<File> attachFiles;
public MailSendeBean() {
}
public MailSendeBean(String emailAddress, String subject, String content) {
this.emailAddress = emailAddress;
this.subject = subject;
this.content = content;
}
public MailSendeBean(String emailAddress, String subject, String content,
List<File> attachFiles) {
this.emailAddress = emailAddress;
this.subject = subject;
this.content = content;
this.attachFiles = attachFiles;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<File> getAttachFiles() {
return attachFiles;
}
public void setAttachFiles(List<File> attachFiles) {
this.attachFiles = attachFiles;
}
}
3 邮件发送器
package com.sunrise.jop.common.mail;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* 邮件发送器
* */
public class MailSender {
//发送邮件的props文件
private final transient Properties props = System.getProperties();
private transient MailAuthenticator authenticator;//邮件服务器登录验证
private transient Session session;//邮箱session
/**
* 初始化邮件发送器
*
* @param smtpHostName
* SMTP邮件服务器地址
* @param username
* 发送邮件的用户名(地址)
* @param password
* 发送邮件的密码
*/
public MailSender(final String smtpHostName, final String username,
final String password) {
init(username, password, smtpHostName);
}
/**
* 初始化邮件发送器
*
* @param username
* 发送邮件的用户名(地址),并以此解析SMTP服务器地址
* @param password
* 发送邮件的密码
*/
public MailSender(final String username, final String password) {
//通过邮箱地址解析出smtp服务器,适合大多数邮箱
final String smtpHostName = "smtp." + username.split("@")[1];
init(username, password, smtpHostName);
}
/**
* 初始化
*
* @param username
* 发送邮件的用户名(地址)
* @param password
* 密码
* @param smtpHostName
* SMTP主机地址
*/
private void init(String username, String password, String smtpHostName) {
// 初始化props
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpHostName);
// 验证
authenticator = new MailAuthenticator(username, password);
// 创建session
session = Session.getInstance(props, authenticator);
}
/**
* 群发邮件
* @throws Exception
* */
public String sendToManyUser( List<MailSendeBean> mailBeanList ) throws Exception{
if(mailBeanList==null||mailBeanList.size()==0)
{
return "请设置邮件相关参数\n ";
}
StringBuffer stringBuffer = new StringBuffer();
for( int i = 0 ; i < mailBeanList.size() ; i++ ){
MailSendeBean mailBean = mailBeanList.get(i);
stringBuffer.append(this.sendToOneUser(mailBean));
}
return stringBuffer.toString();
}
/**
* 单个邮件发送
* @throws Exception
* */
public String sendToOneUser(MailSendeBean mailSendeBean) throws Exception{
if(mailSendeBean==null) return "请设置邮件相关参数\n ";
//返回提示信息
String result = "["+mailSendeBean.getEmailAddress() +"]发送邮件成功\n " ;
// 创建mime类型邮件
final MimeMessage message = new MimeMessage(session);
// 设置发信人
message.setFrom(new InternetAddress(authenticator.getUserName()));
// 设置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(mailSendeBean.getEmailAddress()));
// 设置主题
message.setSubject(mailSendeBean.getSubject());
message.setSentDate(new Date());
// // 设置邮件内容
// message.setContent(mailSendeBean.getContent().toString(), "text/html;charset=utf-8");
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 添加邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(mailSendeBean.getContent().toString(), "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
List<File> attachFiles = mailSendeBean.getAttachFiles();
if(attachFiles!=null&&attachFiles.size() >0 ){
for( int i = 0 ; i < attachFiles.size() ; i++){
File attchFile = attachFiles.get(i);
if(attchFile != null){
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attchFile);
attachmentBodyPart.setDataHandler(new DataHandler(source));
//MimeUtility.encodeWord可以避免文件名乱码
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attchFile.getName()));
multipart.addBodyPart(attachmentBodyPart);
}
}
}
// 将multipart对象放到message中
message.setContent(multipart);
try{
Transport.send(message);
}catch(Exception e){
result = "["+mailSendeBean.getEmailAddress() +"]未知异常,发送邮件失败\n " ;
}
return result;
}
}
4 邮件发送demo
package com.sunrise.jop.common.mail;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 邮件发送demo
* */
public class MailSenderDemo {
//创建邮件服务器
private MailSender createMailSender(){
String smtpHostName = "imap2.eumail.qq.com"; //邮件服务器
String username = "jiangpg@op-moibile.com.cn";
String password = "JianigPeng24212411";
MailSender mailSender = new MailSender(smtpHostName,username,password);
return mailSender;
}
//多个用户邮件发送
private void sendToManyUser() throws Exception{
//初始化邮件服务器
MailSender mailSender = this.createMailSender();
//第一个用户
//创建邮件bean
String emailAddress1 = "125429020113@qq.com";
String subject1 = "邮件主题测试1";
String content1 = "邮件内容测试1";
//附件
File txtFile1 = new File("D:"+File.separator+ "emailTest" + File.separator + "testText.txt");
File docFile1 = new File("D:"+File.separator+ "emailTest" + File.separator + "testDoc.doc");
File excelFile1 = new File("D:"+File.separator+ "emailTest" + File.separator + "testExcel.xls");
File photoFile1 = new File("D:"+File.separator+ "emailTest" + File.separator + "testPhoto.jpg");
List<File> attchFiles1 = new ArrayList<File>();
attchFiles1.add(txtFile1); attchFiles1.add(docFile1);
attchFiles1.add(excelFile1); attchFiles1.add(photoFile1);
//封装单个邮件bean
MailSendeBean mailSendeBean1 = new MailSendeBean( emailAddress1, subject1, content1,attchFiles1);
//第二个用户
String emailAddress2 = "3313418032173@qq.com";
String subject2 = "邮件主题测试2";
String content2 = "邮件内容测试2";
//附件
File txtFile2 = new File("D:"+File.separator+ "emailTest" + File.separator + "testText.txt");
File docFile2 = new File("D:"+File.separator+ "emailTest" + File.separator + "testDoc.doc");
List<File> attchFiles2 = new ArrayList<File>();
attchFiles2.add(txtFile2); attchFiles2.add(docFile2);
//封装单个邮件bean
MailSendeBean mailSendeBean2 = new MailSendeBean( emailAddress2, subject2, content2,attchFiles2);
//封装多个邮件bean
List<MailSendeBean> mailList = new ArrayList<MailSendeBean>();
mailList.add(mailSendeBean1); mailList.add(mailSendeBean2);
// 发送邮件
String resultMessage = mailSender.sendToManyUser(mailList);
System.out.println("resultMessage = " + resultMessage);
}
//单个邮件发送
private void sendToOneUser() throws Exception{
//初始化邮件服务器
MailSender mailSender = this.createMailSender();
//创建邮件bean
String emailAddress = "1254290201@qq.com";
String subject = "邮件主题测试";
String content = "邮件内容测试";
//附件
File txtFile = new File("D:"+File.separator+ "emailTest" + File.separator + "testText.txt");
File docFile = new File("D:"+File.separator+ "emailTest" + File.separator + "testDoc.doc");
File excelFile = new File("D:"+File.separator+ "emailTest" + File.separator + "testExcel.xls");
File photoFile = new File("D:"+File.separator+ "emailTest" + File.separator + "testPhoto.jpg");
List<File> attchFiles = new ArrayList<File>();
attchFiles.add(txtFile); attchFiles.add(docFile);
attchFiles.add(excelFile); attchFiles.add(photoFile);
//封装邮件bean
MailSendeBean mailSendeBean = new MailSendeBean( emailAddress, subject, content,attchFiles);
// 发送邮件
String resultMessage = mailSender.sendToOneUser(mailSendeBean);
System.out.println("resultMessage = " + resultMessage);
}
public static void main(String args[]) throws Exception{
MailSenderDemo mailDemo = new MailSenderDemo();
// mailDemo.sendToOneUser(); // 测试单个邮件发送
mailDemo.sendToManyUser(); // 测试多个邮件发送
}
}