Log4j, SMTPAppender, and AuthenticationTuesday, September 29th, 2009 with 1 Comment »

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.

Using CSS for an Image with Semi-Transparent CaptionSaturday, May 16th, 2009 with No Comments »

This effect is similar to the front page of AllKpop, which has an image link with a transparent black text box with white text on top. This effect can be done using CSS, with code taken from the WordPress theme Infinity.

What is important is the (.thumb width) = (.thumb title h2 a width) + 2*(.thumb title h2 a padding).  This ensures the text is properly centered in the image.

The basic CSS is as follows:

.thumb {
  background: #fff;
  display: block;
  width: 190px;
  overflow: hidden;
  height: 250px;
  position: relative; }
.thumb-title {
  margin-bottom:0px;
  background:#000;
  bottom:0;
  right:0;
  display:block;
  position:absolute;
  padding:0px;
  filter:alpha(opacity=75);-moz-opacity:.75;opacity:.75;}
.thumb-title:hover { background:#222; }
.thumb-title h2 { margin: 0px;}
.thumb-title h2 a {
  padding: 10px;
  font:14px Arial,helvetica;
  display:block;
  color:#fff;
  text-decoration: none;
  width: 170px;}
.thumb-title h2 a:hover {
  padding: 10px;
  font:14px Arial,helvetica;
  display:block;
  color:#fff;
  text-decoration: none;
  width: 170px;}

Usage example:

<div class="thumb powered">
<a href="http://example.com"><img src="/img/example.jpg"/></a>
<div class="thumb-title"><h2>
<a href="http://example.com">Example</a></h2></div>
</div>
Get updates as often as we post! Subscribe to our full feed or comments feed.