Hi Erik,
I agree that my coding style is different and that Steve's task is the
way to go - even (besides code style) because he was more careful about
compatibility with the existing mail task.
However, there was an interesting bit you missed in my task: having CC
(Carbon Copy) and BCC (Blind Carbon Copy) recipient lists. For some
tasks these can be (as my data exports (o;= ) they can be quite
important.
There was also had a bug on building filenames for filesets - a base
directory argument was missing for building the File objects.
BUT I just loved the HTML mail thing! It really works!
=:oD
I made some fixes for the above problems and I send attached to this
mail both my new version of the MimeMail task ("MimeMail.java") and a
diff ("patch.txt") of the changes I made.
Going trough the changes as shown in the diff file:
1- Changed imports to make dependencies explicit on a class-by-class
basis;
2- Added "ccList" and "bccList" properties;
3- Changed validate() method to reflect the addition of the "ccList"
and "bccList" properties - now the rule for destination is that
at least one of "toList", "ccList" and "bccList" properties
should be filled;
4- Added addRecipients() helper method which helps on adding the
addresses for each of the recipient lists. Another change is that
I use the InternetAddress.parse() method instead of a
StringTokenizer to build parse those lists;
5- Fixed the above mentioned bug by using the base directory for
each file in a file set;
6- Changed the message of the exception thrown when a file does not
exist or is not readable (this helped me diagnosing the above
mentioned bug) in order to also show the absolute path of such
file.
Now I am throwing away my task and using this one.
=:o)
Have fun,
Paulo Gaspar
> -----Original Message-----
> From: Erik Hatcher [mailto:[EMAIL PROTECTED]
> Sent: Monday, June 11, 2001 7:19 AM
> To: ant-dev
> Subject: Re: modifying optional task code / <mimemail>
>
>
> Stefan, Steve, Paulo, and others interested,
>
> Attached is a .zip file with my modifications for the JUnitReport task and
> the MimeMail task. Shall I post the patch difference files also so that
> someone can commit these changes if they are acceptable?
>
> Note - There has been a change since the 1.3 release to
> XMLJUnitResultFormatter, but my code is built on the 1.3 codebase.
>
> The MimeMail task that is attached is a modified version of Steve's task.
> I've also included an HTML file in Ant-style describing its
> usage. I added
> the FileSet capability, and another attribute called 'messageFile' so that
> the message body can be pulled either from the 'message' attribute or from
> the file pointed to by 'messageFile'. The handy thing about
> 'messageFile'
> is that HTML files can be sent so that e-mail readers will recognize them
> inline rather than as an attachment (but the messageMimeType must be
> specified as "text/html" also). This is nice when sending
> JUnitReport HTML
> results.
>
> I will address a few issues that have been brought up:
>
> - Should we merge MimeMail and Mail? It would be nice, but is
> unnecessary.
> Its probably not a huge effort, but more effort than its worth
> since whoever
> needs the MimeMail capability can just get the necessary .jar files and
> <taskdef>.
>
> - Should the other JUnitResultFormatters be modified to pick up the newly
> added properties with the JUnitTask modification? I looked at the
> PlainJUnitResultFormatter, and did not want to mess with its output as
> someone may be relying on it as-is, but its easy enough for any of these
> formatters to be modified to grab these properties if desired.
> startTestSuite(JUnitTest suite) and endTestSuite(JUnitTest
> suite) both, of
> course, have access to the JUnitTest and I added a method on JUnitTest
> called getProperties that hands back a Properties object that contains the
> name/value pairs. Stefan proposed a different method to hand Properties
> off to the formatters - I'm not sure what his proposed method
> gives us that
> mine misses.
>
> - As for Paulo's task - now that MimeMail has been modified to support
> FileSet's there really isn't much difference except MimeMail has the
> messageMimeType. MimeMail seems to be more in line with the coding style
> of the other Ant tasks. I vote we stick with MimeMail and make that an
> optional task.
>
> - I've included the necessary XSL files to generate an overview of JUnit
> tests. I only modified overview-summary.xsl (but toolkit.xsl is
> needed in
> the same directory). I added the display of each test-suites
> properties -
> and in IE5 the display of these properties can be toggled. I
> didn't spend
> a lot of time making this display of the properties really nice, but the
> good thing is now that they are there in the XML its easy enough to do
> something with them. Let me know if you have any suggestions on how they
> should be reported.
>
> - This probably isn't behavior that can be relied upon, but by placing
> JUnitMail.jar into my ANT_HOME/lib directory I'm able to
> <taskdef> MimeMail
> and it works fine, as well as have my modified JUnitTask picked up in the
> classpath prior to the one in optional.jar. I'm assuming this is because
> the loop in Ant.bat to add the lib .jar files is picking them up
> alphabetically. Will this work consistently on Windows and
> other platforms
> in this manner if folks are using the wrapper scripts to start Ant?
>
> Thanks again for everyones assistance,
> Erik
>
>
diff -u oldmail/MimeMail.java mail/MimeMail.java
--- oldmail/MimeMail.java Sun Jun 10 22:05:28 2001
+++ mail/MimeMail.java Mon Jun 11 14:40:09 2001
@@ -44,7 +44,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
- *
+ *
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
@@ -54,18 +54,43 @@
package org.apache.tools.ant.taskdefs.optional.mail;
-import java.io.*;
-import java.util.*;
-import org.apache.tools.ant.*;
-import org.apache.tools.ant.types.*;
-import org.apache.tools.ant.taskdefs.MatchingTask;
-import org.apache.tools.ant.BuildException;
+
+// Standard SDK imports
+import java.util.Properties;
+import java.util.Vector;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
//imported for the mail api
-import javax.mail.*;
-import javax.mail.internet.*;
+import javax.mail.Address;
+import javax.mail.Session;
+import javax.mail.Message;
+import javax.mail.BodyPart;
+import javax.mail.Multipart;
+import javax.mail.SendFailedException;
+import javax.mail.MessagingException;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.AddressException;
+
//imported for data source and handler
-import javax.activation.*;
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+
+// Ant imports
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.FileScanner;
+import org.apache.tools.ant.DirectoryScanner;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.EnumeratedAttribute;
+import org.apache.tools.ant.types.FileSet;
+
+
/**
* A task to send SMTP email. This version has near identical syntax to
@@ -76,6 +101,7 @@
* @author [EMAIL PROTECTED]
* @author [EMAIL PROTECTED] steve loughran
* @author [EMAIL PROTECTED] Erik Hatcher
+ * @author [EMAIL PROTECTED] Paulo Gaspar
* @created 01 May 2001
*/
public class MimeMail extends Task {
@@ -106,11 +132,21 @@
private File messageFile = null;
/**
- * recipients
+ * TO recipients
*/
private String toList = null;
/**
+ * CC (Carbon Copy) recipients
+ */
+ protected String ccList = null;
+
+ /**
+ * BCC (Blind Carbon Copy) recipients
+ */
+ protected String bccList = null;
+
+ /**
* subject field
*/
private String subject = null;
@@ -158,6 +194,24 @@
this.toList = toList;
}
+ /**
+ * Sets the toList parameter of this build task.
+ *
+ * @param toList Comma-separated list of email recipient addreses.
+ */
+ public void setCcList(String ccList) {
+ this.ccList = ccList;
+ }
+
+ /**
+ * Sets the toList parameter of this build task.
+ *
+ * @param toList Comma-separated list of email recipient addreses.
+ */
+ public void setBccList(String bccList) {
+ this.bccList = bccList;
+ }
+
/**
* Sets the "from" parameter of this build task.
@@ -223,8 +277,8 @@
throw new BuildException("Attribute \"from\" is required.");
}
- if (toList == null) {
- throw new BuildException("Attribute \"toList\" is required.");
+ if ((toList == null) && (ccList == null) && (bccList == null)) {
+ throw new BuildException("Attribute \"toList\", \"ccList\" or
\"bccList\" is required.");
}
if (message == null && filesets.isEmpty() && messageFile == null) {
@@ -261,6 +315,28 @@
}
+ // helper method to add recipients
+ private static void addRecipients( MimeMessage msg,
+ Message.RecipientType recipType,
+ String addrUserName,
+ String addrList
+ ) throws MessagingException,
BuildException {
+ if ((null == addrList) || (addrList.trim().length() <= 0))
+ return;
+
+ try {
+ InternetAddress[] addrArray = InternetAddress.parse(addrList);
+
+ if ((null == addrArray) || (0 == addrArray.length))
+ throw new BuildException("Empty " + addrUserName + "
recipients list was specified");
+
+ msg.setRecipients(recipType, addrArray);
+ }
+ catch(AddressException ae) {
+ throw new BuildException("Invalid " + addrUserName + " recipient
list");
+ }
+ }
+
/**
* here is where the mail is sent
*
@@ -284,21 +360,11 @@
log("message sender: " + from, Project.MSG_VERBOSE);
msg.setFrom(new InternetAddress(from));
- //build the recipient list, first cracking the string open
- //into a list to get the size
- Vector addresses = new Vector();
- int count = 0;
- StringTokenizer t = new StringTokenizer(toList, ", ", false);
- while (t.hasMoreTokens()) {
- String to = t.nextToken();
- log("to: " + to, Project.MSG_VERBOSE);
- msg.addRecipient(Message.RecipientType.TO,
- new InternetAddress(to));
- count++;
- }
- if (count == 0) {
- throw new BuildException("no recipients specified");
- }
+ // add recipient lists
+ addRecipients(msg, Message.RecipientType.TO, "To", toList);
+ addRecipients(msg, Message.RecipientType.CC, "Cc", ccList);
+ addRecipients(msg, Message.RecipientType.BCC, "Bcc", bccList);
+
if (subject != null) {
log("subject: " + subject, Project.MSG_VERBOSE);
msg.setSubject(subject);
@@ -328,22 +394,23 @@
textbody.setContent(message, messageMimeType);
attachments.addBodyPart(textbody);
}
-
+
for (int i = 0; i < filesets.size(); i++)
{
FileSet fs = (FileSet) filesets.elementAt(i);
if (fs != null)
{
- FileScanner ds = fs.getDirectoryScanner(project);
+ DirectoryScanner ds = fs.getDirectoryScanner(project);
String[] dsfiles = ds.getIncludedFiles();
+ File baseDir = ds.getBasedir();
for (int j = 0; j < dsfiles.length; j++)
{
- File file = new File(dsfiles[j]);
+ File file = new File(baseDir, dsfiles[j]);
MimeBodyPart body;
body = new MimeBodyPart();
if (!file.exists() || !file.canRead()) {
- throw new BuildException("File \"" + file.getName()
+ throw new BuildException("File \"" +
file.getAbsolutePath()
+ "\" does not exist or is not readable.");
}
log("Attaching " + file.toString()+" - " +file.length()+"
bytes",
MimeMail.java
Description: JavaScript source
