Java Mail Log输出

日志输出实现类

import org.apache.log4j.Logger;

import java.io.PrintStream;

/**
 * 扩展PrintStream,用于输出Java Mail打印Debug日志
 * <p>
 */
public class MailPrintStream extends PrintStream {
    private final Logger logger;

    public MailPrintStream(Logger logger) {
        super(System.out);
        this.logger = logger;
    }

    @Override
    public void print(String s) {
        logger.info(s);
    }

    @Override
    public void print(boolean b) {
        print(String.valueOf(b));
    }

    @Override
    public void print(char c) {
        print(String.valueOf(c));
    }

    @Override
    public void print(int i) {
        print(String.valueOf(i));
    }

    @Override
    public void print(long l) {
        print(String.valueOf(l));
    }

    @Override
    public void print(float f) {
        print(String.valueOf(f));
    }

    @Override
    public void print(double d) {
        print(String.valueOf(d));
    }

    @Override
    public void print(char[] s) {
        print(String.valueOf(s));
    }

    @Override
    public void print(Object obj) {
        print(String.valueOf(obj));
    }

    @Override
    public void println() {
        logger.info("");
    }

    @Override
    public void println(boolean x) {
        println(String.valueOf(x));
    }

    @Override
    public void println(char x) {
        println(String.valueOf(x));
    }

    @Override
    public void println(int x) {
        println(String.valueOf(x));
    }

    @Override
    public void println(long x) {
        println(String.valueOf(x));
    }

    @Override
    public void println(float x) {
        println(String.valueOf(x));
    }

    @Override
    public void println(double x) {
        println(String.valueOf(x));
    }

    @Override
    public void println(char[] x) {
        println(String.valueOf(x));
    }

    @Override
    public void println(String x) {
        logger.info(x);
    }

    @Override
    public void println(Object x) {
        println(String.valueOf(x));
    }

}

 

将实现类配置到DebugOut,并开启Debug模式

     Properties props = new Properties();
        props.setProperty("mail.transport.protocol", SMTP_MAIL_PROTOCOL);
        props.setProperty("mail.host", SMTP_MAIL_HOST);
        props.setProperty("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        session.setDebugOut(new MailPrintStream(log4j));
        session.setDebug(true);

        MimeMessage message = new MimeMessage(session);

 

posted @ 2023-08-28 16:01  为牛马者,背负前行  阅读(100)  评论(0编辑  收藏  举报

如果本页面列出的内容侵犯了您的权益,请告知。
知识共享许可协议
996.icu