Changes below include:
Modified build.xml to test for the existence of javax.mail.MimeMessage
Modified index.html
Modified defaults.properties
Email.java source to be added to optional taskdefs directory.This functionality could be used to notify users about the status of a nightly build process to anything device that has an email address. It could also be used to "forward" build output to mail filter on multiple machines that would then operate on the MIME attachments, etc.
Please note that I did not mean to conflict with James Duncan Davidson or Jason Hunter here. I had already implemented this task before the discussion of adding the com.oreilly.servlet.MailMessage took place, and I decided to err on the side of openness and not hold on to my code. In other words, this is my first patch submission, and I definitely don't want to step on anyone's toes. ( Especially the guy who wrote the book I learned servlets from. :-| )
PS: One thing that might help this is if the Available task could check for the existence of more than one class at a time. Only checking for javax.mail.MimeMessage is dangerous, because I'm using the JAF to deal with the mime attachments.
Tim O'Brien [EMAIL PROTECTED]
Index: build.xml =================================================================== RCS file: /home/cvspublic/jakarta-ant/build.xml,v retrieving revision 1.22 diff -u -r1.22 build.xml --- build.xml 2000/06/23 16:46:38 1.22 +++ build.xml 2000/06/24 02:58:52 @@ -41,6 +41,8 @@ classname="com.kvisco.xsl.XSLProcessor" /> <available property="xalan.present" classname="org.apache.xalan.xslt.XSLTProcessorFactory" /> + <available property="javamail.present" + classname="javax.mail.MimeMessage"/> </target>
<!-- =================================================================== -->
@@ -66,6 +68,7 @@
<exclude name="**/NetRexxC.java" unless="netrexx.present" />
<exclude name="**/XslpLiaison.java" unless="xslp.present" />
<exclude name="**/XalanLiaison.java" unless="xalan.present" />
+ <exclude name="**/Email.java" unless="javamail.present" />
</javac>
<copydir src="${src.dir}" dest="${build.classes}">Index: index.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/index.html,v
retrieving revision 1.28
diff -u -r1.28 index.html
--- index.html 2000/06/23 16:46:45 1.28
+++ index.html 2000/06/24 03:00:10
@@ -19,6 +19,7 @@
<li>Arnout J. Kuiper (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li>
<li>Stefano Mazzocchi (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li>
<li>Sam Ruby (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li>
+ <li>Tim O'Brien (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li>
</ul>
<p>Version 1.0.8.1 - 2000/06/13</p> @@ -2571,10 +2572,71 @@
<h2><a name="optionaltasks">Optional tasks</a></h2>
<ul>
+ <li><a href="#email">Email</a></li>
<li><a href="#netrexxc">NetRexxC</a></li>
<li><a href="#renameexts">RenameExtensions</a></li>
<li><a href="#script">Script</a></li>
</ul>
+<hr>
+<h2><a name="email">Email</a></h2>
+<h3><b>Description:</b></h3>
+<p>Emails a message originating from a supplied string or file, and can also send multiple files as Mime attachments.
+</p>
+<h3>Parameters:</h3>
+<table border="1" cellpadding="2" cellspacing="0">
+ <tr>
+ <td valign="top"><b>Attribute</b></td>
+ <td valign="top"><b>Description</b></td>
+ <td align="center" valign="top"><b>Required</b></td>
+ </tr>
+ <tr>
+ <td valign="top">mailto</td>
+ <td valign="top">comma separated list of email addresses to send message to.</Td>
+ <td valign="top" align="center">Yes</td>
+ </tr>
+ <tr>
+ <td valign="top">mailfrom</td>
+ <td valign="top">email address that message is from.</td>
+ <td valign="top" align="center">Yes</td>
+ </tr>
+ <tr>
+ <td valign="top">subject</td>
+ <td valign="top">subject of message to send. (default is "Ant Notification")</td>
+ <td valign="top" align="center">No</td>
+ </tr>
+ <tr>
+ <td valign="top">message</td>
+ <td valign="top">message to include in email body. ( message OR messageFile must be present. )</td>
+ <td valign="top" align="center">No</td>
+ </tr>
+ <tr>
+ <td valign="top">messagefile</td>
+ <td valign="top">comma separated list of files to include in the message body.</td>
+ <td valign="top" align="center">No</td>
+ </tr>
+ <tr>
+ <td valign="top">attachfile</td>
+ <td valign="top">comma separated list of files to attach to email message.</td>
+ <td valign="top" align="center">No</td>
+ </tr>
+ <tr>
+ <td valign="top">smtphost</td>
+ <td valign="top">SMTP host to send message with. (default is "localhost")</td>
+ <td valign="top" align="center">No</td>
+ </tr>
+</table>
+<h3>Examples</h3>
+<blockquote>
+ <p><code><email mailto="[EMAIL PROTECTED]"
+ mailfrom="[EMAIL PROTECTED]"
+ subject="Build Status"
+ message="Build Success!"
+ messagefile="status.log,error.log"
+ attachfile="build.xml,distribution.tar.gz"
+ smtphost="mail.somedomain.com"/>
+</code>
+ </p>
+</blockquote>
<hr>
<h2><a name="netrexxc">NetRexxC</a></h2>
<h3><b>Description:</b></h3>
Index: defaults.properties
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults. properties,v
retrieving revision 1.15
diff -r1.15 defaults.properties
39a40
> email=org.apache.tools.ant.taskdefs.optional.Email
************************ And add the following file to the optional taskdefs src directory: Email.java BEGIN FILE: ************************
/* * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * 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 * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */
package org.apache.tools.ant.taskdefs.optional;
import org.apache.tools.ant.*; import java.io.*; import java.net.*; import java.util.*;
import javax.mail.*; import javax.mail.internet.*;
import javax.activation.*;
/**
* Task to send email messages from Ant. This task can take the following
* arguments:
* <ul>
* <li><B>mailto</B> - A comma separated list of email addresses</li>
* <li><B>mailfrom</B> - An email address from which mail is sent</li>
* <li><B>subject</B> - The subject of the email message</li>
* <li><B>message</B> - The message body</li>
* <li><B>messagefile</B> - A comma separated list of files to include in the message body</li>
* <li><B>attachfile</B> - A comma separated list of files to attach as MIME attachments</li>
* <li><B>smtphost</B> - SMTP host to send mail</LI>
* </ul>
* Of these arguments, <b>mailto</b> and <b>mailfrom</b> must be specified, and one of the following
* arguments must be present <B>message</B> and <B>messagefile</B>.
*
* @author Tim O'Brien <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
*/
public class Email extends Task {
private static final String delimiter = ",";
private File messageFile;
private File[] attachFiles;
private InternetAddress[] toAddresses;
private InternetAddress fromAddress;
private String subject = "Ant notification";
private String message;
private String smtpHost = "localhost"; public void setMessagefile(String msgfile) { try {messageFile = project.resolveFile(msgfile);
} catch( Exception e ) {throw new BuildException( "Email: messagefile " + msgfile + " not found." );
}
}
public void setAttachfile(String attachfile) {StringTokenizer sToke = new StringTokenizer( attachfile, delimiter );
ArrayList tokens = new ArrayList();
while( sToke.hasMoreElements() ) {tokens.add( sToke.nextElement() );
}
attachFiles = new File[ tokens.size() ];
for( int i = 0; i < tokens.size(); i++ ) { try {File fileToAttach = project.resolveFile( (String) tokens.get(i) );
attachFiles[i] = fileToAttach;
} catch( Exception e ) {throw new BuildException( "Email: attachfile " + tokens.get(i) + " not found." );
}
}
}
public void setMailto( String to_addr ) {StringTokenizer sToke = new StringTokenizer( to_addr, delimiter );
ArrayList tokens = new ArrayList();
while( sToke.hasMoreElements() ) {tokens.add( sToke.nextElement() );
}
toAddresses = new InternetAddress[ tokens.size() ];
for( int i = 0; i < tokens.size(); i++ ) { try {toAddresses[i] = new InternetAddress( (String) tokens.get(i) );
} catch( AddressException e ) {throw new BuildException( "Email: Invalid email address supplied in mailto " + to_addr );
}
}
}
public void setMailfrom( String from_addr ) { try {fromAddress = new InternetAddress( from_addr );
} catch( Exception e ) {throw new BuildException( "Email: Invalid email address supplied in mailfrom " + from_addr );
}
}
public void setSubject( String subj ) {
subject = subj;
} public void setMessage( String msg ) {
message = msg;
} public void setSmtphost( String host ) {
smtpHost = host;
} /**
* Does the work.
*
* @exception BuildException if someting goes wrong with the build
*/
public void execute() throws BuildException { if( toAddresses == null ) { throw new BuildException("Email: mailto is not specified");}
if( fromAddress == null ) { throw new BuildException("Email: mailfrom is not specified" );}
if( message == null && messageFile == null ) {throw new BuildException("Email: message and messagefile missing, one must be present.");
}
// Create properties to hold smtpHost for Session.
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);Session session = Session.getDefaultInstance(props, null);
try { // Create a MimeMessage
MimeMessage msg = new MimeMessage(session); // Set the from and to addresses
// Set the subject and date.
msg.setFrom( fromAddress );
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date()); // create the Multipart and its parts to it
Multipart mp = new MimeMultipart();MimeBodyPart mbp1 = new MimeBodyPart();
StringBuffer messageBody = new StringBuffer();
if( message != null ) {messageBody.append( message );
}
if( messageFile != null ) {FileReader mFileReader = null;
try {mFileReader = new FileReader( messageFile );
} catch( FileNotFoundException e ) {}
LineNumberReader lnReader = new LineNumberReader( mFileReader );
String currentLine = new String();
try {messageBody.append( "\n\n********* Included File: " + messageFile.getName() + "\n\n");
while( ( currentLine = lnReader.readLine() ) != null ) {messageBody.append( currentLine + "\n");
}
} catch( IOException e ) {}
}
mbp1.setText( messageBody.toString() );
mp.addBodyPart(mbp1);
if( attachFiles != null ) { for( int i = 0; i < attachFiles.length; i++ ) { // create and fill the second message part
MimeBodyPart mbp = new MimeBodyPart(); FileDataSource fds= new FileDataSource(attachFiles[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName()); // Add this file as a MIME attachment
mp.addBodyPart(mbp);}
}
// add the Multipart to the message
msg.setContent(mp); // Send the email
Transport.send(msg); } catch (MessagingException mex) {throw new BuildException( mex );
}
}}
