分享
 
 
 

自定义SMTPAppender的源码

王朝java/jsp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

package piv.zheng.log4j.test;

import org.apache.log4j.AppenderSkeleton;

import org.apache.log4j.Layout;

import org.apache.log4j.helpers.LogLog;

import org.apache.log4j.spi.LoggingEvent;

import org.apache.log4j.spi.ErrorCode;

import java.util.Properties;

import java.util.Date;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import javax.mail.Multipart;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.AddressException;

public class SMTPAppender extends AppenderSkeleton {

private String to; //收件人

private String from; // 发件人

private String subject; //邮件标题

private String smtpHost; //邮件服务器

private String auth; //身份验证标识

private String username; //用户

private String password; //密码

private boolean locationInfo = false;

protected Session ses;

protected Message msg;

protected Properties props;

public SMTPAppender() {}

public SMTPAppender(Layout layout) {

this.layout = layout;

}

//应用设置

public void activateOptions() {

props = new Properties(System.getProperties());

if (smtpHost != null) {

props.put("mail.smtp.host", smtpHost);

}

if (auth != null) {

props.put("mail.smtp.auth", auth);

}

if (username != null) {

props.put("username", username);

}

if (password != null) {

props.put("password", password);

}

ses = Session.getInstance(props); //创建session

msg = new MimeMessage(ses); //创建message

try {

//为message设置发件人

if (from != null) {

msg.setFrom(getAddress(from));

} else {

msg.setFrom();

}

//为message设置收件人

if (to != null) {

msg.setRecipients(Message.RecipientType.TO, parseAddress(to));

}

//为message设置标题

if (subject != null) {

msg.setSubject(subject);

}

} catch (MessagingException e) {

LogLog.error("Could not activate SMTPAppender options.", e);

}

}

//重写执行方法,当执行日志输出时会被调用

public void append(LoggingEvent event) {

//检查message及layout

if (!checkEntryConditions()) {

return;

}

event.getThreadName();

event.getNDC();

if (locationInfo) {

event.getLocationInformation();

}

//发送邮件

sendBuffer(event);

}

protected boolean checkEntryConditions() {

if (this.msg == null) {

errorHandler.error("Message object not configured.");

return false;

}

if (this.layout == null) {

errorHandler.error("No layout set for appender named [" + name + "].");

return false;

}

return true;

}

synchronized public void close() {

this.closed = true;

}

InternetAddress getAddress(String addressStr) {

try {

return new InternetAddress(addressStr);

} catch (AddressException e) {

errorHandler.error("Could not parse address [" + addressStr + "].", e, ErrorCode.ADDRESS_PARSE_FAILURE);

return null;

}

}

InternetAddress[] parseAddress(String addressStr) {

try {

return InternetAddress.parse(addressStr, true);

} catch (AddressException e) {

errorHandler.error("Could not parse address [" + addressStr + "].", e, ErrorCode.ADDRESS_PARSE_FAILURE);

return null;

}

}

public boolean requiresLayout() {

return true;

}

//发送邮件

protected void sendBuffer(LoggingEvent event) {

try {

//将日志信息设置到message中

MimeBodyPart part = new MimeBodyPart();

StringBuffer sbuf = new StringBuffer();

String t = layout.getHeader();

if (t != null) {

sbuf.append(t);

}

sbuf.append(layout.format(event));

if (layout.ignoresThrowable()) {

String[] s = event.getThrowableStrRep();

if (s != null) {

for (int j = 0; j < s.length; j++) {

sbuf.append(s[j]);

}

}

}

t = layout.getFooter();

if (t != null) {

sbuf.append(t);

}

part.setContent(sbuf.toString(), layout.getContentType());

Multipart mp = new MimeMultipart();

mp.addBodyPart(part);

msg.setContent(mp); //为message设置内容,即日志信息

msg.setSentDate(new Date()); //为message设置发送时间

//发送邮件

Transport tr = ses.getTransport("smtp");

tr.connect(smtpHost, username, password);

tr.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));

tr.close();

} catch (Exception e) {

LogLog.error("Error occured while sending e-mail notification.", e);

}

}

public void setTo(String to) {

this.to = to;

}

public String getTo() {

return to;

}

public void setFrom(String from) {

this.from = from;

}

public String getFrom() {

return from;

}

public String getSubject() {

return subject;

}

public void setSubject(String subject) {

this.subject = subject;

}

public void setSMTPHost(String smtpHost) {

this.smtpHost = smtpHost;

}

public String getSMTPHost() {

return smtpHost;

}

public void setAuth(String auth) {

this.auth = auth;

}

public String getAuth() {

return auth;

}

public void setUsername(String username) {

this.username = username;

}

public String getUsername() {

return username;

}

public void setPassword(String password) {

this.password = password;

}

public String getPassword() {

return password;

}

public void setLocationInfo(boolean locationInfo) {

this.locationInfo = locationInfo;

}

public boolean getLocationInfo() {

return locationInfo;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有