Attaching diff. Note it's not the whole thing, and probably not final. Greetings, Lilianne # 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 # 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: com/xpn/xwiki/plugin/mailsender/MailConfiguration.java --- com/xpn/xwiki/plugin/mailsender/MailConfiguration.java Base (BASE) +++ com/xpn/xwiki/plugin/mailsender/MailConfiguration.java Locally Modified (Based On LOCAL) @@ -19,7 +19,12 @@ */ package com.xpn.xwiki.plugin.mailsender; +import java.util.Properties; +import java.util.Enumeration; + import com.xpn.xwiki.api.XWiki; +import java.io.IOException; +import java.io.StringReader; /** * Represents a Mail Server configuration. @@ -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: com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java --- com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java Base (BASE) +++ com/xpn/xwiki/plugin/mailsender/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); Index: com/xpn/xwiki/XWiki.java --- com/xpn/xwiki/XWiki.java Base (BASE) +++ com/xpn/xwiki/XWiki.java Locally Modified (Based On LOCAL) @@ -118,6 +118,7 @@ import com.xpn.xwiki.objects.meta.MetaClass; import com.xpn.xwiki.plugin.XWikiPluginInterface; import com.xpn.xwiki.plugin.XWikiPluginManager; +import com.xpn.xwiki.plugin.mailsender.MailSenderPluginApi; import com.xpn.xwiki.plugin.query.QueryPlugin; import com.xpn.xwiki.plugin.query.XWikiCriteria; import com.xpn.xwiki.plugin.query.XWikiQuery; @@ -2711,10 +2712,9 @@ needsUpdate |= bclass.addTextAreaField("menu", "Menu", 60, 8); needsUpdate |= bclass.addTextAreaField("meta", "HTTP Meta Info", 60, 8); + // registration needsUpdate |= bclass.addBooleanField("use_email_verification", "Use eMail Verification", "yesno"); - needsUpdate |= bclass.addTextField("smtp_server", "SMTP Server", 30); - needsUpdate |= bclass.addTextField("admin_email", "Admin eMail", 30); needsUpdate |= bclass.addTextAreaField("validation_email_content", "Validation eMail Content", 72, 10); @@ -2725,6 +2725,13 @@ bclass.addTextAreaField("invitation_email_content", "Invitation eMail Content", 72, 10); + // mail + needsUpdate |= bclass.addTextField("admin_email", "Admin eMail", 30); + needsUpdate |= bclass.addTextField("smtp_server", "SMTP Server", 30); + needsUpdate |= bclass.addTextField("smtp_server_username", "SMTP Server username (optional)", 30); + needsUpdate |= bclass.addTextField("smtp_server_password", "SMTP Server password (optional)", 30); + needsUpdate |= bclass.addTextAreaField("javamail_extra_props", "Additional JavaMail properties", 60, 6); + needsUpdate |= bclass.addTextField("macros_languages", "Macros Languages", 60); needsUpdate |= bclass.addTextField("macros_velocity", "Macros for Velocity", 60); needsUpdate |= bclass.addTextField("macros_groovy", "Macros for Groovy", 60); @@ -3207,6 +3214,63 @@ public void sendMessage(String sender, String[] recipient, String message, XWikiContext context) throws XWikiException { + // TODO - provide some sensible default + String subject = "XWiki message"; + + sendMessage(sender, recipient, subject, message, context); + } + + /** + * @deprecated replaced by the <a + * href="http://code.xwiki.org/xwiki/bin/view/Plugins/MailSenderPlugin">Mail Sender + * Plugin</a> + */ + public void sendMessage(String sender, String[] recipient, String subject, + String message, XWikiContext context) throws XWikiException + { + LOG.debug("Entering sendMessage(...)..."); + + MailSenderPluginApi mailSenderApi = + (MailSenderPluginApi)getPluginApi("mailsender", context); + + LOG.debug("mailSenderApi = " + mailSenderApi); + + LOG.debug("sender = " + sender); + LOG.debug("recipient.length = " + recipient.length); + for(int i = 0; i < recipient.length; i++) + { + LOG.debug("recipient[" + i + "] = " + recipient[i]); + } + LOG.debug("subject = " + subject); + LOG.debug("message = " + message); + LOG.debug("context = " + context); + + String recipientsAsString = recipient[0]; + for(int i = 1; i < recipient.length; i++) + { + recipientsAsString = recipientsAsString + "," + recipient[i]; + } + + + int result = mailSenderApi.sendTextMessage(sender, recipientsAsString, + subject, message); + + // note: -1 is magic number for any error + if( result == -1 ) + { + String errorString = (String)context.get("error"); + XWikiException xwe = + new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, + XWikiException.ERROR_XWIKI_EMAIL_ERROR_SENDING_EMAIL, + errorString); + LOG.error(xwe, xwe); + throw xwe; + } + + LOG.debug("Exiting sendMessage(...). It seems everything went ok."); + + + /* SMTPClient smtpc = null; try { String server = getXWikiPreference("smtp_server", context); @@ -3269,6 +3333,7 @@ } } } + */ } /**