Index: src/com/ecyrd/jspwiki/util/MailUtil.java =================================================================== RCS file: /p/cvs/JSPWiki/src/com/ecyrd/jspwiki/util/MailUtil.java,v retrieving revision 1.15 diff -u -r1.15 MailUtil.java --- src/com/ecyrd/jspwiki/util/MailUtil.java 27 Jan 2008 19:35:29 -0000 1.15 +++ src/com/ecyrd/jspwiki/util/MailUtil.java 28 Jan 2008 16:44:58 -0000 @@ -36,156 +36,192 @@ import com.ecyrd.jspwiki.WikiEngine; /** - *

Contains static methods for sending e-mails to recipients using JNDI-supplied - * JavaMail - * Sessions supplied by a web container (preferred) or configured via - * jspwiki.properties; both methods are described below. - * Because most e-mail servers require authentication, - * for security reasons implementors are strongly encouraged to use - * container-managed JavaMail Sessions so that passwords are not exposed in - * jspwiki.properties.

- *

To enable e-mail functions within JSPWiki, administrators must do three things: - * ensure that the required JavaMail JARs are on the runtime classpath, configure - * JavaMail appropriately, and (recommdended) configure the JNDI JavaMail session factory.

+ *

+ * Contains static methods for sending e-mails to recipients using JNDI-supplied + * JavaMail Sessions + * supplied by a web container (preferred) or configured via + * jspwiki.properties; both methods are described below. Because + * most e-mail servers require authentication, for security reasons implementors + * are strongly encouraged to use container-managed JavaMail Sessions + * so that passwords are not exposed in jspwiki.properties. + *

+ *

+ * To enable e-mail functions within JSPWiki, administrators must do three + * things: ensure that the required JavaMail JARs are on the runtime classpath, + * configure JavaMail appropriately, and (recommdended) configure the JNDI + * JavaMail session factory. + *

* JavaMail runtime JARs - *

The first step is easy: JSPWiki bundles - * recent versions of the required JavaMail mail.jar and - * activation.jar into the JSPWiki WAR file; so, out of the box - * this is already taken care of. However, when using JNDI-supplied - * Session factories, these should be moved, not copied, to a classpath location - * where the JARs can be shared by both the JSPWiki webapp and the container. For example, - * Tomcat 5 provides the directory $CATALINA_HOME>/common/lib - * for storage of shared JARs; move mail.jar and activation - * there instead of keeping them in /WEB-INF/lib.

+ *

+ * The first step is easy: JSPWiki bundles recent versions of the required + * JavaMail mail.jar and activation.jar into the + * JSPWiki WAR file; so, out of the box this is already taken care of. However, + * when using JNDI-supplied Session factories, these should be moved, + * not copied, to a classpath location where the JARs can be shared + * by both the JSPWiki webapp and the container. For example, Tomcat 5 provides + * the directory $CATALINA_HOME>/common/lib for + * storage of shared JARs; move mail.jar and + * activation there instead of keeping them in + * /WEB-INF/lib. + *

* JavaMail configuration - *

Regardless of the method used for supplying JavaMail sessions (JNDI container-managed - * or via jspwiki.properties, JavaMail needs certain properties - * set in order to work correctly. Configurable properties are these:

+ *

+ * Regardless of the method used for supplying JavaMail sessions (JNDI + * container-managed or via jspwiki.properties, JavaMail needs + * certain properties set in order to work correctly. Configurable properties + * are these: + *

* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * *
PropertyDefaultDefinition
jspwiki.mail.jndinamemail/SessionThe JNDI name of the JavaMail session factory
mail.smtp.host127.0.0.1The SMTP mail server from which messages will be sent.
mail.smtp.port25The port number of the SMTP mail service.
mail.smtp.account(not set)The user name of the sender. If this value is supplied, the JavaMail - * session will attempt to authenticate to the mail server before sending - * the message. If not supplied, JavaMail will attempt to send the message - * without authenticating (i.e., it will use the server as an open relay). - * In real-world scenarios, you should set this value.
mail.smtp.password(not set)The password of the sender. In real-world scenarios, you - * should set this value.
mail.from${user.name}@${mail.smtp.host}*The e-mail address of the sender.
mail.smtp.timeout5000*Socket I/O timeout value, in milliseconds. The default is 5 seconds.
mail.smtp.connectiontimeout5000*Socket connection timeout value, in milliseconds. The default is 5 seconds.
mail.smtp.starttls.enabletrue*If true, enables the use of the STARTTLS command (if - * supported by the server) to switch the connection to a - * TLS-protected connection before issuing any login commands. - * Note that an appropriate trust store must configured so that - * the client will trust the server's certificate. By default, - * the JRE trust store contains root CAs for most public certificate - * authorities.
PropertyDefaultDefinition
jspwiki.mail.jndinamemail/SessionThe JNDI name of the JavaMail session factory
mail.smtp.host127.0.0.1The SMTP mail server from which messages will be sent.
mail.smtp.port25The port number of the SMTP mail service.
mail.smtp.account(not set)The user name of the sender. If this value is supplied, the JavaMail + * session will attempt to authenticate to the mail server before sending the + * message. If not supplied, JavaMail will attempt to send the message without + * authenticating (i.e., it will use the server as an open relay). In real-world + * scenarios, you should set this value.
mail.smtp.password(not set)The password of the sender. In real-world scenarios, you should set this + * value.
mail.from${user.name}@${mail.smtp.host}*The e-mail address of the sender.
mail.smtp.timeout5000*Socket I/O timeout value, in milliseconds. The default is 5 seconds.
mail.smtp.connectiontimeout5000*Socket connection timeout value, in milliseconds. The default is 5 + * seconds.
mail.smtp.starttls.enabletrue*If true, enables the use of the STARTTLS command (if supported by the + * server) to switch the connection to a TLS-protected connection before issuing + * any login commands. Note that an appropriate trust store must configured so + * that the client will trust the server's certificate. By default, the JRE + * trust store contains root CAs for most public certificate authorities.
- *

*These defaults apply only if the stand-alone Session factory is used - * (that is, these values are obtained from jspwiki.properties). - * If using a container-managed JNDI Session factory, the container will - * likely supply its own default values, and you should probably override - * them (see the next section).

+ *

+ * *These defaults apply only if the stand-alone Session factory is used (that + * is, these values are obtained from jspwiki.properties). If + * using a container-managed JNDI Session factory, the container will likely + * supply its own default values, and you should probably override them (see the + * next section). + *

* Container JNDI Session factory configuration - *

You are strongly encouraged to use a container-managed JNDI factory for - * JavaMail sessions, rather than configuring JavaMail through jspwiki.properties. - * To do this, you need to two things: uncomment the <resource-ref> block - * in /WEB-INF/web.xml that enables container-managed JavaMail, and - * configure your container's JavaMail resource factory. The web.xml - * part is easy: just uncomment the section that looks like this:

- *
<resource-ref>
- *   <description>Resource reference to a container-managed JNDI JavaMail factory for sending e-mails.</description>
- *   <res-ref-name>mail/Session</res-ref-name>
- *   <res-type>javax.mail.Session</res-type>
- *   <res-auth>Container</res-auth>
- * </resource-ref>
- *

To configure your container's resource factory, follow the directions supplied by - * your container's documentation. For example, the - * Tomcat - * 5.5 docs state that you need a properly configured <Resource> - * element inside the JSPWiki webapp's <Context> declaration. Here's an example shows - * how to do it:

- *
<Context ...>
+ * 

+ * You are strongly encouraged to use a container-managed JNDI factory for + * JavaMail sessions, rather than configuring JavaMail through + * jspwiki.properties. To do this, you need to two things: + * uncomment the <resource-ref> block in + * /WEB-INF/web.xml that enables container-managed JavaMail, and + * configure your container's JavaMail resource factory. The + * web.xml part is easy: just uncomment the section that looks + * like this: + *

+ * + *
+ * <resource-ref>
+ *   <description>Resource reference to a container-managed JNDI JavaMail factory for sending e-mails.</description>
+ *   <res-ref-name>mail/Session</res-ref-name>
+ *   <res-type>javax.mail.Session</res-type>
+ *   <res-auth>Container</res-auth>
+ * </resource-ref>
+ * 
+ * + *

+ * To configure your container's resource factory, follow the directions + * supplied by your container's documentation. For example, the Tomcat + * 5.5 docs state that you need a properly configured + * <Resource> element inside the JSPWiki webapp's + * <Context> declaration. Here's an example shows how to do + * it: + *

+ * + *
+ * <Context ...>
  * ...
- * <Resource name="mail/Session" auth="Container"
- *           type="javax.mail.Session"
- *           mail.smtp.host="127.0.0.1"/>
- *           mail.smtp.port="25"/>
- *           mail.smtp.account="your-account-name"/>
- *           mail.smtp.password="your-password"/>
- *           mail.from="Snoop Dogg <snoop@dogg.org>"/>
- *           mail.smtp.timeout="5000"/>
- *           mail.smtp.connectiontimeout="5000"/>
- *           mail.smtp.starttls.enable="true"/>
+ * <Resource name="mail/Session" auth="Container"
+ *           type="javax.mail.Session"
+ *           mail.smtp.host="127.0.0.1"/>
+ *           mail.smtp.port="25"/>
+ *           mail.smtp.account="your-account-name"/>
+ *           mail.smtp.password="your-password"/>
+ *           mail.from="Snoop Dogg <snoop@dogg.org>"/>
+ *           mail.smtp.timeout="5000"/>
+ *           mail.smtp.connectiontimeout="5000"/>
+ *           mail.smtp.starttls.enable="true"/>
  * ...
- * </Context>
- *

Note that with Tomcat (and most other application containers) you can also declare the JavaMail - * JNDI factory as a global resource, shared by all applications, instead of as a local JSPWiki - * resource as we have done here. For example, the following entry in - * $CATALINA_HOME/conf/server.xml creates a global resource:

- *
<GlobalNamingResources>
- *   <Resource name="mail/Session" auth="Container"
- *             type="javax.mail.Session"
+ * </Context>
+ * 
+ * + *

+ * Note that with Tomcat (and most other application containers) you can also + * declare the JavaMail JNDI factory as a global resource, shared by all + * applications, instead of as a local JSPWiki resource as we have done here. + * For example, the following entry in + * $CATALINA_HOME/conf/server.xml creates a global + * resource: + *

+ * + *
+ * <GlobalNamingResources>
+ *   <Resource name="mail/Session" auth="Container"
+ *             type="javax.mail.Session"
  *             ...
- *             mail.smtp.starttls.enable="true"/>
- * </GlobalNamingResources>
- *

This approach — creating a global JNDI resource — yields somewhat decreased - * deployment complexity because the JSPWiki webapp no longer needs its own JavaMail resource - * declaration. However, it is slightly less secure because it means that all other applications - * can now obtain a JavaMail session if they want to. In many cases, this is what - * you want.

- *

NOTE: Versions of Tomcat 5.5 later than 5.5.17, and up to and including 5.5.23 have a - * b0rked version of $CATALINA_HOME/common/lib/naming-factory.jar - * that prevents usage of JNDI. To avoid this problem, you should patch your 5.5.23 version - * of naming-factory.jar with the one from 5.5.17. This is a known issue - * and the bug report (#40668) is - * here. - * + * mail.smtp.starttls.enable="true"/> + * </GlobalNamingResources> + *

+ * + *

+ * This approach — creating a global JNDI resource — yields somewhat + * decreased deployment complexity because the JSPWiki webapp no longer needs + * its own JavaMail resource declaration. However, it is slightly less secure + * because it means that all other applications can now obtain a JavaMail + * session if they want to. In many cases, this is what you want. + *

+ *

+ * NOTE: Versions of Tomcat 5.5 later than 5.5.17, and up to and including + * 5.5.23 have a b0rked version of + * $CATALINA_HOME/common/lib/naming-factory.jar that + * prevents usage of JNDI. To avoid this problem, you should patch your 5.5.23 + * version of naming-factory.jar with the one from 5.5.17. This + * is a known issue and the bug report (#40668) is here. + * * @author Christoph Sauer * @author Dan Frankowski * @author Andrew Jaquith @@ -204,264 +240,298 @@ protected static final Logger log = Logger.getLogger(MailUtil.class); - protected static final String DEFAULT_MAIL_JNDI_NAME = "mail/Session"; + protected static final String DEFAULT_MAIL_JNDI_NAME = "mail/Session"; - protected static final String DEFAULT_MAIL_HOST = "localhost"; + protected static final String DEFAULT_MAIL_HOST = "localhost"; - protected static final String DEFAULT_MAIL_PORT = "25"; + protected static final String DEFAULT_MAIL_PORT = "25"; - protected static final String DEFAULT_MAIL_TIMEOUT = "5000"; - - protected static final String DEFAULT_SENDER = "jspwiki@localhost"; + protected static final String DEFAULT_MAIL_TIMEOUT = "5000"; - protected static final String PROP_MAIL_JNDI_NAME = "jspwiki.mail.jndiname"; + protected static final String DEFAULT_SENDER = "jspwiki@localhost"; - protected static final String PROP_MAIL_HOST = "mail.smtp.host"; + protected static final String PROP_MAIL_JNDI_NAME = "jspwiki.mail.jndiname"; - protected static final String PROP_MAIL_PORT = "mail.smtp.port"; + protected static final String PROP_MAIL_HOST = "mail.smtp.host"; - protected static final String PROP_MAIL_ACCOUNT = "mail.smtp.account"; + protected static final String PROP_MAIL_PORT = "mail.smtp.port"; - protected static final String PROP_MAIL_PASSWORD = "mail.smtp.password"; + protected static final String PROP_MAIL_ACCOUNT = "mail.smtp.account"; - protected static final String PROP_MAIL_TIMEOUT = "mail.smtp.timeout"; + protected static final String PROP_MAIL_PASSWORD = "mail.smtp.password"; + + protected static final String PROP_MAIL_TIMEOUT = "mail.smtp.timeout"; protected static final String PROP_MAIL_CONNECTION_TIMEOUT = "mail.smtp.connectiontimeout"; - protected static final String PROP_MAIL_TRANSPORT = "smtp"; + protected static final String PROP_MAIL_TRANSPORT = "smtp"; + + protected static final String PROP_MAIL_SENDER = "mail.from"; - protected static final String PROP_MAIL_SENDER = "mail.from"; + protected static final String PROP_MAIL_STARTTLS = "mail.smtp.starttls.enable"; - protected static final String PROP_MAIL_STARTTLS = "mail.smtp.starttls.enable"; + private static String fromAddress = null; /** - * Private constructor prevents instantiation. + * Private constructor prevents instantiation. */ private MailUtil() { } /** - *

Sends an e-mail to a specified receiver using a JavaMail Session supplied + *

+ * Sends an e-mail to a specified receiver using a JavaMail Session supplied * by a JNDI mail session factory (preferred) or a locally initialized - * session based on properties in jspwiki.properties. - * See the top-level JavaDoc for this class for a description of - * required properties and their default values.

- *

The e-mail address used for the to parameter must be in - * RFC822 format, as described in the JavaDoc for {@link javax.mail.internet.InternetAddress} - * and more fully at - * http://www.freesoft.org/CIE/RFC/822/index.htm. - * In other words, e-mail addresses should look like this:

+ * session based on properties in jspwiki.properties. See + * the top-level JavaDoc for this class for a description of required + * properties and their default values. + *

+ *

+ * The e-mail address used for the to parameter must be in + * RFC822 format, as described in the JavaDoc for + * {@link javax.mail.internet.InternetAddress} and more fully at http://www.freesoft.org/CIE/RFC/822/index.htm. + * In other words, e-mail addresses should look like this: + *

*
Snoop Dog <snoop.dog@shizzle.net>
* snoop.dog@shizzle.net
- *

Note that the first form allows a "friendly" user name to be supplied - * in addition to the actual e-mail address.

- * - * @param engine the WikiEngine for the current wiki - * @param to the receiver - * @param subject the subject line of the message - * @param content the contents of the mail message, as plain text + *

+ * Note that the first form allows a "friendly" user name to be supplied in + * addition to the actual e-mail address. + *

+ * + * @param engine + * the WikiEngine for the current wiki + * @param to + * the receiver + * @param subject + * the subject line of the message + * @param content + * the contents of the mail message, as plain text + * @throws AddressException + * @throws MessagingException */ - public static void sendMessage( WikiEngine engine, String to, String subject, String content ) - throws AddressException, MessagingException + public static void sendMessage(WikiEngine engine, String to, String subject, String content) + throws AddressException, MessagingException { - String from = engine.getWikiProperties().getProperty( PROP_MAIL_SENDER, DEFAULT_SENDER ).trim(); - sendMessage( engine, to, from, subject, content ); + Properties props = engine.getWikiProperties(); + Session session = getMailSession(engine); + getSenderEmailAddress(session, props); + + try + { + // Create and address the message + MimeMessage msg = new MimeMessage(session); + msg.setFrom(new InternetAddress(fromAddress)); + msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); + msg.setSubject(subject); + msg.setText(content, "UTF-8"); + msg.setSentDate(new Date()); + + // Send and log it + Transport.send(msg); + if (log.isInfoEnabled()) + { + log.info("Sent e-mail to=" + to + ", subject=\"" + subject + "\", jndi=" + (c_useJndi ? TRUE : FALSE)); + } + } + catch (MessagingException e) + { + log.error(e); + throw e; + } } + // --------- JavaMail Session Helper methods + // --------------------------------- + /** - *

Sends an e-mail to a specified receiver from a specified sender, using a - * JavaMail Session supplied by a JNDI mail session factory (preferred) or - * a locally initialized session based on properties in - * jspwiki.properties. See the top-level JavaDoc for this - * class for a description of required properties and their - * default values.

- *

The e-mail addresses used for the to and from - * parameters must be in RFC822 format, as described in the JavaDoc for - * {@link javax.mail.internet.InternetAddress} and more fully at - * http://www.freesoft.org/CIE/RFC/822/index.htm. - * In other words, e-mail addresses should look like this:

- *
Snoop Dog <snoop.dog@shizzle.net>
- * snoop.dog@shizzle.net
- *

Note that the first form allows a "friendly" user name to be supplied - * in addition to the actual e-mail address.

- * - * @param engine the WikiEngine for the current wiki - * @param to the receiver - * @param from the address the email will be from - * @param subject the subject line of the message - * @param content the contents of the mail message, as plain text + * Gets the Sender's email address from JNDI Session if available, otherwise + * from the jspwiki.properties or lastly the default value. + * @param pSession Session + * @param pProperties Properties + * @return String + */ + protected static String getSenderEmailAddress(Session pSession, Properties pProperties) + { + if (fromAddress == null) + { + // First, attempt to get the email address from the JNDI Mail Session. + if (pSession != null && c_useJndi) + { + fromAddress = pSession.getProperty(MailUtil.PROP_MAIL_SENDER); + } + // If unsuccessful, get the email address from the properties or default. + if (fromAddress == null) { + fromAddress = pProperties.getProperty(PROP_MAIL_SENDER, DEFAULT_SENDER).trim(); + } + } + return fromAddress; + } + + /** + * Returns the Mail Session from either JNDI or creates a stand-alone. + * @param engine a WikiEngine + * @return Session */ - public static void sendMessage(WikiEngine engine, String to, String from, String subject, String content) - throws MessagingException + private static Session getMailSession(WikiEngine engine) { + Session result = null; Properties props = engine.getWikiProperties(); - String jndiName = props.getProperty( PROP_MAIL_JNDI_NAME, DEFAULT_MAIL_JNDI_NAME ).trim(); - Session session = null; + String jndiName = props.getProperty(PROP_MAIL_JNDI_NAME, DEFAULT_MAIL_JNDI_NAME).trim(); if (c_useJndi) { // Try getting the Session from the JNDI factory first try { - session = getJNDIMailSession(jndiName); - c_useJndi = false; + result = getJNDIMailSession(jndiName); } catch (NamingException e) { // Oops! JNDI factory must not be set up + c_useJndi = false; } } // JNDI failed; so, get the Session from the standalone factory - if ( session == null ) - { - session = getStandaloneMailSession( props ); - } - - try - { - // Create and address the message - MimeMessage msg = new MimeMessage( session ); - msg.setFrom( new InternetAddress( from ) ); - msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to, false ) ); - msg.setSubject( subject ); - msg.setText( content, "UTF-8" ); - msg.setSentDate( new Date() ); - - // Send and log it - Transport.send( msg ); - if ( log.isInfoEnabled() ) - { - log.info( "Sent e-mail to=" + to + ", subject=\"" + subject - + "\", jndi=" + ( c_useJndi ? TRUE : FALSE ) ); - } - } - catch ( MessagingException e ) + if (result == null) { - log.error( e ); - throw e; + result = getStandaloneMailSession(props); } + return result; } - // --------- JavaMail Session Helper methods --------------------------------- - /** - * Returns a stand-alone JavaMail Session by looking up the correct - * mail account, password and host from a supplied set of properties. - * If the JavaMail property {@value #PROP_MAIL_ACCOUNT} is set to - * a value that is non-null and of non-zero length, the - * Session will be initialized with an instance of - * {@link javax.mail.Authenticator}. - * @param props the properties that contain mail session properties + * Returns a stand-alone JavaMail Session by looking up the correct mail + * account, password and host from a supplied set of properties. If the + * JavaMail property {@value #PROP_MAIL_ACCOUNT} is set to a value that is + * non-null and of non-zero length, the Session will be + * initialized with an instance of {@link javax.mail.Authenticator}. + * + * @param props + * the properties that contain mail session properties * @return the initialized JavaMail Session */ - protected static Session getStandaloneMailSession( Properties props ) + protected static Session getStandaloneMailSession(Properties props) { // Read the JSPWiki settings from the properties - String host = props.getProperty( PROP_MAIL_HOST, DEFAULT_MAIL_HOST ); - String port = props.getProperty( PROP_MAIL_PORT, DEFAULT_MAIL_PORT ); - String account = props.getProperty( PROP_MAIL_ACCOUNT ); - String password = props.getProperty( PROP_MAIL_PASSWORD ); - boolean starttls = TextUtil.getBooleanProperty( props, PROP_MAIL_STARTTLS, true); - + String host = props.getProperty(PROP_MAIL_HOST, DEFAULT_MAIL_HOST); + String port = props.getProperty(PROP_MAIL_PORT, DEFAULT_MAIL_PORT); + String account = props.getProperty(PROP_MAIL_ACCOUNT); + String password = props.getProperty(PROP_MAIL_PASSWORD); + boolean starttls = TextUtil.getBooleanProperty(props, PROP_MAIL_STARTTLS, true); + boolean useAuthentication = account != null && account.length() > 0; Properties mailProps = new Properties(); // Set JavaMail properties - mailProps.put( PROP_MAIL_HOST, host ); - mailProps.put( PROP_MAIL_PORT, port ); - mailProps.put( PROP_MAIL_TIMEOUT, DEFAULT_MAIL_TIMEOUT ); - mailProps.put( PROP_MAIL_CONNECTION_TIMEOUT, DEFAULT_MAIL_TIMEOUT ); - mailProps.put( PROP_MAIL_STARTTLS, starttls ? TRUE : FALSE ); + mailProps.put(PROP_MAIL_HOST, host); + mailProps.put(PROP_MAIL_PORT, port); + mailProps.put(PROP_MAIL_TIMEOUT, DEFAULT_MAIL_TIMEOUT); + mailProps.put(PROP_MAIL_CONNECTION_TIMEOUT, DEFAULT_MAIL_TIMEOUT); + mailProps.put(PROP_MAIL_STARTTLS, starttls ? TRUE : FALSE); // Add SMTP authentication if required Session session = null; - if ( useAuthentication ) + if (useAuthentication) { - mailProps.put( PROP_MAIL_AUTH, TRUE ); - SmtpAuthenticator auth = new SmtpAuthenticator( account, password ); + mailProps.put(PROP_MAIL_AUTH, TRUE); + SmtpAuthenticator auth = new SmtpAuthenticator(account, password); - session = Session.getInstance( mailProps, auth ); + session = Session.getInstance(mailProps, auth); } else { - session = Session.getInstance( mailProps ); + session = Session.getInstance(mailProps); } - if ( log.isDebugEnabled() ) + if (log.isDebugEnabled()) { - String mailServer = host + ":" + port + ", auth=" + ( useAuthentication ? TRUE : FALSE ); - log.debug( "JavaMail session obtained from standalone mail factory: " + mailServer ); + String mailServer = host + ":" + port + ", auth=" + (useAuthentication ? TRUE : FALSE); + log.debug("JavaMail session obtained from standalone mail factory: " + mailServer); } return session; } - /** - * Returns a JavaMail Session instance from a JNDI container-managed factory. - * @param jndiName the JNDI name for the resource. If null, the default value - * of mail/Session will be used + * Returns a JavaMail Session instance from a JNDI container-managed + * factory. + * + * @param jndiName + * the JNDI name for the resource. If null, the + * default value of mail/Session will be used * @return the initialized JavaMail Session - * @throws NamingException if the Session cannot be obtained; for example, if the factory is not configured + * @throws NamingException + * if the Session cannot be obtained; for example, if the + * factory is not configured */ - protected static Session getJNDIMailSession( String jndiName ) throws NamingException + protected static Session getJNDIMailSession(String jndiName) throws NamingException { Session session = null; try { Context initCtx = new InitialContext(); - Context ctx = (Context) initCtx.lookup( JAVA_COMP_ENV ); - session = (Session) ctx.lookup( jndiName ); + Context ctx = (Context) initCtx.lookup(JAVA_COMP_ENV); + session = (Session) ctx.lookup(jndiName); } - catch( NamingException e ) + catch (NamingException e) { - log.warn( "JavaMail initialization error: " + e.getMessage() ); + log.warn("JavaMail initialization error: " + e.getMessage()); throw e; } - if ( log.isDebugEnabled() ) + if (log.isDebugEnabled()) { - log.debug( "JavaMail session obtained from JNDI mail factory: " + jndiName ); + log.debug("JavaMail session obtained from JNDI mail factory: " + jndiName); } return session; } /** - * Simple {@link javax.mail.Authenticator} subclass that authenticates a user to - * an SMTP server. + * Simple {@link javax.mail.Authenticator} subclass that authenticates a + * user to an SMTP server. + * * @author Christoph Sauer */ protected static class SmtpAuthenticator extends Authenticator { private static final String BLANK = ""; + private final String m_pass; + private final String m_login; /** - * Constructs a new SmtpAuthenticator with a supplied username and password. - * @param login the user name - * @param pass the password + * Constructs a new SmtpAuthenticator with a supplied username and + * password. + * + * @param login + * the user name + * @param pass + * the password */ public SmtpAuthenticator(String login, String pass) { super(); - m_login = login == null ? BLANK : login; - m_pass = pass == null ? BLANK : pass; + m_login = login == null ? BLANK : login; + m_pass = pass == null ? BLANK : pass; } /** * Returns the password used to authenticate to the SMTP server. + * @return PasswordAuthentication */ public PasswordAuthentication getPasswordAuthentication() { - if ( BLANK.equals(m_pass) ) + if (BLANK.equals(m_pass)) { return null; } - return new PasswordAuthentication( m_login, m_pass ); + return new PasswordAuthentication(m_login, m_pass); } }