posts - 165, comments - 198, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

logg4j 使用笔记

Posted on 2008-07-02 11:20 G_G 阅读(727) 评论(0)  编辑  收藏 所属分类: javaGeneral
参考:http://www.builder.com.cn/2008/0517/866964.shtml

上面很全文就不说了。

在这logger邮件,总不成功。在此我 重写:
log4j.appender.MAIL=uu.Mail
#defines how often emails are send
log4j.appender.MAIL.BufferSize
=1
log4j.appender.MAIL.threshold
=error


log4j.appender.MAIL.SMTPHost
=mail.chinanet.net.cn
log4j.appender.MAIL.
From=wap@chinanet.net.cn
log4j.appender.MAIL.
To=liukaiyi@gmail.com
log4j.appender.MAIL.Subject
=Application.log error occurred

log4j.appender.MAIL.layout
=org.apache.log4j.PatternLayout
log4j.appender.MAIL.SMTPUsername
=wap
log4j.appender.MAIL.SMTPPassword
=aaaaaa
log4j.appender.MAIL.layout.ConversionPattern
=%%5p [%c] %n%m%n





package uu;


import java.util.Date;
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.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.net.SMTPAppender;



public class Mail extends SMTPAppender {
    
public void activateOptions() {
        Mail sml 
= new Mail();
        sml.send(
"wap","aaaaaa","liukaiyi@gmail.com","logg4j"," 邮件正文 测试开始 ");
        System.out.println(
"邮件发送成功!");
    }
    
    
// goMail 收邮件邮箱  mailContent 邮件正文
    public static void sendMail(String goMail,String mailContent){
        Mail sml 
= new Mail();
        sml.send(
"wap","aaaaaa",goMail,"订单管理",mailContent);
        System.out.println(
"邮件发送成功!");
    }
    
public void send(String name,String pass,String goMail,String title,String mailContent){
        
try
        {
            Properties props 
= new Properties();
            Session sendMailSession;
            Store store;
            Transport transport;
            props.put(
"mail.smtp.auth","true");
            props.put(
"mail.smtp.host""mail.chinanet.net.cn"); //smtp
            
            props.put(
"mail.smtp.user",name+"@chinanet.net.cn"); //
            
            
            props.put(
"mail.smtp.password",pass); //�ʼ����
            PopupAuthenticator popA=new PopupAuthenticator();//
            PasswordAuthentication pop = popA.performCheck(name+"@chinanet.net.cn",pass); //��д�û�������
            sendMailSession = Session.getInstance(props, popA);
            Message newMessage 
= new MimeMessage(sendMailSession);
            newMessage.setFrom(
new InternetAddress(name+"@chinanet.net.cn"));
            
            newMessage.setRecipient(Message.RecipientType.TO, 
new InternetAddress(goMail));  //���շ��ʼ���ַ
            newMessage.setSubject(title);
            newMessage.setSentDate(
new Date());
            mailContent
+="\n\n"+new Date().toLocaleString();
            newMessage.setText(mailContent); 
//�ʼ�����
            transport = sendMailSession.getTransport("smtp");
            transport.send(newMessage);
        }
        
catch (MessagingException ex)
        {
            ex.printStackTrace();
        }
    }

    
public class PopupAuthenticator extends Authenticator{
        String username
=null;
        String password
=null;
        
public PopupAuthenticator(){}
        
public PasswordAuthentication performCheck(String user,String pass){
            username 
= user;  
            password 
= pass; 
            
return getPasswordAuthentication();
        }
        
protected PasswordAuthentication getPasswordAuthentication() {
            
return new PasswordAuthentication(username, password);
        }
    }
    
}


只有注册用户登录后才能发表评论。


网站导航: