Fields Summary |
---|
public static final String | AUTOConstant to show that the best available mailer should be used. |
public static final String | MIMEConstant to allow the Mime mailer to be requested |
public static final String | UUConstant to allow the UU mailer to be requested |
public static final String | PLAINConstant to allow the plaintext mailer to be requested |
private String | encoding |
private String | hosthost running SMTP |
private int | port |
private String | subjectsubject field |
private Message | messageany text |
private boolean | failOnErrorfailure flag |
private boolean | includeFileNames |
private String | messageMimeType |
private EmailAddress | fromsender |
private Vector | replyToListreplyto |
private Vector | toListTO recipients |
private Vector | ccListCC (Carbon Copy) recipients |
private Vector | bccListBCC (Blind Carbon Copy) recipients |
private Vector | headersgeneric headers |
private org.apache.tools.ant.types.Path | attachmentsfile list |
private String | charsetCharacter set for MimeMailer |
private String | userUser for SMTP auth |
private String | passwordPassword for SMTP auth |
private boolean | sslindicate if the user wishes SSL-TLS |
Methods Summary |
---|
public void | addBcc(EmailAddress address)Add a "bcc" address element.
bccList.addElement(address);
|
public void | addCc(EmailAddress address)Add a "cc" address element.
ccList.addElement(address);
|
public void | addFileset(org.apache.tools.ant.types.FileSet fs)Add a set of files (nested fileset attribute).
createAttachments().add(fs);
|
public void | addFrom(EmailAddress address)Add a from address element.
if (this.from != null) {
throw new BuildException("Emails can only be from one address");
}
this.from = address;
|
public void | addMessage(Message message)Add a message element.
if (this.message != null) {
throw new BuildException(
"Only one message can be sent in an email");
}
this.message = message;
|
public void | addReplyTo(EmailAddress address)Add a replyto address element.
this.replyToList.add(address);
|
public void | addTo(EmailAddress address)Add a to address element.
toList.addElement(address);
|
public org.apache.tools.ant.types.Path | createAttachments()Creates a Path as container for attachments. Supports any
filesystem resource-collections that way.
if (attachments == null) {
attachments = new Path(getProject());
}
return attachments.createPath();
|
public Header | createHeader()Create a nested header element.
Header h = new Header();
headers.add(h);
return h;
|
public void | execute()Send an email.
Message savedMessage = message;
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) ClasspathUtils.newInstance(
"org.apache.tools.ant.taskdefs.email.MimeMailer",
EmailTask.class.getClassLoader(), Mailer.class);
autoFound = true;
log("Using MIME mail", Project.MSG_VERBOSE);
} catch (BuildException e) {
Throwable t = e.getCause() == null ? e : e.getCause();
log("Failed to initialise MIME mail: " + t.getMessage(),
Project.MSG_WARN);
return;
}
}
// SMTP auth only allowed with MIME mail
if (!autoFound && ((user != null) || (password != null))
&& (encoding.equals(UU) || encoding.equals(PLAIN))) {
throw new BuildException("SMTP auth only possible with MIME mail");
}
// SSL only allowed with MIME mail
if (!autoFound && (ssl)
&& (encoding.equals(UU) || encoding.equals(PLAIN))) {
throw new BuildException("SSL only possible with MIME mail");
}
// try UU format
if (encoding.equals(UU)
|| (encoding.equals(AUTO) && !autoFound)) {
try {
mailer = (Mailer) ClasspathUtils.newInstance(
"org.apache.tools.ant.taskdefs.email.UUMailer",
EmailTask.class.getClassLoader(), Mailer.class);
autoFound = true;
log("Using UU mail", Project.MSG_VERBOSE);
} catch (BuildException e) {
Throwable t = e.getCause() == null ? e : e.getCause();
log("Failed to initialise UU mail: " + t.getMessage(),
Project.MSG_WARN);
return;
}
}
// 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) {
message = new Message();
message.setProject(getProject());
}
// 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");
}
message.setMimeType(messageMimeType);
}
// set the character set if not done already (and required)
if (charset != null) {
if (message.getCharset() != null) {
throw new BuildException("The charset can only be "
+ "specified in one location");
}
message.setCharset(charset);
}
// identify which files should be attached
Vector files = new Vector();
if (attachments != null) {
Iterator iter = attachments.iterator();
while (iter.hasNext()) {
FileResource fr = (FileResource) iter.next();
files.addElement(fr.getFile());
}
}
// let the user know what's going to happen
log("Sending email: " + subject, Project.MSG_INFO);
log("From " + from, Project.MSG_VERBOSE);
log("ReplyTo " + replyToList, 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.setUser(user);
mailer.setPassword(password);
mailer.setSSL(ssl);
mailer.setMessage(message);
mailer.setFrom(from);
mailer.setReplyToList(replyToList);
mailer.setToList(toList);
mailer.setCcList(ccList);
mailer.setBccList(bccList);
mailer.setFiles(files);
mailer.setSubject(subject);
mailer.setTask(this);
mailer.setIncludeFileNames(includeFileNames);
mailer.setHeaders(headers);
// 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) {
Throwable t = e.getCause() == null ? e : e.getCause();
log("Failed to send email: " + t.getMessage(), Project.MSG_WARN);
if (failOnError) {
throw e;
}
} catch (Exception e) {
log("Failed to send email: " + e.getMessage(), Project.MSG_WARN);
if (failOnError) {
throw new BuildException(e);
}
} finally {
message = savedMessage;
}
|
public java.lang.String | getCharset()Returns the character set of mail message.
return charset;
|
public boolean | getIncludeFileNames()Get whether file names should be included.
return includeFileNames;
|
public void | setBccList(java.lang.String list)Shorthand to set the "bcc" address element.
StringTokenizer tokens = new StringTokenizer(list, ",");
while (tokens.hasMoreTokens()) {
bccList.addElement(new EmailAddress(tokens.nextToken()));
}
|
public void | setCcList(java.lang.String list)Shorthand to set the "cc" address element.
StringTokenizer tokens = new StringTokenizer(list, ",");
while (tokens.hasMoreTokens()) {
ccList.addElement(new EmailAddress(tokens.nextToken()));
}
|
public void | setCharset(java.lang.String charset)Sets the character set of mail message.
Will be ignored if mimeType contains ....; Charset=... substring or
encoding is not a mime .
this.charset = charset;
|
public void | setEncoding(org.apache.tools.ant.taskdefs.email.EmailTask$Encoding encoding)Set the preferred encoding method.
this.encoding = encoding.getValue();
|
public void | setFailOnError(boolean failOnError)Set whether BuildExceptions should be passed back to the core.
this.failOnError = failOnError;
|
public void | setFiles(java.lang.String filenames)Set the list of files to be attached.
StringTokenizer t = new StringTokenizer(filenames, ", ");
while (t.hasMoreTokens()) {
createAttachments()
.add(new FileResource(getProject().resolveFile(t.nextToken())));
}
|
public void | setFrom(java.lang.String address)Shorthand to set the from address element.
if (this.from != null) {
throw new BuildException("Emails can only be from one address");
}
this.from = new EmailAddress(address);
|
public void | setIncludefilenames(boolean includeFileNames)Set whether to include filenames.
this.includeFileNames = includeFileNames;
|
public void | setMailhost(java.lang.String host)Set the host.
this.host = host;
|
public void | setMailport(int port)Set the mail server port.
this.port = port;
|
public void | setMessage(java.lang.String message)Shorthand method to set the message.
if (this.message != null) {
throw new BuildException("Only one message can be sent in an "
+ "email");
}
this.message = new Message(message);
this.message.setProject(getProject());
|
public void | setMessageFile(java.io.File file)Shorthand method to set the message from a file.
if (this.message != null) {
throw new BuildException("Only one message can be sent in an "
+ "email");
}
this.message = new Message(file);
this.message.setProject(getProject());
|
public void | setMessageMimeType(java.lang.String type)Shorthand method to set type of the text message, text/plain by default
but text/html or text/xml is quite feasible.
this.messageMimeType = type;
|
public void | setPassword(java.lang.String password)Set the password for SMTP auth; this requires JavaMail.
this.password = password;
|
public void | setReplyTo(java.lang.String address)Shorthand to set the replyto address element.
this.replyToList.add(new EmailAddress(address));
|
public void | setSSL(boolean ssl)Set whether to send data over SSL.
this.ssl = ssl;
|
public void | setSubject(java.lang.String subject)Set the subject line of the email.
this.subject = subject;
|
public void | setToList(java.lang.String list)Shorthand to set the "to" address element.
StringTokenizer tokens = new StringTokenizer(list, ",");
while (tokens.hasMoreTokens()) {
toList.addElement(new EmailAddress(tokens.nextToken()));
}
|
public void | setUser(java.lang.String user)Set the user for SMTP auth; this requires JavaMail.
this.user = user;
|