<mimemail> tasks. * Deprecated <mimemail> task, and old SendEmail and MimeMail classes. Submitted by Rob Oxspring <roxspring@yahoo.com>. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271148 13f79535-47bb-0310-9956-ffa450edef68master
@@ -189,6 +189,9 @@ | |||
<available property="bcel.present" | |||
classname="org.apache.bcel.Constants" | |||
classpathref="classpath" /> | |||
<available property="sunuue.present" | |||
classname="sun.misc.UUEncoder" | |||
classpathref="classpath" /> | |||
<condition property="javamail.complete"> | |||
<and> | |||
@@ -269,6 +272,9 @@ | |||
<exclude name="${ant.package}/listener/Log4jListener.java" | |||
unless="log4j.present" /> | |||
<exclude name="${ant.package}/taskdefs/email/UUMailer.java" unless="sunuue.present" /> | |||
<exclude name="${ant.package}/taskdefs/email/MimeMailer.java" unless="javamail.complete" /> | |||
<exclude name="${optional.package}/IContract.java" unless="icontract.present" /> | |||
<exclude name="${optional.package}/Script.java" unless="bsf.present" /> | |||
<exclude name="${optional.package}/StyleBook.java" unless="stylebook.present" /> | |||
@@ -286,7 +292,6 @@ | |||
<exclude name="${optional.package}/ejb/IPlanet*.java" unless="ejbjar.support" /> | |||
<exclude name="${optional.package}/Javah.java" unless="jdk1.2+" /> | |||
<exclude name="${optional.package}/junit/*" unless="junit.present" /> | |||
<exclude name="${optional.package}/net/MimeMail.java" unless="javamail.complete" /> | |||
<exclude name="${optional.package}/net/FTP.java" unless="netcomp.present" /> | |||
<exclude name="${optional.package}/net/TelnetTask.java" unless="netcomp.present" /> | |||
<exclude name="${optional.package}/scm/AntStarTeam*.java" unless="starteam.present" /> | |||
@@ -9,9 +9,10 @@ | |||
<h2><a name="mail">Mail</a></h2> | |||
<h3>Description</h3> | |||
<p>A task to send SMTP email. Text and text files to include in the message | |||
body may be specified. To send binary attachments the optional | |||
<a href="../OptionalTasks/mimemail.html">MimeMail</a> task should be used instead</p> | |||
<p>A task to send SMTP email. This task can send mail using either plain | |||
text, uu encoding or Mime format mail depending on what is available. | |||
Attachments may be sent using nested | |||
<a href="../CoreTypes/fileset.html">fileset</a> elements.</p> | |||
<h3>Parameters</h3> | |||
<table border="1" cellpadding="2" cellspacing="0"> | |||
@@ -23,45 +24,75 @@ body may be specified. To send binary attachments the optional | |||
<tr> | |||
<td valign="top">from</td> | |||
<td valign="top">Email address of sender.</td> | |||
<td align="center" valign="top">Yes</td> | |||
<td align="center" valign="top">Either a <code>from</code> attribute, or a <code><from></code> | |||
element.</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">tolist</td> | |||
<td valign="top">Comma-separated list of recipients.</td> | |||
<td align="center" valign="top">Yes</td> | |||
<td align="center" valign="middle" rowspan="3">At least one of these, or the | |||
equivalent elements.</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">cclist</td> | |||
<td valign="top">Comma-separated list of recipients to carbon copy</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">bcclist</td> | |||
<td valign="top">Comma-separated list of recipients to carbon copy</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">message</td> | |||
<td valign="top">Message to send in the body of the email.</td> | |||
<td align="center" valign="middle" rowspan="2">One of the two</td> | |||
<td align="center" valign="middle" rowspan="2">One of these or a | |||
<code><message></code> element.</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">messagefile</td> | |||
<td valign="top">File to send as the body of the email.</td> | |||
</tr> | |||
<td valign="top">messagemimetype</td> | |||
<td valign="top">The content type of the message. The default is | |||
<code>text/plain</code>.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">files</td> | |||
<td valign="top">Filename(s) of text to send in the body of the email. | |||
Multiple files are comma-separated.</td> | |||
<td valign="top">Files to send as attachments to the email. Separate multiple | |||
file names using a comma. You can also use <code><fileset></code> | |||
elements to specify files.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">failonerror</td> | |||
<td valign="top">flag to indicate whether to halt the build on | |||
any error.</td> | |||
<td align="center" valign="top">No, default is <I>true</I></td> | |||
</tr> | |||
<td valign="top">flag to indicate whether to halt the build on | |||
any error. The default value is <code>true</code>.</td> | |||
<td align="center" valign="top">No.</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">includefilenames</td> | |||
<td valign="top">Include filename(s) before file contents. | |||
Valid only when the files attribute is specified.</td> | |||
<td align="center" valign="top">No, default is <I>false</I></td> | |||
Valid only when the <code>plain</code> encoding is used. The default | |||
value is <code>false</code>.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">mailhost</td> | |||
<td valign="top">Host name of the mail server.</td> | |||
<td align="center" valign="top">No, default is "localhost"</td> | |||
<td valign="top">Host name of the SMTP server. The default value is | |||
<code>localhost</code>.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">mailport</td> | |||
<td valign="top">Port of the mail server.</td> | |||
<td align="center" valign="top">No, default to SMTP default (25)</td> | |||
<td valign="top">TCP port of the SMTP server. The default value is 25.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">encoding</td> | |||
<td valign="top">Specifies the encoding to use for the content of the email. | |||
Values are <code>mime</code>, <code>uu</code>, <code>plain</code>, or | |||
<code>auto</code>. The default value is <code>auto</code>.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">subject</td> | |||
@@ -69,13 +100,84 @@ body may be specified. To send binary attachments the optional | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
</table> | |||
<h3>Parameters specified as nested elements</h3> | |||
<h4>to / cc / bcc / from</h4> | |||
<p>Adds an email address element. It takes the following attributes:</p> | |||
<table width="60%" 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">name</td> | |||
<td valign="top">The display name for the address.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">address</td> | |||
<td valign="top">The email address.</td> | |||
<td align="center" valign="top">Yes</td> | |||
</tr> | |||
</table> | |||
<h4>message</h4> | |||
<p>Specifies the message to include in the email body. It takes the following | |||
attributes:</p> | |||
<table width="60%" 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">src</td> | |||
<td valign="top">The file to use as the message.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">mimetype</td> | |||
<td valign="top">The content type to use for the message.</td> | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
</table> | |||
<p>If the <code>src</code> attribute is not specified, then text can be added | |||
inside the <code><message></code> element.</p> | |||
<h3>Examples</h3> | |||
<pre> | |||
<mail from="me" tolist="you" subject="Results of nightly build" | |||
files="build.log"/></pre> | |||
<p>Sends an eMail from <i>me</i> to <i>you</i> with a subject of | |||
<i>Results of nightly build</i> and includes the contents of <i>build.log</i> | |||
in the body of the message.</p> | |||
<blockquote><pre> | |||
<mail from="me" | |||
tolist="you" | |||
subject="Results of nightly build" | |||
files="build.log" /> | |||
</pre></blockquote> | |||
<p>Sends an email from <i>me</i> to <i>you</i> with a subject of | |||
<i>Results of nightly build</i> and includes the contents of the file | |||
<i>build.log</i> in the body of the message.</p> | |||
<blockquote><pre> | |||
<mail mailhost="smtp.myisp.com" mailport="1025" subject="Test build"> | |||
<from name="me@myisp.com"> | |||
<to name="all@xyz.com"> | |||
<message>A new nightly build has completed</message> | |||
<fileset dir="dist"> | |||
<includes name="**/*.zip"/> | |||
</fileset> | |||
</mail> | |||
</pre></blockquote> | |||
<p>Sends an eMail from <i>me@myisp.com</i> to <i>all@xyz.com</i> with a subject of | |||
<i>Test Build</i> and attaches any zip files from the dist directory. The | |||
task will attempt to use JavaMail and fall back to UU encoding or no encoding in | |||
that order depending on what support classes are available.</p> | |||
<hr> | |||
<p align="center">Copyright © 2001-2002 Apache Software Foundation. All rights | |||
Reserved.</p> | |||
@@ -8,10 +8,13 @@ | |||
<body> | |||
<h2><a name="mimemail">MimeMail</a></h2> | |||
<h3><i>Deprecated</i></h3> | |||
<p><i>This task has been deprecated. Use the <a href="../CoreTasks/mail.html">mail</a> task instead.</i></p> | |||
<h3>Description</h3> | |||
<p>Sends SMTP mail with MIME attachments. If no attachments | |||
are needed, use the built-in <a href="../CoreTasks/mail.html">Mail</a> | |||
task. <a href="http://java.sun.com/products/javamail/index.html">JavaMail</a> | |||
<p>Sends SMTP mail with MIME attachments. | |||
<a href="http://java.sun.com/products/javamail/index.html">JavaMail</a> | |||
and <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java | |||
Activation Framework</a> are required for this task.</p> | |||
<p>Multiple files can be attached using <a href="../CoreTypes/fileset.html">FileSets.</a></p> | |||
@@ -79,7 +82,7 @@ Activation Framework</a> are required for this task.</p> | |||
</table> | |||
<h3>Examples</h3> | |||
<p><b>Send a single HTML file as the body of a message</b></p> | |||
<pre> <mimemail messageMimeType="text/html" messageFile="overview-summary.html" | |||
<pre> <mimemail messageMimeType="text/html" messageFile="overview-summary.html" | |||
tolist="you" subject="JUnit Test Results: ${TODAY}" from="me"/></pre> | |||
<p><b>Sends all files in a directory as attachments</b></p> | |||
<pre> <mimemail message="See attached files" tolist="you" subject="Attachments" from="me"> | |||
@@ -88,7 +91,7 @@ Activation Framework</a> are required for this task.</p> | |||
</fileset> | |||
</mimemail> | |||
</pre> | |||
<hr><p align="center">Copyright © 2001 Apache Software Foundation. All rights | |||
<hr><p align="center">Copyright © 2001-2002 Apache Software Foundation. All rights | |||
Reserved.</p> | |||
</body> | |||
@@ -342,13 +342,13 @@ Installing Ant / Optional Tasks</a> section above.</p> | |||
</tr> | |||
<tr> | |||
<td>mail.jar</td> | |||
<td>MimeMail task</td> | |||
<td>Mail task with Mime encoding, and the MimeMail task</td> | |||
<td><a href="http://java.sun.com/products/javamail/" | |||
target="_top">http://java.sun.com/products/javamail/</a></td> | |||
</tr> | |||
<tr> | |||
<td>activation.jar</td> | |||
<td>MimeMail task</td> | |||
<td>Mail task with Mime encoding, and the MimeMail task</td> | |||
<td><a href="http://java.sun.com/products/javabeans/glasgow/jaf.html" | |||
target="_top">http://java.sun.com/products/javabeans/glasgow/jaf.html</a></td> | |||
</tr> | |||
@@ -31,7 +31,7 @@ | |||
<a href="OptionalTasks/junitreport.html">JUnitReport</a><br> | |||
<a href="OptionalTasks/mmetrics.html">Metamata Metrics</a><br> | |||
<a href="OptionalTasks/maudit.html">Metamata Audit</a><br> | |||
<a href="OptionalTasks/mimemail.html">MimeMail</a><br> | |||
<a href="OptionalTasks/mimemail.html"><i>MimeMail</i></a><br> | |||
<a href="OptionalTasks/mparse.html">MParse</a><br> | |||
<a href="OptionalTasks/native2ascii.html">Native2Ascii</a><br> | |||
<a href="OptionalTasks/netrexxc.html">NetRexxC</a><br> | |||
@@ -51,23 +51,10 @@ | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.taskdefs; | |||
import java.io.PrintStream; | |||
import java.io.File; | |||
import java.io.BufferedInputStream; | |||
import java.io.FileInputStream; | |||
import java.io.IOException; | |||
import java.util.Vector; | |||
import java.util.StringTokenizer; | |||
import java.util.Enumeration; | |||
import org.apache.tools.mail.ErrorInQuitException; | |||
import org.apache.tools.mail.MailMessage; | |||
package org.apache.tools.ant.taskdefs; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.taskdefs.email.EmailTask; | |||
/** | |||
* A task to send SMTP email. | |||
@@ -119,200 +106,16 @@ import org.apache.tools.ant.BuildException; | |||
* @author glenn_twiggs@bmc.com | |||
* @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a> | |||
*/ | |||
public class SendEmail extends Task { | |||
private String from; | |||
private String mailhost = "localhost"; | |||
private int mailport = MailMessage.DEFAULT_PORT; | |||
private String message; | |||
private String toList; | |||
private String subject; | |||
private Vector files = new Vector(); | |||
private boolean includefilenames; | |||
/** | |||
* failure flag | |||
*/ | |||
private boolean failOnError = true; | |||
/** Creates new SendEmail */ | |||
public SendEmail() { | |||
} | |||
/** | |||
* Sets the toList parameter of this build task. | |||
* | |||
* @param toList Comma-separated list of email recipient addreses. | |||
*/ | |||
public void setToList(String toList) { | |||
this.toList = toList; | |||
} | |||
/** | |||
* Sets the from parameter of this build task. | |||
* | |||
* @param from Email address of sender. | |||
*/ | |||
public void setFrom(String from) { | |||
this.from = from; | |||
} | |||
/** | |||
* Sets the mailhost parameter of this build task. | |||
* | |||
* @param mailhost Mail host name. | |||
*/ | |||
public void setMailhost(String mailhost) { | |||
this.mailhost = mailhost; | |||
} | |||
public class SendEmail extends EmailTask | |||
{ | |||
/** | |||
* Sets the mailport parameter of this build task. | |||
* @param value mail port name. | |||
*/ | |||
public void setMailport(Integer value){ | |||
this.mailport = value.intValue(); | |||
} | |||
/** | |||
* Sets the message parameter of this build task. | |||
* | |||
* @param message Message body of this email. | |||
*/ | |||
public void setMessage(String message) { | |||
this.message = message; | |||
} | |||
/** | |||
* Sets the subject parameter of this build task. | |||
* | |||
* @param subject Subject of this email. | |||
*/ | |||
public void setSubject(String subject) { | |||
this.subject = subject; | |||
} | |||
/** | |||
* Sets the file parameter of this build task. | |||
* | |||
* @param filenames Filenames to include as the message body of this email. | |||
*/ | |||
public void setFiles(String filenames) { | |||
StringTokenizer t = new StringTokenizer(filenames, ", "); | |||
while (t.hasMoreTokens()) { | |||
files.addElement(project.resolveFile(t.nextToken())); | |||
} | |||
} | |||
/** | |||
* Sets Includefilenames attribute | |||
* | |||
* @param includefilenames Set to true if file names are to be included. | |||
* @since 1.5 | |||
*/ | |||
public void setIncludefilenames(boolean includefilenames) { | |||
this.includefilenames = includefilenames; | |||
} | |||
/** | |||
* Sets the FailOnError attribute of the MimeMail object | |||
* | |||
* @param failOnError The new FailOnError value | |||
* @since 1.5 | |||
* @deprecated Use {@link #setMailport(int)} instead. | |||
*/ | |||
public void setFailOnError(boolean failOnError) { | |||
this.failOnError = failOnError; | |||
public void setMailport( Integer value ) | |||
{ | |||
setMailport( value.intValue() ); | |||
} | |||
/** | |||
* Executes this build task. | |||
* | |||
* @throws BuildException if there is an error during task execution. | |||
*/ | |||
public void execute() throws BuildException { | |||
try { | |||
MailMessage mailMessage = new MailMessage(mailhost); | |||
mailMessage.setPort(mailport); | |||
if (from != null) { | |||
mailMessage.from(from); | |||
} else { | |||
throw new BuildException("Attribute \"from\" is required."); | |||
} | |||
if (toList != null) { | |||
StringTokenizer t = new StringTokenizer(toList, ", ", false); | |||
while (t.hasMoreTokens()) { | |||
mailMessage.to(t.nextToken()); | |||
} | |||
} else { | |||
throw new BuildException("Attribute \"toList\" is required."); | |||
} | |||
if (subject != null) { | |||
mailMessage.setSubject(subject); | |||
} | |||
if (!files.isEmpty()) { | |||
PrintStream out = mailMessage.getPrintStream(); | |||
for (Enumeration e = files.elements(); e.hasMoreElements(); ) { | |||
File file = (File)e.nextElement(); | |||
if (file.exists() && file.canRead()) { | |||
int bufsize = 1024; | |||
int length; | |||
byte[] buf = new byte[bufsize]; | |||
if (includefilenames) { | |||
String filename = file.getName(); | |||
int filenamelength = filename.length(); | |||
out.println(filename); | |||
for (int star=0; star < filenamelength; star++) { | |||
out.print('='); | |||
} | |||
out.println(); | |||
} | |||
BufferedInputStream in = null; | |||
try { | |||
in = new BufferedInputStream( | |||
new FileInputStream(file), bufsize); | |||
while ((length = in.read(buf, 0, bufsize)) != -1) { | |||
out.write(buf, 0, length); | |||
} | |||
if (includefilenames) { | |||
out.println(); | |||
} | |||
} finally { | |||
if (in != null) { | |||
try { | |||
in.close(); | |||
} catch (IOException ioe) {} | |||
} | |||
} | |||
} else { | |||
throw new BuildException("File \"" + file.getName() | |||
+ "\" does not exist or is not readable."); | |||
} | |||
} | |||
} else if (message != null) { | |||
PrintStream out = mailMessage.getPrintStream(); | |||
out.print(message); | |||
} else { | |||
throw new BuildException("Attribute \"file\" or \"message\" is required."); | |||
} | |||
log("Sending email"); | |||
mailMessage.sendAndClose(); | |||
} catch (IOException ioe) { | |||
String err="IO error sending mail: "+ioe.toString(); | |||
if (!failOnError || ioe instanceof ErrorInQuitException) { | |||
log(err, Project.MSG_ERR); | |||
} else { | |||
throw new BuildException(err,ioe,location); | |||
} | |||
} | |||
} | |||
} |
@@ -38,7 +38,7 @@ antstructure=org.apache.tools.ant.taskdefs.AntStructure | |||
execon=org.apache.tools.ant.taskdefs.ExecuteOn | |||
antcall=org.apache.tools.ant.taskdefs.CallTarget | |||
sql=org.apache.tools.ant.taskdefs.SQLExec | |||
mail=org.apache.tools.ant.taskdefs.SendEmail | |||
mail=org.apache.tools.ant.taskdefs.email.EmailTask | |||
fail=org.apache.tools.ant.taskdefs.Exit | |||
war=org.apache.tools.ant.taskdefs.War | |||
uptodate=org.apache.tools.ant.taskdefs.UpToDate | |||
@@ -0,0 +1,127 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 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", "Ant", 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 apache@apache.org. | |||
* | |||
* 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.email; | |||
/** | |||
* Holds an email address. | |||
* | |||
* @author roxspring@yahoo.com Rob Oxspring | |||
* @since 1.5 | |||
*/ | |||
public class EmailAddress | |||
{ | |||
private String name; | |||
private String address; | |||
/** Creates an empty email address | |||
*/ | |||
public EmailAddress() | |||
{ | |||
} | |||
/** Creates a new email address based on the given string | |||
* @param address the email address | |||
*/ | |||
public EmailAddress( String address ) | |||
{ | |||
this.address = address; | |||
} | |||
/** sets the personal / display name of the address | |||
* @param name the display name | |||
*/ | |||
public void setName( String name ) | |||
{ | |||
this.name = name; | |||
} | |||
/** sets the email address | |||
* @param address the actual email address | |||
*/ | |||
public void setAddress( String address ) | |||
{ | |||
this.address = address; | |||
} | |||
/** Constructs a string "name <address>" or "address" | |||
* @return a string representation of the address | |||
*/ | |||
public String toString() | |||
{ | |||
if( name == null ) | |||
{ | |||
return address; | |||
} | |||
else | |||
{ | |||
return name + " <" + address + ">"; | |||
} | |||
} | |||
/** returns the address | |||
* @return the address part | |||
*/ | |||
public String getAddress() | |||
{ | |||
return address; | |||
} | |||
/** returns the display name | |||
* @return the display name part | |||
*/ | |||
public String getName() | |||
{ | |||
return name; | |||
} | |||
} |
@@ -0,0 +1,529 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2000-2002 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", "Ant", 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 apache@apache.org. | |||
* | |||
* 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.email; | |||
// Ant imports | |||
import java.io.File; | |||
import java.util.Enumeration; | |||
import java.util.StringTokenizer; | |||
import java.util.Vector; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.DirectoryScanner; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.types.EnumeratedAttribute; | |||
import org.apache.tools.ant.types.FileSet; | |||
/** | |||
* A task to send SMTP email. This is a refactoring of the SendMail and MimeMail | |||
* tasks such that both are within a single task. The functionality of SendMail can | |||
* be found in {@link org.apache.tools.ant.taskdefs.email.PlainMailer PlainMailer}, | |||
* MimeMail in {@link org.apache.tools.ant.taskdefs.email.MimeMailer MimeMailer}, | |||
* and a new intermediate UU format. | |||
* {@link org.apache.tools.ant.taskdefs.email.UUMailer UUMailer}. | |||
* | |||
* @author Magesh Umasankar | |||
* @author glenn_twiggs@bmc.com | |||
* @author steve_l@iseran.com steve loughran | |||
* @author erik@hatcher.net Erik Hatcher | |||
* @author paulo.gaspar@krankikom.de Paulo Gaspar | |||
* @author roxspring@yahoo.com Rob Oxspring | |||
* @created 10 Dec 2001 | |||
* @since 1.5 | |||
*/ | |||
public class EmailTask | |||
extends Task | |||
{ | |||
/** Constant to show that the best available mailer should be used. | |||
*/ | |||
public final static String AUTO = "auto"; | |||
/** Constant to allow the Mime mailer to be requested | |||
*/ | |||
public final static String MIME = "mime"; | |||
/** Constant to allow the UU mailer to be requested | |||
*/ | |||
public final static String UU = "uu"; | |||
/** Constant to allow the plaintext mailer to be requested | |||
*/ | |||
public final static String PLAIN = "plain"; | |||
/** Enumerates the encoding constants | |||
*/ | |||
public static class Encoding extends EnumeratedAttribute | |||
{ | |||
/** finds the valid encoding values | |||
* @return a list of valid entries | |||
*/ | |||
public String[] getValues() | |||
{ | |||
return new String[] | |||
{AUTO, MIME, UU, PLAIN}; | |||
} | |||
} | |||
private String encoding = AUTO; | |||
/** | |||
* host running SMTP | |||
*/ | |||
private String host = "localhost"; | |||
private int port = 25; | |||
/** | |||
* subject field | |||
*/ | |||
private String subject = null; | |||
/** | |||
* any text | |||
*/ | |||
private Message message = null; | |||
/** failure flag */ | |||
private boolean failOnError = true; | |||
private boolean includeFileNames = false; | |||
private boolean simpleMessage = false; | |||
private String messageMimeType = null; | |||
/** | |||
* sender | |||
*/ | |||
private EmailAddress from = null; | |||
/** | |||
* TO recipients | |||
*/ | |||
private Vector toList = new Vector(); | |||
/** | |||
* CC (Carbon Copy) recipients | |||
*/ | |||
private Vector ccList = new Vector(); | |||
/** | |||
* BCC (Blind Carbon Copy) recipients | |||
*/ | |||
private Vector bccList = new Vector(); | |||
/** | |||
* file list | |||
*/ | |||
private Vector files = new Vector(); | |||
private Vector filesets = new Vector(); | |||
/** Allows the build writer to choose the preferred encoding method | |||
* @param encoding The encoding (one of AUTO,MIME,UU,PLAIN) | |||
*/ | |||
public void setEncoding( Encoding encoding ) | |||
{ | |||
this.encoding = encoding.getValue(); | |||
} | |||
/** Sets the mail server port | |||
* @see setPort(int) | |||
* @param port The port to use | |||
*/ | |||
public void setMailport( int port ) | |||
{ | |||
this.port = port; | |||
} | |||
/** Sets the host | |||
* @see setHost() | |||
* @param host The host to connect to | |||
*/ | |||
public void setMailhost( String host ) | |||
{ | |||
this.host = host; | |||
} | |||
/** Sets the subject line of the email | |||
* | |||
* @param subject Subject of this email. | |||
*/ | |||
public void setSubject( String subject ) | |||
{ | |||
this.subject = subject; | |||
} | |||
/** Shorthand method to set the message | |||
* @param message Message body of this email. | |||
*/ | |||
public void setMessage( String message ) | |||
{ | |||
if( this.message != null ) | |||
{ | |||
throw new BuildException( "Only one message can be sent in an email" ); | |||
} | |||
this.message = new Message( message ); | |||
} | |||
/** Shorthand method to set the message from a file | |||
* @param file The file from which to take the message | |||
*/ | |||
public void setMessageFile( File file ) | |||
{ | |||
if( this.message != null ) | |||
{ | |||
throw new BuildException( "Only one message can be sent in an email" ); | |||
} | |||
this.message = new Message( file ); | |||
} | |||
/** Shorthand method to set type of the text message, text/plain by default but text/html | |||
* or text/xml is quite feasible. | |||
* | |||
* @param type The new MessageMimeType value | |||
*/ | |||
public void setMessageMimeType( String type ) | |||
{ | |||
this.messageMimeType = type; | |||
} | |||
/** Add a message elemnt | |||
* @param message The message object | |||
* @throws BuildException if a message has already been added | |||
*/ | |||
public void addMessage( Message message ) | |||
throws BuildException | |||
{ | |||
if( this.message != null ) | |||
{ | |||
throw new BuildException( "Only one message can be sent in an email" ); | |||
} | |||
this.message = message; | |||
} | |||
/** Adds a from address element | |||
* @param address The address to send from | |||
*/ | |||
public void addFrom( EmailAddress address ) | |||
{ | |||
if( this.from != null ) | |||
{ | |||
throw new BuildException( "Emails can only be from one address" ); | |||
} | |||
this.from = address; | |||
} | |||
/** Shorthand to set the from address element | |||
* | |||
* @param address The address to send mail from | |||
*/ | |||
public void setFrom( String address ) | |||
{ | |||
if( this.from != null ) | |||
{ | |||
throw new BuildException( "Emails can only be from one address" ); | |||
} | |||
this.from = new EmailAddress( address ); | |||
} | |||
/** Adds a to address element | |||
* @param address An email address | |||
*/ | |||
public void addTo( EmailAddress address ) | |||
{ | |||
toList.addElement( address ); | |||
} | |||
/** Adds "to" address elements | |||
* | |||
* | |||
* @param list Comma separated list of addresses | |||
*/ | |||
public void setToList( String list ) | |||
{ | |||
StringTokenizer tokens = new StringTokenizer( list, "," ); | |||
while( tokens.hasMoreTokens() ) | |||
{ | |||
toList.addElement( new EmailAddress( tokens.nextToken() ) ); | |||
} | |||
} | |||
/** Adds "cc" address element | |||
* @param address The email address | |||
*/ | |||
public void addCc( EmailAddress address ) | |||
{ | |||
ccList.addElement( address ); | |||
} | |||
/** Adds "cc" address elements | |||
* | |||
* | |||
* @param list Comma separated list of addresses | |||
*/ | |||
public void setCcList( String list ) | |||
{ | |||
StringTokenizer tokens = new StringTokenizer( list, "," ); | |||
while( tokens.hasMoreTokens() ) | |||
{ | |||
ccList.addElement( new EmailAddress( tokens.nextToken() ) ); | |||
} | |||
} | |||
/** Adds "bcc" address elements | |||
* @param address The email address | |||
*/ | |||
public void addBcc( EmailAddress address ) | |||
{ | |||
bccList.addElement( address ); | |||
} | |||
/** Adds "bcc" address elements | |||
* | |||
* | |||
* @param list comma separated list of addresses | |||
*/ | |||
public void setBccList( String list ) | |||
{ | |||
StringTokenizer tokens = new StringTokenizer( list, "," ); | |||
while( tokens.hasMoreTokens() ) | |||
{ | |||
bccList.addElement( new EmailAddress( tokens.nextToken() ) ); | |||
} | |||
} | |||
/** Indicates whether BuildExceptions should be passed back to the core | |||
* | |||
* @param failOnError The new FailOnError value | |||
*/ | |||
public void setFailOnError( boolean failOnError ) | |||
{ | |||
this.failOnError = failOnError; | |||
} | |||
/** Adds a list of files to be attached | |||
* | |||
* @param filenames Comma separated list of files | |||
*/ | |||
public void setFiles( String filenames ) | |||
{ | |||
StringTokenizer t = new StringTokenizer( filenames, ", " ); | |||
while( t.hasMoreTokens() ) | |||
{ | |||
files.addElement( project.resolveFile( t.nextToken() ) ); | |||
} | |||
} | |||
/** Adds a set of files (nested fileset attribute). | |||
* @param fs The fileset | |||
*/ | |||
public void addFileset( FileSet fs ) | |||
{ | |||
filesets.addElement( fs ); | |||
} | |||
/** Sets Includefilenames attribute | |||
* | |||
* @param includeFileNames Whether to include filenames in the text of the message | |||
*/ | |||
public void setIncludefilenames( boolean includeFileNames ) | |||
{ | |||
this.includeFileNames = includeFileNames; | |||
} | |||
/** Identifies whether file names should be included | |||
* @return Identifies whether file names should be included | |||
*/ | |||
public boolean getIncludeFileNames() | |||
{ | |||
return includeFileNames; | |||
} | |||
/** Sends an email | |||
*/ | |||
public void execute() | |||
{ | |||
try | |||
{ | |||
Mailer mailer = null; | |||
// prepare for the auto select mechanism | |||
boolean autoFound = false; | |||
// try MIME format | |||
if( encoding.equals( MIME ) || ( encoding.equals( AUTO ) && !autoFound ) ) | |||
{ | |||
try | |||
{ | |||
mailer = (Mailer)getClass().forName( "org.apache.tools.ant.taskdefs.email.MimeMailer" ).newInstance(); | |||
autoFound = true; | |||
log( "Using MIME mail", Project.MSG_VERBOSE ); | |||
} | |||
catch( Throwable e ) | |||
{ | |||
log( "Failed to initialise MIME mail", Project.MSG_WARN ); | |||
} | |||
} | |||
// try UU format | |||
if( encoding.equals( UU ) || ( encoding.equals( AUTO ) && !autoFound ) ) | |||
{ | |||
try | |||
{ | |||
mailer = (Mailer)getClass().forName( "org.apache.tools.ant.taskdefs.email.UUMailer" ).newInstance(); | |||
autoFound = true; | |||
log( "Using UU mail", Project.MSG_VERBOSE ); | |||
} | |||
catch( Throwable e ) | |||
{ | |||
log( "Failed to initialise UU mail", Project.MSG_WARN ); | |||
} | |||
} | |||
// try plain format | |||
if( encoding.equals( PLAIN ) || ( encoding.equals( AUTO ) && !autoFound ) ) | |||
{ | |||
mailer = new PlainMailer(); | |||
autoFound = true; | |||
log( "Using plain mail", Project.MSG_VERBOSE ); | |||
} | |||
// a valid mailer must be present by now | |||
if( mailer == null ) | |||
{ | |||
throw new BuildException( "Failed to initialise encoding: " + encoding ); | |||
} | |||
// a valid message is required | |||
if( message == null ) | |||
{ | |||
throw new BuildException( "A message is required" ); | |||
} | |||
// an address to send from is required | |||
if( from == null || from.getAddress() == null ) | |||
{ | |||
throw new BuildException( "A from element is required" ); | |||
} | |||
// at least one address to send to/cc/bcc is required | |||
if( toList.isEmpty() && ccList.isEmpty() && bccList.isEmpty() ) | |||
{ | |||
throw new BuildException( "At least one of to,cc or bcc must be supplied" ); | |||
} | |||
// set the mimetype if not done already (and required) | |||
if( messageMimeType != null ) | |||
{ | |||
if( message.isMimeTypeSpecified() ) | |||
{ | |||
throw new BuildException( "The mime type can only be specified in one location" ); | |||
} | |||
else | |||
{ | |||
message.setMimeType( messageMimeType ); | |||
} | |||
} | |||
// identify which files should be attached | |||
Enumeration e = filesets.elements(); | |||
while( e.hasMoreElements() ) | |||
{ | |||
FileSet fs = (FileSet)e.nextElement(); | |||
DirectoryScanner ds = fs.getDirectoryScanner( project ); | |||
String[] includedFiles = ds.getIncludedFiles(); | |||
File baseDir = ds.getBasedir(); | |||
for( int j = 0; j < includedFiles.length; ++j ) | |||
{ | |||
File file = new File( baseDir, includedFiles[ j ] ); | |||
files.addElement( file ); | |||
} | |||
} | |||
// let the user know what's going to happen | |||
log( "Sending email: " + subject, Project.MSG_INFO ); | |||
log( "From " + from, Project.MSG_VERBOSE ); | |||
log( "To " + toList, Project.MSG_VERBOSE ); | |||
log( "Cc " + ccList, Project.MSG_VERBOSE ); | |||
log( "Bcc " + bccList, Project.MSG_VERBOSE ); | |||
// pass the params to the mailer | |||
mailer.setHost( host ); | |||
mailer.setPort( port ); | |||
mailer.setMessage( message ); | |||
mailer.setFrom( from ); | |||
mailer.setToList( toList ); | |||
mailer.setCcList( ccList ); | |||
mailer.setBccList( bccList ); | |||
mailer.setFiles( files ); | |||
mailer.setSubject( subject ); | |||
mailer.setTask( this ); | |||
mailer.setIncludeFileNames( includeFileNames ); | |||
// send the email | |||
mailer.send(); | |||
// let the user know what happened | |||
int count = files.size(); | |||
log( "Sent email with " + count + " attachment" + ( count == 1?"":"s" ), Project.MSG_INFO ); | |||
} | |||
catch( BuildException e ) | |||
{ | |||
log( "Failed to send email", Project.MSG_WARN ); | |||
if( failOnError ) | |||
{ | |||
throw e; | |||
} | |||
} | |||
} | |||
} | |||
@@ -0,0 +1,173 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 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", "Ant", 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 apache@apache.org. | |||
* | |||
* 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.email; | |||
import java.util.Vector; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.Task; | |||
/** | |||
* Base class for the various emailing implementations. | |||
* | |||
* @author roxspring@yahoo.com Rob Oxspring | |||
* @since 1.5 | |||
*/ | |||
public abstract class Mailer | |||
{ | |||
protected String host = null; | |||
protected int port = -1; | |||
protected Message message; | |||
protected EmailAddress from; | |||
protected Vector toList = null; | |||
protected Vector ccList = null; | |||
protected Vector bccList = null; | |||
protected Vector files = null; | |||
protected String subject = null; | |||
protected Task task; | |||
protected boolean includeFileNames = false; | |||
/** sets the mail server | |||
* @param host | |||
*/ | |||
public void setHost( String host ) | |||
{ | |||
this.host = host; | |||
} | |||
/** sets the smtp port | |||
* @param port | |||
*/ | |||
public void setPort( int port ) | |||
{ | |||
this.port = port; | |||
} | |||
/** sets the message | |||
* @param m | |||
*/ | |||
public void setMessage( Message m ) | |||
{ | |||
this.message = m; | |||
} | |||
/** sets the address to send from | |||
* @param from | |||
*/ | |||
public void setFrom( EmailAddress from ) | |||
{ | |||
this.from = from; | |||
} | |||
/** set the to addresses | |||
* @param list | |||
*/ | |||
public void setToList( Vector list ) | |||
{ | |||
this.toList = list; | |||
} | |||
/** sets the cc addresses | |||
* @param list | |||
*/ | |||
public void setCcList( Vector list ) | |||
{ | |||
this.ccList = list; | |||
} | |||
/** sets the bcc addresses | |||
* @param list | |||
*/ | |||
public void setBccList( Vector list ) | |||
{ | |||
this.bccList = list; | |||
} | |||
/** sets the files to attach | |||
* @param files | |||
*/ | |||
public void setFiles( Vector files ) | |||
{ | |||
this.files = files; | |||
} | |||
/** sets the subject | |||
* @param subject | |||
*/ | |||
public void setSubject( String subject ) | |||
{ | |||
this.subject = subject; | |||
} | |||
/** sets the owning task | |||
* @param task | |||
*/ | |||
public void setTask( Task task ) | |||
{ | |||
this.task = task; | |||
} | |||
/** indicates whether filenames should be listed in the body | |||
* @param b | |||
*/ | |||
public void setIncludeFileNames( boolean b ) | |||
{ | |||
this.includeFileNames = b; | |||
} | |||
/** This method should send the email | |||
* @throws BuildException | |||
*/ | |||
public abstract void send() | |||
throws BuildException; | |||
} |
@@ -0,0 +1,168 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 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", "Ant", 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 apache@apache.org. | |||
* | |||
* 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.email; | |||
import java.io.BufferedReader; | |||
import java.io.File; | |||
import java.io.FileReader; | |||
import java.io.IOException; | |||
import java.io.PrintStream; | |||
/** | |||
* Class representing an email message. | |||
* | |||
* @author roxspring@yahoo.com Rob Oxspring | |||
* @since 1.5 | |||
*/ | |||
public class Message | |||
{ | |||
private File messageSource = null; | |||
private StringBuffer buffer = new StringBuffer(); | |||
private String mimeType = "text/plain"; | |||
private boolean specified = false; | |||
/** creates a new empty message | |||
*/ | |||
public Message() | |||
{ | |||
} | |||
/** Creates a new message based on the given string | |||
* @param text the message | |||
*/ | |||
public Message( String text ) | |||
{ | |||
addText( text ); | |||
} | |||
/** Creates a new message using the contents of the given file. | |||
* @param file the source of the message | |||
*/ | |||
public Message( File file ) | |||
{ | |||
messageSource = file; | |||
} | |||
/** Adds a textual part of the message | |||
* @param text some text to add | |||
*/ | |||
public void addText( String text ) | |||
{ | |||
buffer.append( text ); | |||
} | |||
/** Sets the source file of the message | |||
* @param src the source of the message | |||
*/ | |||
public void setSrc( File src ) | |||
{ | |||
this.messageSource = src; | |||
} | |||
/** Sets the content type for the message | |||
* @param mimeType a mime type e.g. "text/plain" | |||
*/ | |||
public void setMimeType( String mimeType ) | |||
{ | |||
this.mimeType = mimeType; | |||
specified = true; | |||
} | |||
/** Returns the content type | |||
* @return the mime type | |||
*/ | |||
public String getMimeType() | |||
{ | |||
return mimeType; | |||
} | |||
/** prints the message onto an output stream | |||
* @param out The print stream to write to | |||
* @throws IOException if an error occurs | |||
*/ | |||
public void print( PrintStream out ) | |||
throws IOException | |||
{ | |||
if( messageSource != null ) | |||
{ | |||
// Read message from a file | |||
FileReader freader = new FileReader( messageSource ); | |||
try | |||
{ | |||
BufferedReader in = new BufferedReader( freader ); | |||
String line = null; | |||
while( ( line = in.readLine() ) != null ) | |||
{ | |||
out.println( line ); | |||
} | |||
} | |||
finally { | |||
freader.close(); | |||
} | |||
} | |||
else | |||
{ | |||
out.println( buffer ); | |||
} | |||
} | |||
/** returns true iff the mimeType has been set. | |||
* @return false if the default value is in use | |||
*/ | |||
public boolean isMimeTypeSpecified() | |||
{ | |||
return specified; | |||
} | |||
} |
@@ -0,0 +1,184 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 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", "Ant", 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 apache@apache.org. | |||
* | |||
* 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.email; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.io.PrintStream; | |||
import java.io.UnsupportedEncodingException; | |||
import java.util.Enumeration; | |||
import java.util.Properties; | |||
import java.util.Vector; | |||
import javax.activation.DataHandler; | |||
import javax.activation.FileDataSource; | |||
import javax.mail.Message; | |||
import javax.mail.MessagingException; | |||
import javax.mail.Session; | |||
import javax.mail.Transport; | |||
import javax.mail.internet.AddressException; | |||
import javax.mail.internet.InternetAddress; | |||
import javax.mail.internet.MimeBodyPart; | |||
import javax.mail.internet.MimeMessage; | |||
import javax.mail.internet.MimeMultipart; | |||
import org.apache.tools.ant.BuildException; | |||
/** | |||
* Uses the JavaMail classes to send Mime format email. | |||
* | |||
* @author roxspring@yahoo.com Rob Oxspring | |||
* @since 1.5 | |||
*/ | |||
public class MimeMailer | |||
extends Mailer | |||
{ | |||
/** Sends the email | |||
*/ | |||
public void send() | |||
{ | |||
try | |||
{ | |||
Properties props = new Properties(); | |||
props.put( "mail.smtp.host", host ); | |||
props.put( "mail.smtp.port", String.valueOf( port ) ); | |||
//Aside, the JDK is clearly unaware of the scottish 'session', which | |||
//involves excessive quantities of alcohol :-) | |||
Session sesh = Session.getDefaultInstance( props, null ); | |||
//create the message | |||
MimeMessage msg = new MimeMessage( sesh ); | |||
MimeMultipart attachments = new MimeMultipart(); | |||
//set the sender | |||
if( from.getName() == null ) | |||
{ | |||
msg.setFrom( new InternetAddress( from.getAddress() ) ); | |||
} | |||
else | |||
{ | |||
msg.setFrom( new InternetAddress( from.getAddress(), from.getName() ) ); | |||
} | |||
msg.setRecipients( Message.RecipientType.TO, internetAddresses( toList ) ); | |||
msg.setRecipients( Message.RecipientType.CC, internetAddresses( ccList ) ); | |||
msg.setRecipients( Message.RecipientType.BCC, internetAddresses( bccList ) ); | |||
if( subject != null ) | |||
{ | |||
msg.setSubject( subject ); | |||
} | |||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |||
PrintStream out = new PrintStream( baos ); | |||
message.print( out ); | |||
out.close(); | |||
MimeBodyPart textbody = new MimeBodyPart(); | |||
textbody.setContent( baos.toString(), message.getMimeType() ); | |||
attachments.addBodyPart( textbody ); | |||
Enumeration e = files.elements(); | |||
while( e.hasMoreElements() ) | |||
{ | |||
File file = (File)e.nextElement(); | |||
MimeBodyPart body; | |||
body = new MimeBodyPart(); | |||
if( !file.exists() || !file.canRead() ) | |||
{ | |||
throw new BuildException( "File \"" + file.getAbsolutePath() | |||
+ "\" does not exist or is not readable." ); | |||
} | |||
FileDataSource fileData = new FileDataSource( file ); | |||
DataHandler fileDataHandler = new DataHandler( fileData ); | |||
body.setDataHandler( fileDataHandler ); | |||
body.setFileName( file.getName() ); | |||
attachments.addBodyPart( body ); | |||
} | |||
msg.setContent( attachments ); | |||
Transport.send( msg ); | |||
} | |||
catch( MessagingException e ) | |||
{ | |||
throw new BuildException( "Problem while sending mime mail:", e ); | |||
} | |||
catch( IOException e ) | |||
{ | |||
throw new BuildException( "Problem while sending mime mail:", e ); | |||
} | |||
} | |||
private static InternetAddress[] internetAddresses( Vector list ) | |||
throws AddressException, UnsupportedEncodingException | |||
{ | |||
InternetAddress addrs[] = new InternetAddress[ list.size() ]; | |||
for( int i = 0; i < list.size(); ++i ) | |||
{ | |||
EmailAddress addr = (EmailAddress)list.elementAt( i ); | |||
if( addr.getName() == null ) | |||
{ | |||
addrs[ i ] = new InternetAddress( addr.getAddress() ); | |||
} | |||
else | |||
{ | |||
addrs[ i ] = new InternetAddress( addr.getAddress(), addr.getName() ); | |||
} | |||
} | |||
return addrs; | |||
} | |||
} |
@@ -0,0 +1,175 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 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", "Ant", 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 apache@apache.org. | |||
* | |||
* 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.email; | |||
import java.io.BufferedInputStream; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
import java.io.IOException; | |||
import java.io.PrintStream; | |||
import java.util.Enumeration; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.mail.MailMessage; | |||
/** | |||
* Class responsible for sending email through raw protocol methods. | |||
* | |||
* @author roxspring@yahoo.com Rob Oxspring | |||
* @since 1.5 | |||
*/ | |||
public class PlainMailer | |||
extends Mailer | |||
{ | |||
/** Sends the email using the apache {@link org.apache.tools.mail.MailMessage MailMessage} class. | |||
*/ | |||
public void send() | |||
{ | |||
try | |||
{ | |||
MailMessage mailMessage = new MailMessage( host ); | |||
mailMessage.setPort( port ); | |||
mailMessage.from( from.toString() ); | |||
Enumeration e; | |||
e = toList.elements(); | |||
while( e.hasMoreElements() ) | |||
{ | |||
mailMessage.to( e.nextElement().toString() ); | |||
} | |||
e = ccList.elements(); | |||
while( e.hasMoreElements() ) | |||
{ | |||
mailMessage.cc( e.nextElement().toString() ); | |||
} | |||
e = bccList.elements(); | |||
while( e.hasMoreElements() ) | |||
{ | |||
mailMessage.bcc( e.nextElement().toString() ); | |||
} | |||
if( subject != null ) | |||
{ | |||
mailMessage.setSubject( subject ); | |||
} | |||
mailMessage.setHeader( "Content-Type", message.getMimeType() ); | |||
PrintStream out = mailMessage.getPrintStream(); | |||
message.print( out ); | |||
e = files.elements(); | |||
while( e.hasMoreElements() ) | |||
{ | |||
File file = (File)e.nextElement(); | |||
attach( file, out ); | |||
} | |||
mailMessage.sendAndClose(); | |||
} | |||
catch( IOException ioe ) | |||
{ | |||
throw new BuildException( "IO error sending mail", ioe ); | |||
} | |||
} | |||
/** Attaches a file to this email | |||
* @param file The file to attache | |||
* @param out The message stream to add to | |||
* @throws IOException if errors occur | |||
*/ | |||
protected void attach( File file, PrintStream out ) | |||
throws IOException | |||
{ | |||
if( !file.exists() || !file.canRead() ) | |||
{ | |||
throw new BuildException( "File \"" + file.getName() | |||
+ "\" does not exist or is not readable." ); | |||
} | |||
if( includeFileNames ) | |||
{ | |||
out.println(); | |||
String filename = file.getName(); | |||
int filenamelength = filename.length(); | |||
out.println( filename ); | |||
for( int star = 0; star < filenamelength; star++ ) | |||
{ | |||
out.print( '=' ); | |||
} | |||
out.println(); | |||
} | |||
int length; | |||
byte[] buf = new byte[ 1024 ]; | |||
FileInputStream finstr = new FileInputStream( file ); | |||
try | |||
{ | |||
BufferedInputStream in = new BufferedInputStream( finstr, buf.length ); | |||
while( ( length = in.read( buf ) ) != -1 ) | |||
{ | |||
out.write( buf, 0, length ); | |||
} | |||
} | |||
finally | |||
{ | |||
finstr.close(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,95 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 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", "Ant", 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 apache@apache.org. | |||
* | |||
* 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.email; | |||
import java.io.BufferedInputStream; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
import java.io.IOException; | |||
import java.io.PrintStream; | |||
import org.apache.tools.ant.BuildException; | |||
import sun.misc.UUEncoder; | |||
/** | |||
* An emailer that uuencodes attachments. | |||
* | |||
* @author roxspring@yahoo.com Rob Oxspring | |||
* @since 1.5 | |||
*/ | |||
class UUMailer | |||
extends PlainMailer | |||
{ | |||
protected void attach( File file, PrintStream out ) | |||
throws IOException | |||
{ | |||
if( !file.exists() || !file.canRead() ) | |||
{ | |||
throw new BuildException( "File \"" + file.getName() | |||
+ "\" does not exist or is not readable." ); | |||
} | |||
FileInputStream finstr = new FileInputStream( file ); | |||
try | |||
{ | |||
BufferedInputStream in = new BufferedInputStream( finstr ); | |||
UUEncoder encoder = new UUEncoder( file.getName() ); | |||
encoder.encode( in, out ); | |||
} | |||
finally | |||
{ | |||
finstr.close(); | |||
} | |||
} | |||
} |
@@ -1,7 +1,7 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
@@ -51,46 +51,10 @@ | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.taskdefs.optional.net; | |||
// 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.Session; | |||
import javax.mail.Message; | |||
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.DataHandler; | |||
import javax.activation.FileDataSource; | |||
// Ant imports | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.DirectoryScanner; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.types.FileSet; | |||
import org.apache.tools.ant.taskdefs.email.EmailTask; | |||
/** | |||
* A task to send SMTP email. This version has near identical syntax to | |||
@@ -98,337 +62,25 @@ import org.apache.tools.ant.types.FileSet; | |||
* activation.jar to compile and execute, which puts it clearly into the | |||
* very optional category. | |||
* | |||
* @deprecated Use {@link EmailTask} instead. | |||
* | |||
* @author glenn_twiggs@bmc.com | |||
* @author steve_l@iseran.com steve loughran | |||
* @author ehatcher@apache.org Erik Hatcher | |||
* @author paulo.gaspar@krankikom.de Paulo Gaspar | |||
* @created 01 May 2001 | |||
*/ | |||
public class MimeMail extends Task { | |||
/** | |||
* failure flag | |||
*/ | |||
private boolean failOnError = true; | |||
/** | |||
* sender | |||
*/ | |||
private String from = null; | |||
/** | |||
* host running SMTP | |||
*/ | |||
private String mailhost = "localhost"; | |||
/** | |||
* any text | |||
*/ | |||
private String message = null; | |||
/** | |||
* message file (mutually exclusive from message) | |||
*/ | |||
private File messageFile = null; | |||
/** | |||
* 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; | |||
/** | |||
* file list | |||
*/ | |||
private Vector filesets = new Vector(); | |||
/** | |||
* type of the text message, plaintext by default but text/html or | |||
* text/xml is quite feasible | |||
*/ | |||
private String messageMimeType = "text/plain"; | |||
/** | |||
* Creates new instance | |||
*/ | |||
public MimeMail() { | |||
} | |||
/** | |||
* Sets the FailOnError attribute of the MimeMail object | |||
* | |||
* @param failOnError The new FailOnError value | |||
*/ | |||
public void setFailOnError(boolean failOnError) { | |||
this.failOnError = failOnError; | |||
} | |||
/** | |||
* Adds a set of files (nested fileset attribute). | |||
*/ | |||
public void addFileset(FileSet set) { | |||
filesets.addElement(set); | |||
} | |||
/** | |||
* Sets the toList parameter of this build task. | |||
* | |||
* @param toList Comma-separated list of email recipient addreses. | |||
*/ | |||
public void setToList(String toList) { | |||
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. | |||
* | |||
* @param from Email address of sender. | |||
*/ | |||
public void setFrom(String from) { | |||
this.from = from; | |||
} | |||
/** | |||
* Sets the mailhost parameter of this build task. | |||
* | |||
* @param mailhost Mail host name. | |||
*/ | |||
public void setMailhost(String mailhost) { | |||
this.mailhost = mailhost; | |||
} | |||
/** | |||
* Sets the message parameter of this build task. | |||
* | |||
* @param message Message body of this email. | |||
*/ | |||
public void setMessage(String message) { | |||
this.message = message; | |||
} | |||
public void setMessageFile(File messageFile) { | |||
this.messageFile = messageFile; | |||
} | |||
public class MimeMail extends EmailTask | |||
{ | |||
/** | |||
* set type of the text message, plaintext by default but text/html | |||
* or text/xml is quite feasible | |||
* Executes this build task. | |||
* | |||
* @param type The new MessageMimeType value | |||
*/ | |||
public void setMessageMimeType(String type) { | |||
this.messageMimeType = type; | |||
} | |||
/** | |||
* Sets the subject parameter of this build task. | |||
* | |||
* @param subject Subject of this email. | |||
*/ | |||
public void setSubject(String subject) { | |||
this.subject = subject; | |||
} | |||
/** | |||
* verify parameters | |||
* | |||
* @throws BuildException if something is invalid | |||
*/ | |||
public void validate() { | |||
if (from == null) { | |||
throw new BuildException("Attribute \"from\" 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) { | |||
throw new BuildException("FileSet, \"message\", or \"messageFile\" is required."); | |||
} | |||
if (message != null && messageFile != null) { | |||
throw new BuildException("Only one of \"message\" or \"messageFile\" may be specified."); | |||
} | |||
} | |||
/** | |||
* Executes this build task. throws org.apache.tools.ant.BuildException | |||
* if there is an error during task execution. | |||
* | |||
* @exception BuildException Description of Exception | |||
* @exception BuildException On error. | |||
*/ | |||
public void execute() | |||
throws BuildException { | |||
try { | |||
validate(); | |||
doMail(); | |||
} | |||
catch (Exception e) { | |||
if (failOnError) { | |||
throw new BuildException(e); | |||
} | |||
else { | |||
String text = e.toString(); | |||
log(text, Project.MSG_ERR); | |||
} | |||
} | |||
} | |||
// 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 | |||
* | |||
* @exception MessagingException Description of Exception | |||
* @exception AddressException Description of Exception | |||
* @exception BuildException Description of Exception | |||
*/ | |||
public void doMail() | |||
throws MessagingException, AddressException, BuildException { | |||
Properties props = new Properties(); | |||
props.put("mail.smtp.host", mailhost); | |||
//Aside, the JDK is clearly unaware of the scottish 'session', which | |||
//involves excessive quantities of alcohol :-) | |||
Session sesh = Session.getDefaultInstance(props, null); | |||
//create the message | |||
MimeMessage msg = new MimeMessage(sesh); | |||
//set the sender | |||
log("message sender: " + from, Project.MSG_VERBOSE); | |||
msg.setFrom(new InternetAddress(from)); | |||
// 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); | |||
} | |||
//now the complex bit; adding multiple mime objects. And guessing | |||
//the file type | |||
MimeMultipart attachments = new MimeMultipart(); | |||
//first a message | |||
if (messageFile != null) { | |||
int size = (int)messageFile.length(); | |||
byte data[] = new byte[size]; | |||
try { | |||
FileInputStream inStream = new FileInputStream(messageFile); | |||
inStream.read(data); | |||
inStream.close(); | |||
message = new String(data); | |||
} catch (IOException e) { | |||
throw new BuildException(e); | |||
} | |||
} | |||
if (message != null) { | |||
MimeBodyPart textbody = new MimeBodyPart(); | |||
textbody.setContent(message, messageMimeType); | |||
attachments.addBodyPart(textbody); | |||
} | |||
for (int i = 0; i < filesets.size(); i++) | |||
{ | |||
FileSet fs = (FileSet) filesets.elementAt(i); | |||
if (fs != null) | |||
{ | |||
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(baseDir, dsfiles[j]); | |||
MimeBodyPart body; | |||
body = new MimeBodyPart(); | |||
if (!file.exists() || !file.canRead()) { | |||
throw new BuildException("File \"" + file.getAbsolutePath() | |||
+ "\" does not exist or is not readable."); | |||
} | |||
log("Attaching " + file.toString()+" - " +file.length()+" bytes", | |||
Project.MSG_VERBOSE); | |||
FileDataSource fileData = new FileDataSource(file); | |||
DataHandler fileDataHandler = new DataHandler(fileData); | |||
body.setDataHandler(fileDataHandler); | |||
body.setFileName(file.getName()); | |||
attachments.addBodyPart(body); | |||
} // for j | |||
} // if (fs != null) | |||
} // for i | |||
msg.setContent(attachments); | |||
log("sending email "); | |||
Transport.send(msg); | |||
throws BuildException | |||
{ | |||
log( "DEPRECATED - The " + getTaskName() + " task is deprecated. Use the mail task instead." ); | |||
super.execute(); | |||
} | |||
} | |||