You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by le...@apache.org on 2008/08/15 10:59:57 UTC
svn commit: r686179 -
/excalibur/trunk/containerkit/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java
Author: leif
Date: Fri Aug 15 01:59:56 2008
New Revision: 686179
URL: http://svn.apache.org/viewvc?rev=686179&view=rev
Log:
Make it possible to send smtp mails to servers which require authentication and use TLS. This makes it possible to send to gmail servers.
Modified:
excalibur/trunk/containerkit/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java
Modified: excalibur/trunk/containerkit/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java
URL: http://svn.apache.org/viewvc/excalibur/trunk/containerkit/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java?rev=686179&r1=686178&r2=686179&view=diff
==============================================================================
--- excalibur/trunk/containerkit/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java (original)
+++ excalibur/trunk/containerkit/logger/src/java/org/apache/avalon/excalibur/logger/factory/SMTPTargetFactory.java Fri Aug 15 01:59:56 2008
@@ -18,7 +18,10 @@
*/
package org.apache.avalon.excalibur.logger.factory;
+import java.util.Properties;
+import javax.mail.Authenticator;
import javax.mail.Address;
+import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
@@ -53,6 +56,28 @@
* <debug>false</debug>
* </smtp>
* </pre>
+ * </p>
+ * <p>
+ * Or without a session in a context:
+ * <pre>
+ * <smtp id="target-id">
+ * <session>
+ * <parameter name="mail.host" value="smtp.somehost.com"/>
+ * <parameter name="mail.smtp.localhost" value="@app.name@"/> <!-- Needed on some servers for a name for the SMTP EHLO -->
+ * <parameter name="mail.smtp.starttls.enable" value="true"/> <!-- Needed by servers like gmail -->
+ * <authentication user="USER" password="PASSWORD"/> <!-- This optional element will automatically set the mail.smtp.auth parameter. -->
+ * </session>
+ * <format type="raw|pattern|extended">pattern to be used if needed</format>
+ * <to>address-1@host</to>
+ * <to>address-N@host</to>
+ * <from>address@host</from>
+ * <subject>subject line</subject>
+ * <maximum-size>number</maximum-size>
+ * <maximum-delay-time>seconds</maximum-delay-time>
+ * <debug>false</debug>
+ * </smtp>
+ * </pre>
+ * </p>
*
* The Factory will look for a javax.mail.Session instance in the Context using
* the specified context-key. If your needs are simple, then it is also possible
@@ -83,7 +108,7 @@
* <p>
*
* @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
- * @version CVS $Revision: 1.14 $ $Date: 2004/03/10 13:54:50 $
+ * @version $Id$
* @since 4.1
*/
public class SMTPTargetFactory
@@ -187,9 +212,21 @@
}
else
{
- return Session.getInstance(
- Parameters.toProperties(
- Parameters.fromConfiguration( sessionConfig ) ) );
+ Properties properties = Parameters.toProperties(
+ Parameters.fromConfiguration( sessionConfig ) );
+
+ MailAuthenticator authenticator = null;
+ Configuration authenticationConf = sessionConfig.getChild( "authentication", false );
+ if ( authenticationConf != null )
+ {
+ String authUser = authenticationConf.getAttribute( "user" );
+ String authPassword = authenticationConf.getAttribute( "password" );
+ authenticator = new MailAuthenticator( authUser, authPassword );
+
+ properties.setProperty( "mail.smtp.auth", "true" );
+ }
+
+ return Session.getInstance( properties, authenticator );
}
}
@@ -298,4 +335,26 @@
{
return new InternetAddress( address );
}
+
+ /*---------------------------------------------------------------
+ * Inner Classes
+ *-------------------------------------------------------------*/
+ private class MailAuthenticator extends Authenticator
+ {
+ private String m_user;
+ private String m_password;
+
+ MailAuthenticator( String user, String password )
+ {
+ super();
+
+ m_user = user;
+ m_password = password;
+ }
+
+ protected PasswordAuthentication getPasswordAuthentication()
+ {
+ return new PasswordAuthentication( m_user, m_password );
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org