前段時間在工做中用到了郵件發送監控的報警信息,今天在這個記錄一下JavaMail的郵件工具類。html
下邊爲用到的JavaMail的jar包的pom依賴。這裏用的是JavaMail的1.4.6的版本。java
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.6</version> </dependency>
這裏是工具類中的用到的郵件的一些參數對象的實體類。 apache
package com.juihai.entity; import java.util.Arrays; public class EmailTO { private String title;//郵件主題 private String content;//郵件內容 private String sender;//發件人帳號 private String password;//發件人密碼 private String host;//發件郵箱服務器 private String[] toAdress;//收件人地址 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String[] getToAdress() { return toAdress; } public void setToAdress(String[] toAdress) { this.toAdress = toAdress; } @Override public String toString() { return "EmailTO [title=" + title + ", content=" + content + ", sender=" + sender + ", password=" + password + ", host=" + host + ", toAdress=" + Arrays.toString(toAdress) + "]"; } }
這裏是就是發送Email的工具類。具體說明件代碼中的註釋。服務器
import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; import com.juihai.entity.EmailTO; public class SendEmailUtils { /** 日誌輸出 */ private static Logger log = Logger.getLogger(SendEmailUtils.class); public static boolean send(EmailTO to) throws GeneralSecurityException{ final String user = to.getSender(); final String password = to.getPassword(); log.info("SendEmailUtils >>>> Start Send Mail"); Properties properties = System.getProperties();// 獲取系統屬性 properties.setProperty("mail.smtp.host", to.getHost());// 設置郵件服務器 "smtp.163.com",這是發件人的郵箱服務器 properties.put("mail.smtp.auth", "true"); String senderNick = null;
//部分郵箱服務器須要開啓SSL驗證才能夠發送郵件,須要的話,開啓註釋便可 /*MailSSLSocketFactory sf = new MailSSLSocketFactory();// SSL驗證 begin sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf);// SSL驗證 end*/ Session session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); // 發件人郵件用戶名、密碼 } }); try { MimeMessage message = new MimeMessage(session);// 建立默認的 MimeMessage對象 senderNick = javax.mail.internet.MimeUtility.encodeText("Juihai");//設置發件人暱稱 message.setFrom(new InternetAddress(senderNick+"<"+user+">")); //message.addRecipient(Message.RecipientType.TO,new InternetAddress());//建立單個收件人 String[] tos = to.getToAdress();//建立多個收件人 if (tos != null && tos.length != 0) { InternetAddress[] toAddress = new InternetAddress[tos.length]; for (int i = 0; i < tos.length; i++) { toAddress[i] = new InternetAddress(tos[i]); } message.setRecipients(Message.RecipientType.TO, toAddress); } message.setSubject(to.getTitle());//設置郵件主題 //message.setText(content);//發送純文本內容 message.setContent(to.getContent(), "text/html;charset=utf-8");//發送html郵件內容 Transport.send(message);//發送Email log.info(" SendEmailUtils郵件發送成功"); return true; }catch (MessagingException mex) { mex.printStackTrace(); log.info(" >>>>SendEmailUtils郵件發送失敗"+mex); } catch (UnsupportedEncodingException e) { log.info(" >>>>SendEmailUtils郵件發送失敗-設置發件人暱稱error"+e); e.printStackTrace(); } return false; } }