You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2006/08/31 21:30:28 UTC
svn commit: r439024 -
/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SMTPAppender.java
Author: carnold
Date: Thu Aug 31 12:30:27 2006
New Revision: 439024
URL: http://svn.apache.org/viewvc?rev=439024&view=rev
Log:
Bug 24969: SMTPAppender should be able to do auth
Modified:
logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SMTPAppender.java
Modified: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SMTPAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SMTPAppender.java?rev=439024&r1=439023&r2=439024&view=diff
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SMTPAppender.java (original)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/net/SMTPAppender.java Thu Aug 31 12:30:27 2006
@@ -28,6 +28,8 @@
import java.util.Date;
import javax.mail.Session;
+import javax.mail.Authenticator;
+import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.Message;
import javax.mail.MessagingException;
@@ -64,6 +66,9 @@
private String from;
private String subject;
private String smtpHost;
+ private String smtpUsername;
+ private String smtpPassword;
+ private boolean smtpDebug = false;
private int bufferSize = 512;
private boolean locationInfo = false;
@@ -98,13 +103,7 @@
recipient, from, etc. */
public
void activateOptions() {
- Properties props = new Properties (System.getProperties());
- if (smtpHost != null)
- props.put("mail.smtp.host", smtpHost);
-
-
- Session session = Session.getInstance(props, null);
- //session.setDebug(true);
+ Session session = createSession();
msg = new MimeMessage(session);
try {
@@ -143,6 +142,32 @@
msg.setRecipients(Message.RecipientType.BCC, parseAddress(bcc));
}
}
+
+ /**
+ * Create mail session.
+ * @param mail session, may not be null.
+ */
+ protected Session createSession() {
+ Properties props = new Properties (System.getProperties());
+ if (smtpHost != null) {
+ props.put("mail.smtp.host", smtpHost);
+ }
+
+ Authenticator auth = null;
+ if(smtpPassword != null && smtpUsername != null) {
+ props.put("mail.smtp.auth", "true");
+ auth = new Authenticator() {
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(smtpUsername, smtpPassword);
+ }
+ };
+ }
+ Session session = Session.getInstance(props, auth);
+ if (smtpDebug) {
+ session.setDebug(smtpDebug);
+ }
+ return session;
+ }
/**
Perform SMTPAppender specific appending actions, mainly adding
@@ -449,7 +474,57 @@
return bcc;
}
+ /**
+ * The <b>SmtpPassword</b> option takes a string value which should be the password required to authenticate against
+ * the mail server.
+ * @param password password, may be null.
+ */
+ public void setSMTPPassword(final String password) {
+ this.smtpPassword = password;
+ }
+
+ /**
+ * The <b>SmtpUsername</b> option takes a string value which should be the username required to authenticate against
+ * the mail server.
+ * @param username user name, may be null.
+ */
+ public void setSMTPUsername(final String username) {
+ this.smtpUsername = username;
+ }
+
+ /**
+ * Setting the <b>SmtpDebug</b> option to true will cause the mail session to log its server interaction to stdout.
+ * This can be useful when debuging the appender but should not be used during production because username and
+ * password information is included in the output.
+ * @param debug debug flag.
+ */
+ public void setSMTPDebug(final boolean debug) {
+ this.smtpDebug = debug;
+ }
+ /**
+ * Get SMTP password.
+ * @return SMTP password, may be null.
+ */
+ public String getSMTPPassword() {
+ return smtpPassword;
+ }
+
+ /**
+ * Get SMTP user name.
+ * @return SMTP user name, may be null.
+ */
+ public String getSMTPUsername() {
+ return smtpUsername;
+ }
+
+ /**
+ * Get SMTP debug.
+ * @return SMTP debug flag.
+ */
+ public boolean getSMTPDebug() {
+ return smtpDebug;
+ }
}
class DefaultEvaluator implements TriggeringEventEvaluator {
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org