抛异常后自动发报警邮件

一共需要写3个类,

一,已126邮箱发给QQ邮箱为例,首先去126邮箱开启授权码,参考链接如下:

https://jingyan.baidu.com/article/9faa72318b76bf473c28cbf7.html

二,BaseException 继承RuntimeException,在BaseException 的构造中写发邮件逻辑

/**  

* <p>Title: BaseException.java</p>  

* <p>Description: </p>  

* <p>Copyright: Copyright (c) 2017</p>  

* <p>Company: www.cruiseloveashley.com</p>  

* @author 彭闯  

* @date 2018年10月10日  

* @version 1.0  

*/ 
package com.qwkg.mailsent;

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>"
                +"<img src=\"http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg\" style=\"top: 0px; left: 155px; width: 517.333px; height: 291px; cursor: pointer;\"  title=\"点击查看源网页\" width=\"490\" height=\"275.625\">";
        
        try {
            buf.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        //按实际需要选择
        MailTo.sendMail126(title,body);//163邮箱
//        MailTo.sendMailOutLook(title,body);//outlook邮箱
    }
}

三,发邮件的工具类

/**  

* <p>Title: MailTo.java</p>  

* <p>Description: </p>  

* <p>Copyright: Copyright (c) 2017</p>  

* <p>Company: www.cruiseloveashley.com</p>  

* @author 彭闯  

* @date 2018年10月10日  

* @version 1.0  

*/
package com.qwkg.mailsent;

/**  

* <p>Title: www</p>  

* <p>Description: </p>  

* @author 彭闯  

* @date 2018年10月10日  

*/

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 pengchuang on 2018/2/23.
 */
public class MailTo {

	/**
	 * 发送邮件到自己的126邮箱
	 * 
	 * @param title
	 *            需要传输的标题
	 * @param body
	 *            需要传输的内容
	 * @return
	 */
	public static boolean sendMail126(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.126.com");
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.port", 465);
		// 自定义信息
		props.put("username", "xxx@126.com");// 你的邮箱
		props.put("password", "xxx");// 你的授权码
		props.put("to", "xxx@qq.com");// 接收的邮箱

		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", "xxxx@gmail.com");// 你的邮箱
		props.put("password", "xxxx");// 你的密码
		props.put("to", "xxxx@gmail.com");// 接收的邮箱

		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", "xxxx@outlook.com");// 你的邮箱
		props.put("password", "xxxx");// 你的密码
		props.put("to", "xxxx@outlook.com");// 接收的邮箱

		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;
	}

}

四,测试类

/**  

* <p>Title: eee.java</p>  

* <p>Description: </p>  

* <p>Copyright: Copyright (c) 2017</p>  

* <p>Company: www.cruiseloveashley.com</p>  

* @author pengchuang  

* @date 2018年10月10日  

* @version 1.0  

*/ 
package com.qwkg.mailsent;

/**
 * Created by pengchuang on 2018/2/23.
 * 用于测试异常邮件发送
 */
public class MailToTest {
    public static void main(String[] args) {
    	try{
    		int a = 1/0;
        }catch (ArithmeticException e){
            e.printStackTrace();
            throw new BaseException("测试",e);
        }
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值