• Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaMail + Log4j = SMTPAppender (Authentication question)  RSS feed

 
Horaci Macias
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use jakarta's Log4j SMTPAppender to send emails with logging information. The problem is that the SMTP host I use is returning me an error like this:

I'm sure this SMTP host allows delivery to non-local recipients because I can do it with my email client, so I'm trying to authenticate first.
I've been looking through javamail & log4j documentation and I still haven't found how to use javax.mail.Authenticator class. I thought I had to extend it, but all its methods are final
Any help would be much appreciated,
Horaci Macias
 
Chris Butler
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Horaci Macias:
I've been looking through javamail & log4j documentation and I still haven't found how to use javax.mail.Authenticator class. I thought I had to extend it, but all its methods are final
Horaci Macias[/QB]

Here's what I did to extend the log4j SMTP Appender:
package org.apache.log4j.net;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.AddressException;
import java.util.Properties;
import org.apache.log4j.helpers.LogLog;
//import org.apache.log4j.net.SMTPAppender;
import org.apache.log4j.spi.ErrorCode;
public class AuthSMTPAppender extends SMTPAppender {
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it. It pulls the username
* and password from log4j.properties or where appropriate.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = _user;
String password = _password;
return new PasswordAuthentication(username, password);
}
}
private String _user;
private String _password;
public String getUser() {
return _user;
}
public void setUser(String user) {
_user = user;
}
public String getPassword() {
return _password;
}
public void setPassword(String password) {
_password = password;
}
/**
* Overrode activeOptions() to have authentication
*/
public void activateOptions() {
Properties props = new Properties (System.getProperties());
if (getSMTPHost() != null) {
props.put("mail.smtp.host", getSMTPHost());
}
// New authentication bit
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// Session session = Session.getInstance(props, null);
//session.setDebug(true);
msg = new MimeMessage(session);
try {
if (getFrom() != null) {
msg.setFrom(getAddress(getFrom()));
} else {
msg.setFrom();
}
msg.setRecipients(Message.RecipientType.TO, parseAddress(getTo()));
if (getSubject() != null) {
msg.setSubject(getSubject());
}
} catch(MessagingException e) {
LogLog.error("Could not activate SMTPAppender options.", e );
}
}
}
Here's the log4j.properties file entries:
log4j.appender.email=org.apache.log4j.net.AuthSMTPAppender
log4j.appender.email.threshold=ERROR
log4j.appender.email.SMTPHost=<smtp.somedomain.com>
log4j.appender.email.BufferSize=1
log4j.appender.email.user=<your username>
log4j.appender.email.password=<your password>
log4j.appender.email.subject=<Whatever subject>
log4j.appender.email.from=<you@your.address>
log4j.appender.email.to=<recepient1@domain1.com>
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%d{ISO8601} %5p [%t] (%F:%L) - %m%n
The elements within the gt/lt signs (< > should just be replaced by your appropriate information.
Hope I've been clear enough and that helps. I'm tempted to send on this little fix to jakarta. Seems simple enough.
Chris
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!