logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r439025 - /logging/log4j/trunk/src/java/org/apache/log4j/net/SMTPAppender.java
Date Thu, 31 Aug 2006 19:30:44 GMT
Author: carnold
Date: Thu Aug 31 12:30:43 2006
New Revision: 439025

URL: http://svn.apache.org/viewvc?rev=439025&view=rev
Log:
Bug 24969: SMTPAppender should be able to do auth

Modified:
    logging/log4j/trunk/src/java/org/apache/log4j/net/SMTPAppender.java

Modified: logging/log4j/trunk/src/java/org/apache/log4j/net/SMTPAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/java/org/apache/log4j/net/SMTPAppender.java?rev=439025&r1=439024&r2=439025&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/net/SMTPAppender.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/net/SMTPAppender.java Thu Aug 31 12:30:43
2006
@@ -23,6 +23,8 @@
 import javax.mail.MessagingException;
 import javax.mail.Multipart;
 import javax.mail.Session;
+import javax.mail.Authenticator;
+import javax.mail.PasswordAuthentication;
 import javax.mail.Transport;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
@@ -88,6 +90,9 @@
   private String from;
   private String subjectStr = "";
   private String smtpHost;
+  private String smtpUsername;
+  private String smtpPassword;
+  private boolean smtpDebug = false;
   private String charset = "ISO-8859-1";
   private int bufferSize = 512;
   private boolean locationInfo = false;
@@ -117,15 +122,7 @@
      recipient, from, etc. */
   public void activateOptions() {
     int errorCount = 0;
-    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 {
@@ -188,6 +185,32 @@
 	  }
   }
 
+  /**
+   *  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
@@ -522,6 +545,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;
+  }
 }
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message