Index: smtpclient/SmtpClientPluginApi.java
===================================================================
--- smtpclient/SmtpClientPluginApi.java	(revision 0)
+++ smtpclient/SmtpClientPluginApi.java	(revision 0)
@@ -0,0 +1,169 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author fabrice
+ */
+package com.xpn.xwiki.plugin.smtpclient;
+
+import java.util.List;
+
+import com.xpn.xwiki.api.Api;
+import com.xpn.xwiki.api.Document;
+import com.xpn.xwiki.plugin.PluginException;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+
+public class SmtpClientPluginApi extends Api
+{
+    private SmtpClientPlugin plugin;
+
+    public SmtpClientPluginApi(SmtpClientPlugin plugin, XWikiContext context)
+    {
+        super(context);
+        setPlugin(plugin);
+    }
+
+    public SmtpClientPlugin getPlugin()
+    {
+        return plugin;
+    }
+
+    public void setPlugin(SmtpClientPlugin plugin)
+    {
+        this.plugin = plugin;
+    }
+
+    /**
+     * Sends an HTML mail, with a list of attachments
+     *
+     * @see #sendHTMLMessage(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.List)
+     *
+     * @param to the recipient of the message
+     * @param from the sender
+     * @param mailHost the mail server host
+     * @param subject the subject of the message
+     * @param body the body content of the mail
+     * @param alternative the alternative text offered to the mail client
+     * @param attachments List of com.xpn.xwiki.api.Attachment that will be attached to the mail.
+     * @return 0 on success, -1 on failure. on failure the error message is stored in XWiki context
+     */
+    public int sendHTMLMessage(String to, String from, String mailHost, String subject, String body,
+        String alternative, List attachments)
+    {
+        try{
+            this.plugin.sendHTMLMessage(to, from, mailHost, subject, body, alternative, attachments);
+            return 0;
+        } catch (PluginException e) {
+            context.put("error",e.getMessage());
+            return -1;
+        }
+    }
+
+    /**
+     * Sends an HTML message, with alternative text
+     *
+     * @param to the recipient of the message
+     * @param from the sender
+     * @param mailHost the mail server host
+     * @param subject the subject of the message
+     * @param body the body content of the mail
+     * @param alternative the alternative text offered to the mail client
+     * @return 0 on success, -1 on failure. on failure the error message is stored in XWiki context
+     */
+    public int sendHTMLMessage(String to, String from, String mailHost, String subject, String body,
+        String alternative)
+    {
+        try{
+            this.plugin.sendHTMLMessage(to, from, mailHost, subject, body, alternative, null);
+            return 0;
+        } catch (PluginException e) {
+            context.put("error",e.getMessage());
+            return -1;
+        }
+    }
+
+    /**
+     * Sends an HTML message from a XWiki Document.
+     * The message body is obtained from the rendering of the passed document.
+     * Attachments are obtained from the document
+     *
+     * @param to the recipient of the message
+     * @param from the sender
+     * @param mailHost the mail server host
+     * @param subject the subject of the message
+     * @param content the document that holds the content to be rendered in HTML and used as mail body
+     * @param alternative the alternative text offered to the mail client
+     * @return 0 on success, -1 on failure. on failure the error message is stored in XWiki context
+     */
+    public int sendHTMLMessageFromDoc(String to, String from, String mailHost, String subject, Document content, String alternative)
+    {
+        try{
+            String body = content.getRenderedContent();
+            List attachments = content.getAttachmentList();
+            this.plugin.sendHTMLMessage(to, from, mailHost, subject, body, alternative, attachments);
+            return 0;
+        } catch (XWikiException e) {
+            context.put("error",e.getMessage());
+            return -1;
+        }
+    }
+
+    /**
+     * Sends a simple text plain mail
+     *
+     * @param to the recipient of the message
+     * @param from the sender
+     * @param mailHost the mail server host
+     * @param subject the subject of the message
+     * @param message the body of the message
+     * @return 0 on success, -1 on failure. on failure the error message is stored in XWiki context
+     */
+    public int sendTextMessage(String to, String from, String mailHost, String subject, String message)
+    {
+        try{
+            this.plugin.sendTextMessage(to, from, mailHost, subject, message);
+            return 0;
+        } catch (Exception e) {
+            context.put("error",e.getMessage());
+            return -1;
+        }
+    }
+
+    /**
+     * Sends a simple text plain mail with a list of files attachments
+     *
+     * @param to the recipient of the message
+     * @param from the sender
+     * @param mailHost the mail server host
+     * @param subject the subject of the message
+     * @param message the body of the message
+     * @param attachments List of com.xpn.xwiki.api.Attachment that will be attached to the mail.
+     * @return 0 on success, -1 on failure. on failure the error message is stored in XWiki context
+     */
+    public int sendTextMessage(String to, String from, String mailHost, String subject, String message, List attachments)
+    {
+        try{
+            this.plugin.sendTextMessageWithAttachments(to, from, mailHost, subject, message, attachments);
+            return 0;
+        } catch (Exception e) {
+            context.put("error",e.getMessage());
+            return -1;
+        }
+    }
+}
\ No newline at end of file
Index: smtpclient/SmtpClientPlugin.java
===================================================================
--- smtpclient/SmtpClientPlugin.java	(revision 0)
+++ smtpclient/SmtpClientPlugin.java	(revision 0)
@@ -0,0 +1,260 @@
+/*
+ * Copyright 2007, XPertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author fabrice
+ */
+package com.xpn.xwiki.plugin.smtpclient;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.activation.FileDataSource;
+import javax.mail.BodyPart;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Multipart;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+
+import com.xpn.xwiki.plugin.PluginException;
+import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
+import com.xpn.xwiki.plugin.XWikiPluginInterface;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.api.Api;
+import com.xpn.xwiki.api.Attachment;
+import com.xpn.xwiki.doc.XWikiAttachment;
+
+public class SmtpClientPlugin extends XWikiDefaultPlugin implements XWikiPluginInterface
+{
+    protected XWikiContext context;
+
+    protected static final String URL_SEPARATOR = "/";
+
+    public SmtpClientPlugin(String name, String className, XWikiContext context)
+    {
+        super(name, className, context);
+        init(context);
+        this.context = context;
+    }
+
+    public String getName()
+    {
+        return "smtpclient";
+    }
+
+    public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context)
+    {
+        return new SmtpClientPluginApi((SmtpClientPlugin) plugin, context);
+    }
+
+    public void init(XWikiContext context)
+    {
+        super.init(context);
+    }
+
+    public void sendHTMLMessage(String to, String from, String mailHost, String subject, String body, String alternative)
+        throws PluginException
+    {
+        try
+        {
+            sendHTML(to, from, mailHost, subject, body, alternative, null);
+        }
+        catch(Throwable t)
+        {
+            throw new PluginException(SmtpClientPlugin.class, 0,
+                "Error while sending message : " + t.getMessage(), t);
+        }
+    }
+
+    public void sendHTMLMessage(String to, String from, String mailHost, String subject, String body,
+        String alternative, List attachments) throws PluginException
+    {
+        try {
+            System.out.println("Trying to sendHTML message...");
+            sendHTML(to, from, mailHost, subject, body, alternative, attachments);
+            System.out.println("Message sent...");
+        }
+        catch (Throwable t) {
+            throw new PluginException(SmtpClientPlugin.class, 0,
+                "Error while sending message : " + t.getMessage(), t);
+        }
+    }
+
+    public void sendTextMessage(String to, String from, String mailHost, String subject, String message)
+        throws MessagingException, IOException, XWikiException
+    {
+        sendText(to,from,mailHost,subject,message,null);
+    }
+
+    public void sendTextMessageWithAttachments(String to, String from, String mailHost, String subject, String message, List attachments)
+        throws MessagingException, IOException, XWikiException
+    {
+        sendText(to,from,mailHost,subject,message,attachments);
+    }
+
+    private void sendText(String to, String from, String mailHost, String subject, String body,
+        List attachments) throws MessagingException, XWikiException, IOException
+    {
+      Properties props = System.getProperties();
+      props.put("mail.smtp.host", mailHost);
+      Session session = Session.getDefaultInstance(props, null);
+
+      Message msg = new MimeMessage(session);
+
+      msg.setFrom(new InternetAddress(from));
+      msg.setRecipients(Message.RecipientType.TO,
+        InternetAddress.parse(to, false));
+
+      msg.setSubject(subject);
+
+      Multipart multipart = new MimeMultipart("mixed");
+      BodyPart part;
+
+      part = new MimeBodyPart();
+      part.setContent(body,"text/plain");
+
+      multipart.addBodyPart(part);
+
+      if (attachments != null) {
+            Iterator i = attachments.iterator();
+            while (i.hasNext()) {
+                Attachment at = (Attachment) i.next();
+                XWikiAttachment att = at.getAttachment();
+                String name = att.getFilename();
+                byte[] stream = att.getContent(context);
+                File temp = File.createTempFile("tmpfile", ".tmp");
+                FileOutputStream fos = new FileOutputStream(temp);
+                fos.write(stream);
+                fos.close();
+                part = new MimeBodyPart();
+                DataSource source = new FileDataSource(temp);
+                part.setDataHandler(new DataHandler(source));
+                part.setHeader("Content-ID", "<" + name + ">");
+                part.setFileName(name);
+                multipart.addBodyPart(part);
+                temp.deleteOnExit();
+            }
+        }
+
+      msg.setContent(multipart);
+
+      msg.setSentDate(new Date());
+
+      Transport.send(msg);
+    }
+
+    private void sendHTML(String to, String from, String mailHost, String subject, String body,
+        String alternative, List attachments) throws MessagingException, XWikiException, IOException
+    {
+
+        Properties props = System.getProperties();
+        props.put("mail.smtp.host", mailHost);
+        Session session = Session.getInstance(props, null);
+
+        Message message = new MimeMessage(session);
+        message.setFrom(new InternetAddress(from));
+        message.setRecipients(Message.RecipientType.TO,
+            new InternetAddress[]{new InternetAddress(to)});
+        message.setSubject(subject);
+
+        Multipart multipart = new MimeMultipart("alternative");
+        BodyPart part;
+
+        part = new MimeBodyPart();
+        part.setText(alternative);
+        multipart.addBodyPart(part);
+
+        Multipart html_mp = new MimeMultipart("related");
+
+        part = new MimeBodyPart();
+
+        part.setContent(processImageURLS(body), "text/html");
+        part.setHeader("Content-Disposition", "inline");
+        part.setHeader("Content-Transfer-Encoding", "quoted-printable");
+
+        html_mp.addBodyPart(part);
+
+        if (attachments != null) {
+            Iterator i = attachments.iterator();
+            while (i.hasNext()) {
+                Attachment at = (Attachment) i.next();
+                XWikiAttachment att = at.getAttachment();
+                String name = att.getFilename();
+                byte[] stream = att.getContent(context);
+                File temp = File.createTempFile("tmpfile", ".tmp");
+                FileOutputStream fos = new FileOutputStream(temp);
+                fos.write(stream);
+                fos.close();
+                part = new MimeBodyPart();
+                DataSource source = new FileDataSource(temp);
+                part.setDataHandler(new DataHandler(source));
+                part.setHeader("Content-ID", "<" + name + ">");
+                part.setFileName(name);
+                html_mp.addBodyPart(part);
+                temp.deleteOnExit();
+            }
+        }
+
+        part = new MimeBodyPart();
+        part.setContent(html_mp);
+        multipart.addBodyPart(part);
+
+        message.setContent(multipart);
+        Transport.send(message);
+    }
+
+    private String processImageURLS(String html)
+    {
+        //this method/design has to be improved
+
+        Pattern img = Pattern.compile("src=[a-zA-Z0-9/_\"]*.(png|jpg|gif|jpeg){1}",
+            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
+        Matcher matcher = img.matcher(html);
+
+        StringBuffer sb = new StringBuffer();
+
+        while (matcher.find()) {
+            String found = matcher.group(0);
+            String replace = found.substring(found.lastIndexOf("/") + 1);
+            matcher.appendReplacement(sb, "src=\"cid:" + replace);
+        }
+        matcher.appendTail(sb);
+
+        return sb.toString();
+    }
+
+    protected String getFileName(String path)
+    {
+        return path.substring(path.lastIndexOf(URL_SEPARATOR) + 1);
+    }
+}
+
