Tuesday, September 29th, 2009 7:17 pm by
Earl
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.
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….
Floor http://vphillipsj-ih9.01DODGEPARTS.US/tag/Twin+lamp+Floor/ : Floor…
Twin…
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….
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….
Antique http://uqa.yc6.ll1.co : Trader…
Auto…
…
BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…
ANDREW
order cialis in canada online…
Buyno prescription…
ENRIQUE
SERGIO
CURTIS
DARRYL
e-code for zometa…
Buyit now…
cats zanaflex…
Buydrugs without prescription…
Zaditor…
Buydrugs without prescription…
Zyrtec…
Buyit now…
Zyprexa…
Buynow it…
Prozac…
Buygeneric meds…
JARED
DWAYNE
JULIO
LLOYD
.…
Buynow…
JARED
ANDREW
JERRY
.…
Buyno prescription…
THOMAS
OLIVER
ANTONIO
ERNESTO
……
Buygeneric drugs…