Index: xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java
===================================================================
--- xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java	(revision 8210)
+++ xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java	Mon Mar 10 14:23:07 PDT 2008
@@ -152,4 +152,30 @@
             return -1;
         }
     }
+
+    /**
+     *
+     * @return An empty mail message to be populated with recipient addresses, subject, message, etc.
+     */
+    public Mail getMail()
+    {
+        return new Mail();
-}
+    }
+
+    /**
+     *
+     * @param mail A populated mail message
+     * @return 0 on success, -1 on failure. on failure the error message is stored in XWiki context
+     */
+    public int sendMail(Mail mail)
+    {
+        try {
+            getMailSenderPlugin().sendMail(mail, context);
+            return 0;
+        } catch (Exception e) {
+            context.put("error", e.getMessage());
+            LOG.error("sendMailMessage", e);
+            return -1;
+        }
+    }
+}
Index: xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java
===================================================================
--- xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java	(revision 8210)
+++ xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java	Mon Mar 10 14:11:13 PDT 2008
@@ -30,6 +30,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
+import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javax.activation.DataHandler;
@@ -294,6 +295,10 @@
 
         message.setSubject(mail.getSubject(), "UTF-8");
 
+        for(Map.Entry<String,String> header : mail.getHeaders().entrySet()) {
+            message.setHeader(header.getKey(), header.getValue());
+        }
+
         if (mail.getHtmlPart() != null || mail.getAttachments() != null) {
             Multipart multipart = createMimeMultipart(mail, context);
             message.setContent(multipart);
@@ -397,7 +402,7 @@
      *
      * @return The properties
      */
-    private Properties initProperties(XWikiContext context)
+    private Properties initProperties(XWikiContext context, String smtpFrom)
     {
         Properties properties = new Properties();
 
@@ -411,6 +416,13 @@
             context != null ? context.getWiki().getXWikiPreference("smtp_server", context)
                 : "localhost";
         properties.put("mail.smtp.localhost", "localhost");
+        String returnPath =
+            smtpFrom != null ? smtpFrom
+                             : context != null ? context.getWiki().Param("smtp.mail.from")
+                                               : null;
+        if (returnPath != null) {
+            properties.put("mail.smtp.from", returnPath);
+        }
         properties.put("mail.host", "localhost");
         properties.put("mail.smtp.host", smtp);
         properties.put("mail.debug", "false");
@@ -502,16 +514,17 @@
         try {
             for (Iterator emailIt = emails.iterator(); emailIt.hasNext();) {
                 count++;
+
+                Mail mail = (Mail) emailIt.next();
+                LOG.info("Sending email: " + mail.toFullString());
+
                 if ((transport == null) || (session == null)) {
-                    Properties props = initProperties(context);
+                    Properties props = initProperties(context, mail.getSmtpFrom());
                     session = Session.getDefaultInstance(props, null);
                     transport = session.getTransport("smtp");
                     transport.connect();
                 }
 
-                Mail mail = (Mail) emailIt.next();
-                LOG.info("Sending email: " + mail.toFullString());
-
                 try {
 
                     MimeMessage message = createMimeMessage(mail, session, context);
Index: xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/Mail.java
===================================================================
--- xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/Mail.java	(revision 8210)
+++ xwiki-platform-plugins/mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/Mail.java	Mon Mar 10 14:23:51 PDT 2008
@@ -20,6 +20,8 @@
 package com.xpn.xwiki.plugin.mailsender;
 
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 /**
  * The variables to, cc, bcc can contain several email addresses, separated by commas.
@@ -34,7 +36,9 @@
     private String textPart;
     private String htmlPart;
     private List attachments;
+    private Map<String,String> headers;
+    private String smtpFrom;
-    
+
 
     public List getAttachments()
     {
@@ -51,6 +55,7 @@
     public Mail()
     {
         super();
+        headers = new HashMap<String,String>();
     }
     
     
@@ -145,4 +150,32 @@
         this.htmlPart = htmlPart;
     }
 
+    public void setHeader(String header, String value)
+    {
+        headers.put(header, value);
-}
+    }
+
+    public String getHeader(String header)
+    {
+        return headers.get(header);
+    }
+
+    public void setHeaders(Map<String, String> headers)
+    {
+        this.headers = headers;
+    }
+
+    public Map<String,String> getHeaders(){
+        return headers;
+    }
+
+    public void setSmtpFrom(String from)
+    {
+        smtpFrom = from;
+    }
+
+    public String getSmtpFrom()
+    {
+        return smtpFrom;
+    }
+}
