XMail下载地址
http://download.csdn.net/detail/ivanhyx/9058923
此下载地址为相关jar包及此Demo
简单几步,轻松实现发送邮件,把javamail进行了包装。支持向多人发送,多人抄送。多附件,纯文本及html格式邮件。
import java.util.Date;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
public class XMailDemo {
/**收件人地址*/
private String TOAddress;
/**抄送人地址*/
private String CCAddress;
/**密送人地址*/
private String BCCAddress;
/**邮件主题*/
private String email_subject;
/**smtp*/
private String smtp;
/**帐号*/
private String username;
/**密码*/
private String password;
/**发送html格式邮件*/
private String htmlcontent;
/**发送存文本格式邮件*/
private String textcontent;
{
this.smtp = "smtp.qq.com";
this.username = "12345678@qq.com";
this.password = "12345678";
this.TOAddress = "11111111@qq.com,22222222@163.com,33333333@sina.com";
this.CCAddress = "55555555@qq.com";
this.BCCAddress = "66666666@qq.com";
this.email_subject = "系统自动发送邮件";
this.htmlcontent = "此邮件为html格式,图片1:<img src='cid:a.jpg'>图片2:<img src='cid:b.jpg'>";
this.textcontent = "此邮件为存文本格式";
}
/**
* @param args
*/
public void sendMail() {
XMail xmail = new XMail();
//获取Message对象
Message
message = xmail.getMessage(smtp , username , password);
try {
// 设置邮件的发件人
message.setFrom(new InternetAddress( username ));
// 设置邮件的收件人
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TOAddress));
//抄送人
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(CCAddress));
//密送人
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(BCCAddress));
//设置邮件的主题
message.setSubject(email_subject);
//发送日期
message.setSentDate(new Date());
/********************发送纯文本邮件**********************/
/*MimeBodyPart mbp1 = xmail.xTextMainBody("此邮件为纯文本格式");
//无附件,使用此方法。
//MimeMultipart mm1 = xmail.xSyn( mbp1 );
//带附件,支持多附件。String数组中为附件路径
MimeMultipart mm1 = xmail.xSyn( mbp1 , new String[]{"img//a1.rar","img//jna.zip"});
message.setContent(mm1);*/
/****************************************************/
/********************发送Html格式邮件*******************/
//邮件正文,如果html格式邮件含有图片,用此方法,String数组为图片路径,其中cid为文件名。
MimeBodyPart mbp2 = xmail.xHtmlMainBody(
"此邮件为html格式,图片1:<img src='cid:a.jpg'>图片2:<img src='cid:b.jpg'>",
new String[]{"img/b.jpg", "img/a.jpg"}
);
//无附件,使用此方法。
//MimeMultipart mm1 = xmail.xSyn( mbp2 );
//带附件,支持多附件。String数组中为附件路径
MimeMultipart mm2 = xmail.xSyn( mbp2 , new String[]{"img//f1.rar","img//f2.zip"});
message.setContent(mm2);//设置内容
/*****************************************************/
message.saveChanges();//保存修改
Transport.send(message);//发送邮件
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("邮件发送成功");
}
public static void main(String[] args) {
XMailDemo xmd = new XMailDemo();
xmd.sendMail();
}
}