* throws org.apache.tools.ant.BuildException if there is an error during task
* execution.
*/
public void execute() {
try {
MailMessage mailMessage = new MailMessage(mailhost);
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];
BufferedInputStream in = null;
try {
in = new BufferedInputStream(
new FileInputStream(file), bufsize);
while ((length = in.read(buf, 0, bufsize)) != -1) {
out.write(buf, 0, length);
}
} finally {
if (in != null) {
in.close();
}
}
} 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) {
throw new BuildException("IO error sending mail: " + ioe.getMessage());
}
}