<1>IMail 接口
import java.io.File;
import java.util.Date;
import java.util.List;
import javax.mail.BodyPart;
public interface IMail {
public void set主题(String 主题);
public void setText(String Text);
public void setCotnent(BodyPart content);
public void set发件人(Email地址 发件人);
public void set收件人(List<Email地址> 收件人);
public void set抄送人(List<Email地址> 抄送人);
public void set暗送人(List<Email地址> 暗送人);
public void set邮件时间(Date time);
public void set附件(List<File> files);
public Email地址 get发件人();
public List<Email地址> get收件人();
public List<Email地址> get抄送人();
public List<Email地址> get暗送人();
public List<File> get附件();
public Date get邮件时间();
public String get主题();
public String getText();
public BodyPart getContent();
}
<2>Mail类
import java.io.File;
import java.util.Date;
import java.util.List;
import javax.mail.BodyPart;
public class Mail implements IMail{
private Email地址 发件人;
private String 主题;
private String text;
private BodyPart content;
private List<Email地址> 抄送人;
private List<Email地址> 收件人;
private List<Email地址> 暗送人;
private Date 邮件时间 ;
private List<File> 附件;
public Email地址 get发件人() {
return this.发件人;
}
public List<Email地址> get抄送人() {
return this.抄送人;
}
public List<Email地址> get收件人() {
return this.收件人;
}
public List<Email地址> get暗送人() {
return this.暗送人;
}
public Date get邮件时间() {
return this.邮件时间;
}
public List<File> get附件() {
return this.附件;
}
public void set发件人(Email地址 发件人) {
this.发件人 = 发件人;
}
public void set抄送人(List<Email地址> 抄送人) {
this.抄送人 = 抄送人;
}
public void set收件人(List<Email地址> 收件人) {
this.收件人 = 收件人;
}
public void set暗送人(List<Email地址> 暗送人) {
this.暗送人 = 暗送人;
}
public void set邮件时间(Date time) {
this.邮件时间 = time;
}
public void set附件(List<File> files) {
this.附件 = files;
}
public BodyPart getContent() {
return this.content;
}
public String getText() {
return this.text;
}
public String get主题() {
return this.主题;
}
public void setCotnent(BodyPart content) {
this.content = content;
}
public void setText(String text) {
this.text = text;
}
public void set主题(String 主题) {
this.主题 = 主题;
}
}
<3>Abstract邮件发送服务器
import java.io.File;
import java.util.Date;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
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;
public abstract class Abstract邮件发送服务器 {
protected IMail 邮件;
protected Session session;
protected String send方式;
public Abstract邮件发送服务器() {
}
protected InternetAddress[] getAddressByType(List<Email地址> list) throws Exception {
if (list != null) {
InternetAddress address[] = new InternetAddress[list.size()];
for (int i = 0 ; i < list.size(); i++) {
address[i] = list.get(i).toInternetAddress();
}
return address;
}
return null;
}
public void 发送邮件() {
try {
MimeMessage message = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
message.setSubject(邮件.get主题(),"utf-8");
if(邮件.getText() != null)
message.setText(邮件.getText(),"utf-8");
message.setSentDate(邮件.get邮件时间()==null?new Date():this.邮件.get邮件时间());
multipart.addBodyPart(this.邮件.getContent());
//添加发件人
message.setFrom(邮件.get发件人().toInternetAddress());
//添加收件人
InternetAddress address[] = this.getAddressByType(this.邮件.get收件人());
if (address != null)
message.addRecipients(Message.RecipientType.TO,address);
//添加抄送人
address = this.getAddressByType(this.邮件.get抄送人());
if (address != null)
message.addRecipients(Message.RecipientType.CC,address);
//添加暗送人
address = this.getAddressByType(this.邮件.get暗送人());
if (address != null)
message.addRecipients(Message.RecipientType.BCC,address);
//添加附件
if (this.邮件.get附件() != null) {
for (int i = 0; i < this.邮件.get附件().size(); i++) {
File file = this.邮件.get附件().get(i);
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(MimeUtility.encodeWord(file.getName(),"UTF-8","Q"));
multipart.addBodyPart(messageBodyPart);
}
}
message.setContent(multipart);
if(send方式 == null)
send方式 = "smtp";
session.getTransport(send方式).send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public IMail get邮件() {
return 邮件;
}
public void set邮件(IMail 邮件) {
this.邮件 = 邮件;
}
}
<4>Abstract发送邮件建造者
import javax.mail.Session;
public abstract class Abstract发送邮件建造者 {
protected Abstract邮件发送服务器 mailServer;
public Abstract发送邮件建造者() {
}
public abstract void 建造邮件(IMail mail);
public abstract void 建造邮件session(Session session);
public void 建造发送邮件() {
this.mailServer.发送邮件();
}
}
<5>Smtp邮件发送服务器
public class Smtp邮件发送服务器 extends Abstract邮件发送服务器 {
public Smtp邮件发送服务器() {
this.send方式 = "smtp";
}
}
<6>Smtp发送邮件建造者
import java.util.Properties;
import javax.mail.Session;
public class Smtp发送邮件建造者 extends Abstract发送邮件建造者 {
public Smtp发送邮件建造者() {
this.mailServer = new Smtp邮件发送服务器();
}
@Override
public void 建造邮件(IMail mail) {
this.mailServer.set邮件(mail);
}
@Override
public void 建造邮件session(Session session) {
this.mailServer.setSession(session);
}
}
<7>PasswordAuthenticator
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
class PasswordAuthenticator extends Authenticator {
private String username;
private String password;
public PasswordAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
<8>Email地址
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
public class Email地址 {
protected String 地址;
protected String 地址显示别名;
public Email地址() {
}
public Email地址(String 地址, String 地址显示别名){
this.地址 = 地址;
this.地址显示别名 = 地址显示别名;
}
public String get地址() {
return 地址;
}
public void set地址(String 地址) {
this.地址 = 地址;
}
public String get地址显示别名() {
return 地址显示别名;
}
public void set地址显示别名(String 地址显示别名) {
this.地址显示别名 = 地址显示别名;
}
public InternetAddress toInternetAddress() throws Exception {
if (地址显示别名 != null && !地址显示别名.trim().equals("")) {
return new InternetAddress(地址, MimeUtility.encodeWord(地址显示别名,
"utf-8", "Q"));
}
return new InternetAddress(地址);
}
}
<9>发送邮件Director
import javax.mail.Session;
public class 发送邮件Director {
Abstract发送邮件建造者 build;
public 发送邮件Director(Abstract发送邮件建造者 build) {
this.build = build;
}
public void 发送邮件建造(IMail mail, Session session) {
this.build.建造邮件(mail);
this.build.建造邮件session(session);
this.build.建造发送邮件();
}
}
<10>Client
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
public class Client {
public static void main(String args[]) throws Exception {
IMail mail = new Mail();
mail.set主题("제외되는");
mail.set发件人(new Email地址("[email protected]","赵成明"));
List<Email地址> list = new ArrayList<Email地址>();
list.add(new Email地址("[email protected]","刘德华"));
mail.set收件人(list);
list = new ArrayList<Email地址>();
list.add(new Email地址("[email protected]","纯净水"));
list.add(new Email地址("[email protected]","刘德华"));
mail.set抄送人(list);
BodyPart contentPart = new MimeBodyPart();
contentPart.setHeader("Content-Transfer-Encoding", "base64");
contentPart.setContent("测试哟见呵呵","text/html;charset=utf-8");
mail.setCotnent(contentPart);
List<File> listF = new ArrayList<File>();
listF.add(new File("d:/출주제외되는2.rar"));
mail.set附件(listF);
Smtp发送邮件建造者 smtp = new Smtp发送邮件建造者();
Properties 属性配置 = new Properties();
属性配置.put("mail.smtp.host", "smtp.163.com");
属性配置.put("mail.smtp.auth", "true");
Session session = Session.getInstance(属性配置,new PasswordAuthenticator("xxxxx", "xxxxx"));
发送邮件Director director = new 发送邮件Director(smtp);
director.发送邮件建造(mail, session);
}
}
</script>