logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From DeMZed <dem...@gmail.com>
Subject Re: SMTPAppender : how to specify the port ?
Date Wed, 23 Aug 2006 08:31:26 GMT
Thanks, but I've very quickly coded mine :

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.net.SMTPAppender;
import org.apache.log4j.spi.ErrorCode;

public class MySMTPAppender extends SMTPAppender {
    private String smtpPort;

    public String getSMTPPort() {
        return this.smtpPort;
    }

    public void setSMTPPort(String smtpPort) {
        this.smtpPort = smtpPort;
    }

    /**
     * @see org.apache.log4j.spi.OptionHandler#activateOptions()
     */
    public void activateOptions() {
        Properties props = new Properties(System.getProperties());
        if (super.getSMTPHost() != null)
            props.put("mail.smtp.host", super.getSMTPHost());
        if (smtpPort != null) {
            props.put("mail.smtp.port", smtpPort);
        }

        Session session = Session.getInstance(props, null);
        //session.setDebug(true);
        msg = new MimeMessage(session);

        try {
            if (super.getFrom() != null)
                msg.setFrom(getAddress(super.getFrom()));
            else
                msg.setFrom();

            msg.setRecipients(Message.RecipientType.TO, parseAddress(super
                    .getTo()));
            if (super.getSMTPHost() != null)
                msg.setSubject(super.getSMTPHost());
        } catch (MessagingException e) {
            LogLog.error("Could not activate SMTPAppender options.", e);
        }

    }

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

2006/8/23, Patrick Wyss <Patrick.Wyss@mobilesolutions.ch>:
> i have a "AuthSMTPAppender" which i could easyly extend to set the port as well as the
authentification.
> if you're interessted...
>
>
> > -----Urspr√ľngliche Nachricht-----
> > Von: DeMZed [mailto:demzed@gmail.com]
> > Gesendet: Dienstag, 22. August 2006 21:06
> > An: 'Log4J Users List'
> > Betreff: SPAM: RE: SMTPAppender : how to specify the port ?
> >
> >
> > Exactly.
> >
> > It seems that I must create a new class MySMTPAppender extending
> > SMTPAppender :(
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message