可以发送、抄送、密送多人,多个邮箱之间用逗号分隔",",英文的逗号,不要又空格。
写一个用于密码校验的类:
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* @ClassName MyAuthenticator
* @Description 密码验证器
*
* @author laosan
* @date 2014-12-8 下午2:25:56
*/
public class MyAuthenticator extends Authenticator
{
String userName = null;
String password = null;
public MyAuthenticator()
{
}
public MyAuthenticator(String username, String password)
{
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
}
再写一个用于存储邮件信息的类,这个类也可以不写,但是不写,在后面的使用中会有一些不方便,建议写上:
import java.util.Properties;
/**
* @ClassName MailSenderInfo
* @Description 邮件发送信息
*
* @author laosan
* @date 2014-12-8 下午2:19:20
*/
public class MailSenderInfo
{
/**
* @FieldName mailServerHost 发送邮件的服务器的IP
*/
private String mailServerHost;
/**
* @FieldName mailServerPort 发送邮件的服务器的端口
*/
private String mailServerPort = "25";
/**
* @FieldName fromAddress 邮件发送者的地址
*/
private String fromAddress;
/**
* @FieldName toAddress 邮件接收者的地址
*/
private String toAddress;
/**
* @FieldName ccAddress 抄送地址
*/
private String ccAddress;
/**
* @FieldName scAddress 密送地址
*/
private String bccAddress;
/**
* @FieldName userName 登陆邮件发送服务器的用户名
*/
private String userName;
/**
* @FieldName password 登陆邮件发送服务器的密码
*/
private String password;
/**
* @FieldName validate 是否需要身份验证
*/
private boolean validate = false;
/**
* @FieldName subject 邮件主题
*/
private String subject;
/**
* @FieldName content 邮件的文本内容
*/
private String content;
/**
* @FieldName attachFileNames 邮件附件的文件名
*/
private String[] attachFileNames;
/**
* 获得邮件会话属性
*/
public Properties getProperties()
{
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost()
{
return mailServerHost;
}
public void setMailServerHost(String mailServerHost)
{
this.mailServerHost = mailServerHost;
}
public String getMailServerPort()
{
return mailServerPort;
}
public void setMailServerPort(String mailServerPort)
{
this.mailServerPort = mailServerPort;
}
public boolean isValidate()
{
return validate;
}
public void setValidate(boolean validate)
{
this.validate = validate;
}
public String[] getAttachFileNames()
{
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames)
{
this.attachFileNames = fileNames;
}
public String getFromAddress()
{
return fromAddress;
}
public void setFromAddress(String fromAddress)
{
this.fromAddress = fromAddress;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getToAddress()
{
return toAddress;
}
public void setToAddress(String toAddress)
{
this.toAddress = toAddress;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getContent()
{
return content;
}
public void setContent(String textContent)
{
this.content = textContent;
}
public String getCcAddress()
{
return ccAddress;
}
public void setCcAddress(String ccAddress)
{
this.ccAddress = ccAddress;
}
public String getBccAddress()
{
return bccAddress;
}
public void setBccAddress(String bccAddress)
{
this.bccAddress = bccAddress;
}
}
最后写一个用于发送邮件的工具类:
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
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.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* @ClassName SimpleMailSender
* @Description 邮件发送
*
* @author laosan
* @date 2014-12-8 下午2:31:24
*/
@SuppressWarnings("static-access")
public class SimpleMailSender
{
/**
* @MethodName sendTextMail
* @Description 文本格式发送邮件
*
* @author laosan
* @date 2014-12-8 下午2:32:22
* @param mailInfo
* @return
*/
public static boolean sendTextMail(MailSenderInfo mailInfo)
{
if (null == mailInfo)
{
return false;
}
try
{
Message mailMessage = setMailBasicInfo(mailInfo);
// 设置邮件的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
/**
* @MethodName sendHtmlMail
* @Description 以HTML格式发送邮件
*
* @author laosan
* @date 2014-12-8 下午2:35:53
* @param mailInfo
* @return
*/
public static boolean sendHtmlMail(MailSenderInfo mailInfo)
{
if (null == mailInfo)
{
return false;
}
try
{
Message mailMessage = setMailBasicInfo(mailInfo);
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
/**
* @MethodName sendMailForAttachFile
* @Description 发送带有附件的邮件,以HTML形式发送
*
* @author laosan
* @date 2014-12-8 下午03:43:10
* @param mailInfo
* @return
*/
public static boolean sendHtmlMail4AttachFile(MailSenderInfo mailInfo)
{
// 如果传入的信息为空,直接返回发送失败
if (null == mailInfo)
{
return false;
}
try
{
Message mailMessage = setMailBasicInfo(mailInfo);
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart bodyPart = new MimeBodyPart();
// 设置HTML内容
bodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(bodyPart);
// 添加所有的附件
FileDataSource fileDataSource = null;
for (int i = 0; i < mailInfo.getAttachFileNames().length; i++)
{
String attachFileName = mailInfo.getAttachFileNames()[i];
bodyPart = new MimeBodyPart();
fileDataSource = new FileDataSource(attachFileName);
bodyPart.setDataHandler(new DataHandler(fileDataSource));
bodyPart.setFileName(MimeUtility.encodeText(fileDataSource.getName(), "utf-8", null));
mainPart.addBodyPart(bodyPart);
}
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
catch (UnsupportedEncodingException ue)
{
ue.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
/**
* @MethodName setMailBasicInfo
* @Description 设置邮件发送的基本信息
*
* @author laosan
* @date 2014-12-8 下午03:00:10
* @param mailInfo
* @return
*/
private static Message setMailBasicInfo(MailSenderInfo mailInfo)
{
// 如果传入的邮件信息为空,返回空
if (null == mailInfo)
{
return null;
}
// 判断是否需要身份认证, 如果需要身份认证,则创建一个密码验证器
MyAuthenticator authenticator = null;
if (mailInfo.isValidate())
{
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 配置信息
Properties pro = mailInfo.getProperties();
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
try
{
// 发件人地址
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
// 收件人地址
InternetAddress[] to = new InternetAddress().parse(mailInfo.getToAddress());
mailMessage.setRecipients(Message.RecipientType.TO, to);
// 抄送地址
if (null != mailInfo.getCcAddress() && mailInfo.getCcAddress().trim().length() > 0)
{
InternetAddress[] cc = new InternetAddress().parse(mailInfo.getCcAddress());
mailMessage.setRecipients(Message.RecipientType.CC, cc);
}
// 密送地址
if (null != mailInfo.getBccAddress() && mailInfo.getBccAddress().trim().length() > 0)
{
InternetAddress[] bcc = new InternetAddress().parse(mailInfo.getBccAddress());
mailMessage.setRecipients(Message.RecipientType.BCC, bcc);
}
// 设置邮件的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件发送的时间
mailMessage.setSentDate(new Date());
return mailMessage;
}
catch (AddressException e)
{
e.printStackTrace();
}
catch (MessagingException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
测试的代码:
public class MailUtil
{
public static void main(String args[])
{
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("#########@163.com");
// 邮箱密码
mailInfo.setPassword("*********");
mailInfo.setFromAddress("##########@163.com");
mailInfo.setToAddress("******@163.com,******@163.com");
mailInfo.setCcAddress("*****@qq.com,*****@126.com");
mailInfo.setBccAddress("******@qq.com,*****@qq.com");
mailInfo.setSubject("邮件3:带附件");
mailInfo.setContent("邮件3:带附件");
mailInfo.setAttachFileNames(new String[]
{
"C:" + File.separator + "Users" + File.separator + "Desktop" + File.separator
+ "新建文本文档 (1).txt",
"C:" + File.separator + "Users" + File.separator + "Desktop" + File.separator
+ "新建文本文档 (2).txt" });
SimpleMailSender.sendHtmlMail4AttachFile(mailInfo);
System.out.println("Send Mail Over!");
}
}