Log4j, SMTPAppender, and Authentication
Post Info Tuesday, September 29th, 2009 7:17 pm by Earl Print Print this page

I was trying to hook up my log4j configuration to my gmail account so that I could be immediately alert for errors. Pretty simple use case… But I started getting errors like:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first.

and

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Authentication required

I have reason to believe SMTPAppender is borked on anything authentication related because your authentication properties are read after the javax.mail.Session is created. The following is code for an overridden SMTPAppender that takes over the session creation.

import java.security.Security;
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import org.apache.log4j.net.SMTPAppender;

public class SMTPSSLAppender extends SMTPAppender {

	public SMTPSSLAppender() {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	}

	@Override
	protected Session createSession() {
		Properties properties = new Properties();
		properties.setProperty("mail.transport.protocol", "smtp");
		properties.setProperty("mail.host", getSMTPHost());
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.port", "465");
		properties.put("mail.smtp.socketFactory.port", "465");
		properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
		properties.put("mail.smtp.socketFactory.fallback", "false");
		properties.setProperty("mail.smtp.quitwait", "false");

		Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication()
			{ return new PasswordAuthentication(getSMTPUsername(),getSMTPPassword());	}
		});		

		return session;
	}
}

And the log4j.properties would have something like:

log4j.appender.mail=SMTPSSLAppender
log4j.appender.mail.SMTPHost=smtp.gmail.com
log4j.appender.mail.SMTPUsername=myusername
log4j.appender.mail.SMTPPassword=mypassword
log4j.appender.mail.BufferSize=1
log4j.appender.mail.Subject=ZOMG some error occured!
log4j.appender.mail.To=my@email.com
log4j.appender.mail.threshold=error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

Of course, overriding session creation breaks a lot of things, but it still has the 20% of functionality that 80% of folks will use.

32 Responses to “ Log4j, SMTPAppender, and Authentication ”

  1. LEO


    Medicamentspot.com. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. No prescription pills. Order drugs online

    Buy:Lumigan.Prednisolone.Zyban.Valtrex.Zovirax.Synthroid.Prevacid.Human Growth Hormone.Nexium.Mega Hoodia.Arimidex.Retin-A.Accutane.Petcam (Metacam) Oral Suspension.100% Pure Okinawan Coral Calcium.Actos….

  2. lamp

    Floor http://vphillipsj-ih9.01DODGEPARTS.US/tag/Twin+lamp+Floor/ : Floor…

    Twin…

  3. RENE


    CheapTabletsOnline.com. Canadian Health&Care.Best quality drugs.No prescription online pharmacy.Special Internet Prices. Low price pills. Order pills online

    Buy:Mega Hoodia.100% Pure Okinawan Coral Calcium.Zyban.Petcam (Metacam) Oral Suspension.Lumigan.Valtrex.Actos.Human Growth Hormone.Synthroid.Accutane.Nexium.Zovirax.Prednisolone.Retin-A.Arimidex.Prevacid….

  4. ROBERTO


    CheapTabletsOnline.Com. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. High quality pills. Buy drugs online

    Buy:Valtrex.Lumigan.Zyban.Retin-A.100% Pure Okinawan Coral Calcium.Prevacid.Arimidex.Zovirax.Human Growth Hormone.Petcam (Metacam) Oral Suspension.Mega Hoodia.Synthroid.Prednisolone.Accutane.Nexium.Actos….

  5. Antique

    Antique http://uqa.yc6.ll1.co : Trader…

    Auto…

  6. JOE


    NEW FASHION store. Original designers collection at low prices!!! 20 % TO 70 % OFF. END OF SEASON SALE!!!

    BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…

  7. ERNEST

    ANDREW

    order cialis in canada online

    Buyno prescription…

  8. GUY

    ENRIQUE

    SERGIO

    CURTIS

    DARRYL

    e-code for zometa

    Buyit now…

  9. MAX

    cats zanaflex

    Buydrugs without prescription…

  10. KENNETH

    Zaditor

    Buydrugs without prescription…

  11. TROY

    Zyrtec

    Buyit now…

  12. JEFF

    Zyprexa

    Buynow it…

  13. LUIS

    Prozac

    Buygeneric meds…

  14. BRIAN

    JARED

    DWAYNE

    JULIO

    LLOYD

    .

    Buynow…

  15. RALPH

    JARED

    ANDREW

    JERRY

    .

    Buyno prescription…

  16. TROY

    THOMAS

    OLIVER

    ANTONIO

    ERNESTO

    Buygeneric drugs…

Leave a Reply

Get updates as often as we post! Subscribe to our full feed or comments feed.