原文链接:http://blog.sina.com.cn/s/blog_56410d650100jl9b.html
所需jar包:在apache官网下载相关最新的jar包,并在项目中引用(activation.jar,mail.jar),此外还有log4j的jar包
以下为配置文件
================================================================================================
# Global logging configuration
log4j.rootLogger=info, stdout,D,MAIL
### 输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss,SSS} [%5p] %c{1}:%l - %m%n
### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${catalina.home}/logs/GEWS_MSP.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = info ## 输出info级别以上的日志
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %d{yyyy-MM-ddHH:mm:ss,SSS} [%5p] %c{1}:%l - %m%n
### send error through email.
# log4j的邮件发送appender,如果有必要你可以写自己的appender
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
#发送邮件的门槛,仅当等于或高于ERROR(比如FATAL)时,邮件才被发送
log4j.appender.MAIL.Threshold=ERROR
#邮件缓冲区大小
log4j.appender.MAIL.BufferSize=1024
#发送邮件的邮箱帐号
log4j.appender.MAIL.From=****@126.com
#SMTP邮件发送服务器地址
log4j.appender.MAIL.SMTPHost=smtp.126.com
#SMTP发送认证的帐号名
log4j.appender.MAIL.SMTPUsername=****@126.com
#SMTP发送认证帐号的密码
log4j.appender.MAIL.SMTPPassword=*****
#是否打印调试信息,如果选true,则会输出和SMTP之间的握手等详细信息
log4j.appender.MAIL.SMTPDebug=true
#邮件主题
log4j.appender.MAIL.Subject=GEWS_SYSTEM_ERROR(192.168.2.39)
#发送到什么邮箱,如果要发送给多个邮箱,则用逗号分隔;
#如果需要bcc给某人,则加入下列行:
#[email protected]
[email protected],[email protected]
log4j.appender.MAIL.layout=com.zjxcsoft.util.MailLayout_seth
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c-%-4r [%t] %-5p %c %x - %m%n
中文乱码解决:
==================================================================================================
package com.zjxcsoft.util;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
public class MailLayout_seth extendsLayout{
StringBuffersbuf;
@Override
public StringgetContentType()
{
return"text/html;charset=utf-8";
}
public MailLayout_seth(){
sbuf = newStringBuffer(128);
}
@Override
public String format(LoggingEventevent)
{
sbuf.setLength(0);
sbuf.append("错误等级:"+event.getLevel().toString()+"
");
sbuf.append("错误原因:"+event.getMessage().toString()+"
");
sbuf.append("错误的类:"+event.getLocationInformation().getClassName()+"
");
sbuf.append("错误方法:"+event.getLocationInformation().getMethodName()+"
");
sbuf.append("错误位置:"+event.getLocationInformation().getLineNumber()+"行");
return sbuf.toString();
}
@Override
public boolean ignoresThrowable(){
// TODO Auto-generated methodstub
return false;
}
public void activateOptions(){
// TODO Auto-generated methodstub
}
}
自定义发送日志级别:
1.首先我测试的时候是用的info的级别,所以在发送mail的过程中出错了,信息都不会打印出来,并且mail也收不到,我就很奇怪,然后我把log的级别调成error,控制台才打印出了mail 发送失败的log信息,经查阅发现:Log4j的SMTP的级别默认是ERROR级别
默认是ERROR级别,那就是说,只有程序出错了,才可以收到邮件。不过可以自定义的级别,继承TriggeringEventEvaluator类,具体实现见下方代码:
注意事项:
===========================================================================================
邮件方式一般只处理ERROR以上异常。
邮件发送服务器请选择126或者163的,经测试sina的发送服务器很可能导致发送失败
SMTP之间的握手信息:
============================================================================================
EHLO seth-PC
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrUaXObUCa0xDrUUUUj
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "coremail", arg"1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrUaXObUCa0xDrUUUUj"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 dXNlcm5hbWU6
c2V0aDM0QDEyNi5jb20=
334 UGFzc3dvcmQ6
Y3gzNDA2NTIw
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 Mail OK
RCPT TO:<[email protected]>
250 Mail OK
RCPT TO:<[email protected]>
250 Mail OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP: [email protected]
DEBUG SMTP: [email protected]
DATA
354 End data with
Message-ID: <31013261.01278665286649.JavaMail.seth@seth-PC>
Date: Fri, 9 Jul 2010 08:48:06 +0000 (GMT)
From: [email protected]
To: [email protected], [email protected]
Subject: GEWS_SYSTEM_ERROR(192.168.2.39)
MIME-Version: 1.0
Content-Type: multipart/mixed;boundary="----=_Part_0_5034054.1278665286559"
------=_Part_0_5034054.1278665286559
Content-Type: text/html;charset=utf-8
Content-Transfer-Encoding: quoted-printable
=E9=94=99=E8=AF=AF=E7=AD=89=E7=BA=A7=EF=BC=9AERROR
=E9=94=99=E8=AF=AF=E5=
=8E=9F=E5=9B=A0=EF=BC=9Atestdddddddddddddddddddddddddd
=E9=94=99=E8=AF=
=AF=E7=9A=84=E7=B1=BB=EF=BC=9Acom.zjxcsoft.webservice_msp.WebServiceImpl_MS=
P
=E9=94=99=E8=AF=AF=E6=96=B9=E6=B3=95=EF=BC=9AWsAccpData_MSP
=E9=94=
=99=E8=AF=AF=E4=BD=8D=E7=BD=AE=EF=BC=9A15=E8=A1=8C
------=_Part_0_5034054.1278665286559--
.
250 Mail OK queued as smtp7,DsmowKArmANA4jZMbIQJAA--.3261S21278665281
QUIT
2010-07-09 08:48:07,730 [FATAL]WebServiceImpl_MSP:com.zjxcsoft.webservice_msp.WebServiceImpl_MSP.WsAccpData_MSP(WebServiceImpl_MSP.java:16)- test###########################
DEBUG: getProvider() returningjavax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,SunMicrosystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.126.com", port 25,isSSL false
220 126.com Anti-spam GT for Coremail System(126com[20090903])
DEBUG SMTP: connected to host "smtp.126.com", port: 25