# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: D:\Work\Java\Open_Source\xwiki-trunks\xwiki-platform-core\xwiki-core\src\main\java\com\xpn\xwiki\plugin\mailsender # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: MailConfiguration.java --- MailConfiguration.java Base (BASE) +++ MailConfiguration.java Locally Modified (Based On LOCAL) @@ -19,6 +19,11 @@ */ package com.xpn.xwiki.plugin.mailsender; +import java.util.Properties; +import java.util.Enumeration; +import java.io.IOException; +import java.io.StringReader; + import com.xpn.xwiki.api.XWiki; /** @@ -29,7 +34,11 @@ private int port; private String host; private String from; + private String smtpUsername; + private String smtpPassword; + private Properties extraProperties; + public MailConfiguration() { setPort(25); @@ -49,8 +58,26 @@ if (from.length() > 0) { setFrom(from); } + + String smtpServerUsername = + xwiki.getXWikiPreference("smtp_server_username", ""); + String smtpServerPassword = + xwiki.getXWikiPreference("smtp_server_password", ""); + if ((smtpServerUsername.length() > 0) && + (smtpServerPassword.length() > 0)) + { + setSmtpUsername(smtpServerUsername); + setSmtpPassword(smtpServerPassword); } + String javaMailExtraProps = + xwiki.getXWikiPreference("javamail_extra_props", ""); + if (javaMailExtraProps.length() > 0) + { + setExtraPropertiesAsString(javaMailExtraProps); + } + } + public void setHost(String host) { this.host = host; @@ -81,6 +108,88 @@ return this.from; } + public void setSmtpUsername(String smtpUsername) + { + this.smtpUsername = smtpUsername; + } + + public String getSmtpUsername() + { + return smtpUsername; + } + + public void setSmtpPassword(String smtpPassword) + { + this.smtpPassword = smtpPassword; + } + + public String getSmtpPassword() + { + return smtpPassword; + } + + public boolean areSmtpUsernameAndPasswordSet() + { + return (getSmtpUsername() != null && getSmtpUsername().length() > 0) && + (getSmtpPassword() != null && getSmtpPassword().length() > 0); + } + + public void setExtraPropertiesAsString(String extraPropertiesString) + { + if (extraPropertiesString == null || + extraPropertiesString.trim().length() == 0) + { + extraProperties = null; + } + else + { + StringReader sr = new StringReader(extraPropertiesString); + extraProperties = new Properties(); + try + { + extraProperties.load(sr); + } + catch(IOException e) + { + throw new RuntimeException( + "WTF? IOException here shouldn't be possible."); + } + } + } + + /** + * Add extraProperties to an external Properties object + * + * @param externalProperties + * @param overwrite + */ + public void appendExtraPropertiesTo(Properties externalProperties, + boolean overwrite) + { + // sanity check + if( externalProperties == null ) + { + throw new IllegalArgumentException( + "externalProperties can't be null"); + } + + if (extraProperties != null && extraProperties.size() > 0) + { + Enumeration propNames = extraProperties.propertyNames(); + while (propNames.hasMoreElements()) + { + String propName = (String)propNames.nextElement(); + String propValue = extraProperties.getProperty(propName); + + if (overwrite || + externalProperties.getProperty(propName) == null) + { + externalProperties.setProperty(propName, propValue); + } + } + } + } + public String toString() { StringBuffer buffer = new StringBuffer(); @@ -95,6 +204,12 @@ buffer.append(", Port [" + getPort() + "]"); + if( areSmtpUsernameAndPasswordSet() ) + { + buffer.append(", Username [" + getSmtpUsername() + "]"); + buffer.append(", Password [*****]"); + } + return buffer.toString(); } } Index: MailSenderPlugin.java --- MailSenderPlugin.java Base (BASE) +++ MailSenderPlugin.java Locally Modified (Based On LOCAL) @@ -73,6 +73,8 @@ * @see MailSender * @version $Id: $ */ +// DEV NOTE: main JavaMail code is in sendMails method, near line 550. +// note that most settings are initialized in MailConfiguration constructor. public class MailSenderPlugin extends XWikiDefaultPlugin { /** @@ -108,6 +110,7 @@ */ public void init(XWikiContext context) { + LOG.debug("Entering init(...)..."); try { initMailClass(context); } catch (Exception e) { @@ -122,6 +125,7 @@ */ public void virtualInit(XWikiContext context) { + LOG.debug("Entering virtualInit(...)..."); try { initMailClass(context); } catch (Exception e) { @@ -428,6 +432,13 @@ properties.put("mail.smtp.from", mailConfiguration.getFrom()); } + if (mailConfiguration.areSmtpUsernameAndPasswordSet() ) + { + properties.put("mail.smtp.auth", "true"); + } + + mailConfiguration.appendExtraPropertiesTo(properties, true); + return properties; } @@ -552,12 +563,26 @@ LOG.info("Sending email: " + mail.toString()); if ((transport == null) || (session == null)) { + + // initialize JavaMail Session and Transport Properties props = initProperties(mailConfiguration); session = Session.getDefaultInstance(props, null); transport = session.getTransport("smtp"); + + if (!mailConfiguration.areSmtpUsernameAndPasswordSet() ) + { + // no auth info - typical 127.0.0.1 open relay scenario transport.connect(); } + else + { + // auth info present - typical with external smtp server + transport.connect(mailConfiguration.getSmtpUsername(), + mailConfiguration.getSmtpPassword()); + } + } + try { MimeMessage message = createMimeMessage(mail, session, context);