package util;
import java.io.InputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/***
* Send edm to mail
* Jerry.li
* 2012-05-02
*/
public class EdmMail {
private static String MAIL_TITLE = null;
private String mailServerHost = "";
private String fromAddress = ""; //發送人郵箱地址
private String toAddress = ""; //收件人郵箱地址
private MimeMessage mimemsg; //Mime郵件對象
private Session session; //郵件會話對象
private Properties properties; //系統屬性
private Multipart part; //Multipart對象:郵件內容,標題,附件等內容均添加到其中後再生成MimeMessage對象
private String username = ""; //設置smtp的用戶名
private String password = ""; //設置smtp的密碼
private String profile = "search.properties";
private EdmMail(){
getValues();
setSmtpHost(mailServerHost);
getMailSession();
}
public EdmMail(String sub){
this();
MAIL_TITLE = sub;
}
private boolean getValues(){
Properties proties = null;
InputStream input = null;
try {
input = EdmMail.class.getClassLoader().getResourceAsStream(profile);
proties = new Properties();
proties.load(input);
mailServerHost = proties.getProperty("MAIL_HOST");
fromAddress = proties.getProperty("MAIL_FROM");
username = proties.getProperty("MAIL_USERNAME");
password = proties.getProperty("MAIL_PASSWORD");
toAddress = proties.getProperty("MAILTO_LIST");
return true;
} catch (Exception e) {
System.out.println("讀取配置文件出錯!"+e);
return false;
}
}
/***
* 設置SMTP主機
*/
private void setSmtpHost(String mailhost){
if(properties == null){
properties = System.getProperties(); //獲得系統屬性
}
properties.setProperty("mail.smtp.host", mailhost);
}
/***
* 獲得郵件會話對象
* 創建MimeMessage對象
*/
private boolean getMailSession(){
try {
session = Session.getDefaultInstance(properties,null);
} catch (Exception e) {
System.out.println("獲取郵件會話對象出錯,原因:"+e);
return false;
}
try {
mimemsg = new MimeMessage(session);
part = new MimeMultipart();
return true;
} catch (Exception e) {
System.out.println("創建Mime郵件對象出錯,原因:"+e);
return false;
}
}
/***
* 設置smtp身份認證
* mail.smtp.auth = true
*/
private void setSmtpAuth(boolean bool){
if(properties == null){
properties = System.getProperties(); //獲得系統屬性
}
if(bool){
properties.put("mail.smtp.auth", "true");
}else{
properties.put("mail.smtp.auth", "false");
}
}
/***
* 設置郵件的主題
*/
private boolean setMailSub(String mailsubject){
try {
mimemsg.setSubject(mailsubject, "GBK");
return true;
} catch (Exception e) {
System.out.println("設置郵件標題出錯,原因:"+e);
return false;
}
}
/***
* 設置郵件體格式
*/
private boolean setMailBody(String mailBody){
BodyPart bdyPart = new MimeBodyPart();
try {
bdyPart.setContent(mailBody, "text/html;charset=GBK");
part.addBodyPart(bdyPart);
return true;
} catch (Exception e) {
System.out.println("設置郵件體格式出錯,原因:"+e);
return false;
}
}
/***
* 添加郵件附件
*/
private boolean addAttach(String filePath){
BodyPart bdy = new MimeBodyPart();
try {
FileDataSource dataSource = new FileDataSource(filePath);
bdy.setDataHandler(new DataHandler(dataSource));
bdy.setFileName(dataSource.getName()); //設置附件名
part.addBodyPart(bdy);
return true;
} catch (Exception e) {
System.out.println("添加附件:"+filePath+"出錯,原因:"+e);
return false;
}
}
/***
* 設置郵件發送人
*/
private boolean setMailFrom(String from){
try {
mimemsg.setFrom(new InternetAddress(from));
return true;
} catch (Exception e) {
System.out.println("設置郵件發送人:"+from+"出錯,原因:"+e);
return false;
}
}
/***
* 設置郵件接收人
*/
private boolean setMailTo(String mailto){
if(mailto == null){
return false;
}
try {
mimemsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailto));
return true;
}catch (Exception e) {
System.out.println("設置郵件接收人:"+mailto+"出錯,原因:"+e);
return false;
}
}
/***
* 發送郵件
*/
private boolean sendout(){
try {
mimemsg.setContent(part);
mimemsg.saveChanges();
System.out.println("開始發送郵件......");
Session mailSession = Session.getInstance(properties, null);
Transport tsport = mailSession.getTransport("smtp");
tsport.connect((String)properties.get("mail.smtp.host"), username, password);
tsport.sendMessage(mimemsg, mimemsg.getRecipients(Message.RecipientType.TO));
tsport.close();
System.out.println("發送郵件成功!");
return true;
} catch (Exception e) {
System.out.println("發送郵件出錯原因:"+e);
return false;
}
}
//
public void sendMail(String fileAttach){
//拼接整個郵件的內容
StringBuffer content = new StringBuffer();
//頭部
content.append("<html>");
content.append("<head><META content='text/html; charset=gbk' http-equiv=Content-Type>");
content.append("<META name=GENERATOR content='MSHTML 8.00.6001.18702'>");
content.append("<style type='text/css'>");
content.append(".STYLE1 {color: #000000}");
content.append("TABLE {FONT-SIZE: 12px; COLOR: #444444;LINE-HEIGHT: 14px; FONT-FAMILY: '宋體', 'Arial'; TEXT-DECORATION: none;}");
content.append(".STYLE3 { font-size: 13px;color: #FD9800; font-weight: bold;}");
content.append("</style>");
content.append("</head>");
//顯示郵件內容
content.append("<body>");
content.append("<table width='749px' align='left'>");
content.append("<tr><td width='749' height='10px'></td></tr>");
content.append("<tr><td><p align='left'><font size=2>for test!</font></p></td></tr>");
content.append("</table>");
//content.append("");
content.append("</body>");
content.append("</html>");
System.out.println("content********"+content);
setSmtpAuth(false);
if(setMailSub(MAIL_TITLE) == false){
return;
}
if(setMailBody(content.toString()) == false){
return;
}
if(setMailTo(toAddress) == false){
return;
}
if(setMailFrom(fromAddress) == false){
return;
}
if(addAttach(fileAttach) == false){
return;
}
if(sendout()== false){
return;
}
}
public static void main(String[] args) {
EdmMail m = new EdmMail("test");
//附件的位置
m.sendMail("D:\\workspace\\test.xls");
}
}