public boolean sendMail(Map<String,String> senderInfo,String[] mail_to, String subject, String content)
{
try
{
// 獲取系統環境
Properties props = new Properties();
Authenticator auth = new Email_Autherticator(senderInfo.get("account"),senderInfo.get("password"));
props.put("mail.smtp.host", senderInfo.get("smtp"));
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, auth);
// 設置session,和郵件服務器進行通訊。
MimeMessage message = new MimeMessage(session);
// fileEncode 當前文件編碼,gb2312轉換的編碼
// 獲取當前文件編碼
String fileEncode = GetComputer.getPropertieByName("file.encoding");
//content = new String(content.getBytes(fileEncode),"gb2312");
//message.setContent(content, "text/html;charset=gb2312");
message.setSubject(subject); // 設置郵件主題
//message.setText(content); // 設置郵件正文
message.setHeader("DropBox", "Authenticate"); // 設置郵件標題
message.setSentDate(new Date()); // 設置郵件發送日期
// 郵件地址,發件人name
Address address = new InternetAddress(senderInfo.get("addr"), "MashupMail");
message.setFrom(address); // 設置郵件發送者的地址
// 設置郵件接收方的地址
Address[] toAddress = new Address[mail_to.length];
for(int i=0;i<mail_to.length;i++)
{
toAddress[i] = new InternetAddress(mail_to[i]);
}
// 設置要顯示的收件人
message.setRecipients(Message.RecipientType.TO, toAddress);
MimeMultipart mm = new MimeMultipart();
this.setMMPWithImgSource(mm);
// 這句很重要,千萬不要忘了
mm.setSubType("related");
// 新建一個存放信件內容的BodyPart對象
BodyPart mdp = new MimeBodyPart();
// 給BodyPart對象設置內容和格式/編碼方式
mdp.setContent(content.toString(), "text/html;charset=GBK");
mm.addBodyPart(mdp);
message.setContent(mm);
Transport.send(message,toAddress); // 發送郵件
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
/**
* 用來進行服務器對用戶的認證
*/
private class Email_Autherticator extends Authenticator
{
private String account;
private String password;
public Email_Autherticator(String account,String password)
{
super();
this.account = account;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(account,password);
}
}
/**
* 加入圖片資源
* @param mm
* @throws MessagingException
*/
private void setMMPWithImgSource(MimeMultipart mm) throws MessagingException
{
String path = Constants.HUDSON_ROOT_PATH + "/images/sonarImgs/";
FileDataSource blocker_fds = new FileDataSource(path+"BLOCKER.png");
FileDataSource critical_fds = new FileDataSource(path+"CRITICAL.png");
FileDataSource info_fds = new FileDataSource(path+"INFO.png");
FileDataSource major_fds = new FileDataSource(path+"MAJOR.png");
FileDataSource minor_fds = new FileDataSource(path+"MINOR.png");
mm.addBodyPart(getMbp(blocker_fds, "BLOCKER"));
mm.addBodyPart(getMbp(critical_fds, "CRITICAL"));
mm.addBodyPart(getMbp(info_fds, "INFO"));
mm.addBodyPart(getMbp(major_fds, "MAJOR"));
mm.addBodyPart(getMbp(minor_fds, "MINOR"));
}
private MimeBodyPart getMbp(FileDataSource fds, String imgId) throws MessagingException
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setDataHandler(new DataHandler(fds));
mbp.setContentID(imgId);
return mbp;
}