需求
想要使用郵件來監控服務器上的異常信息,當程序拋出指定自定義異常的時候將異常信息以郵件的形式發送到監控的郵箱。
要求
1、發生異常時捕獲異常信息
2、時實發送信息郵件到目標郵箱
解決方案
1、自定義一個基本的異常,其他的使用異常繼承改異常
2、該異常的構造方法接收信息參數和具體異常實體,然後生成郵件信息,發送郵件
3、具體的程序發生異常時捕獲拋出的異常,然後拋出一個自定義的基本異常的子類或他本身
自定義異常
用於異常信息採集和郵件發送
package exception;
import tool.MailTo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Created by yuyu on 2018/2/23.
* 基本的自定義異常,所有接下來的自定義異常繼承這個類
*/
public class BaseException extends RuntimeException {
public BaseException(String message) {
super(message);
}
public BaseException(String message, Throwable cause) {
super(message, cause);
//將異常信息發送出去
String title="DOBEONE發生異常!";
ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
cause.printStackTrace(new java.io.PrintWriter(buf, true));
//設置發送信息
String body = "<h1>"+title+"</h1>"
+"<p>異常信息:"+message+"</p>"
+"<p>"+buf.toString()+"</p>";
try {
buf.close();
} catch (IOException e1) {
e1.printStackTrace();
}
//按實際需要選擇
MailTo.sendMail163(title,body);//163郵箱
MailTo.sendMailOutLook(title,body);//outlook郵箱
}
}
郵件發送工具
支持163郵箱、outlook郵箱
package tool;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* 發送郵件工具類
* Created by yuyu on 2018/2/23.
*/
public class MailTo {
/**
* 發送郵件到自己的163郵箱
* @param title 需要傳輸的標題
* @param body 需要傳輸的內容
* @return
*/
public static boolean sendMail163(String title, String body) {
//設置參數
Properties props = new Properties();
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", 465);
//自定義信息
props.put("username", "[email protected]");//你的郵箱
props.put("password", "xxxx");//你的密碼
props.put("to", "[email protected]");//接收的郵箱
return MailTo.send(props,title,body);
}
/**
* 發送郵件到gmail
* 國內網絡無法訪問(因爲衆所周知的原因)
* @param title 標題
* @param body 內容
* @return
*/
public static boolean sendMailGmail(String title, String body){
//設置參數
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//自定義信息
props.put("username", "[email protected]");//你的郵箱
props.put("password", "xxxx");//你的密碼
props.put("to", "[email protected]");//接收的郵箱
return MailTo.send(props,title,body);
}
/**
* 發送郵件到outlook
* @param title 標題
* @param body 內容
* @return
*/
public static boolean sendMailOutLook(String title, String body){
//設置參數
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.outlook.com");
props.put("mail.smtp.port", "587");
//自定義信息
props.put("username", "[email protected]");//你的郵箱
props.put("password", "xxxx");//你的密碼
props.put("to", "[email protected]");//接收的郵箱
return MailTo.send(props,title,body);
}
/**
* 獲取系統當前的時間
* 以傳入時間格式返回,傳空返回默認格式
* @param format 時間格式
* @return
*/
private static String getTitleTimeFormat(String format){
if (format==null){
format="yyyy-MM-dd HH:mm:ss/SSS";
}
SimpleDateFormat df = new SimpleDateFormat(format);//設置日期格式
return df.format(new Date());// new Date()爲獲取當前系統時間
}
/**
* 發送郵件,獲取參數,和標題還有內容
* @param props 參數
* @param title 標題
* @param body 內容
* @return
*/
private static Boolean send(Properties props, String title, String body){
//發送郵件地址
final String username=props.getProperty("username");
//發送郵件名稱
final String password=props.getProperty("password");
//接收郵件地址
String to=props.getProperty("to");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(title+"("+MailTo.getTitleTimeFormat(null)+")");
message.setContent(body,"text/html;charset=utf-8");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("發送完畢!");
return true;
}
}
測試
測試代碼
package tool;
import exception.BaseException;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Created by yuyu on 2018/2/23.
* 用於測試異常郵件發送
*/
public class MailToTest {
@Test
public void testCatch(){
try{
throw new BaseException("測試",new Exception());
}catch (Exception e){
e.printStackTrace();
}
}
}
測試截圖
163郵件截圖
outlook郵件截圖
總結
1、雖然工具類中給出了gmail的方法,但是因爲衆所周知的原因無法訪問的,請使用telnet命令檢查您與服務器的連接,使用:telnet smtp.gmail.com 465
2、發送給別的郵箱可能會被當成垃圾郵件退信,請不要包含敏感字符
3、具體郵件的內容請自己修改美化