| Method from org.apache.tools.ant.taskdefs.email.EmailTask Detail: |
public void addBcc(EmailAddress address) {
bccList.addElement(address);
}
Add a "bcc" address element. |
public void addCc(EmailAddress address) {
ccList.addElement(address);
}
Add a "cc" address element. |
public void addFileset(FileSet fs) {
createAttachments().add(fs);
}
Add a set of files (nested fileset attribute). |
public void addFrom(EmailAddress address) {
if (this.from != null) {
throw new BuildException("Emails can only be from one address");
}
this.from = address;
}
Add a from address element. |
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;
}
|
public void addReplyTo(EmailAddress address) {
this.replyToList.add(address);
}
Add a replyto address element. |
public void addTo(EmailAddress address) {
toList.addElement(address);
}
Add a to address element. |
public Path createAttachments() {
if (attachments == null) {
attachments = new Path(getProject());
}
return attachments.createPath();
}
Creates a Path as container for attachments. Supports any
filesystem resource-collections that way. |
public Header createHeader() {
Header h = new Header();
headers.add(h);
return h;
}
Create a nested header element. |
public void execute() {
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 {
//check to make sure that activation.jar
//and mail.jar are available - see bug 31969
Class.forName("javax.activation.DataHandler");
Class.forName("javax.mail.internet.MimeMessage");
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) {
logBuildException("Failed to initialise MIME mail: ", e);
}
}
// 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 || starttls)
&& (encoding.equals(UU) || encoding.equals(PLAIN))) {
throw new BuildException("SSL and STARTTLS 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) {
logBuildException("Failed to initialise UU mail: ", e);
}
}
// 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()) {
Resource r = (Resource) iter.next();
files.addElement(((FileProvider) r.as(FileProvider.class))
.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.setEnableStartTLS(starttls);
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);
mailer.setIgnoreInvalidRecipients(ignoreInvalidRecipients);
// 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) {
logBuildException("Failed to send email: ", e);
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 String getCharset() {
return charset;
}
Returns the character set of mail message. |
public boolean getIncludeFileNames() {
return includeFileNames;
}
Get whether file names should be included. |
public void setBccList(String list) {
StringTokenizer tokens = new StringTokenizer(list, ",");
while (tokens.hasMoreTokens()) {
bccList.addElement(new EmailAddress(tokens.nextToken()));
}
}
Shorthand to set the "bcc" address element. |
public void setCcList(String list) {
StringTokenizer tokens = new StringTokenizer(list, ",");
while (tokens.hasMoreTokens()) {
ccList.addElement(new EmailAddress(tokens.nextToken()));
}
}
Shorthand to set the "cc" address element. |
public void setCharset(String charset) {
this.charset = charset;
}
Sets the character set of mail message.
Will be ignored if mimeType contains ....; Charset=... substring or
encoding is not a mime. |
public void setEnableStartTLS(boolean b) {
this.starttls = b;
}
Set whether to allow authentication to switch to a TLS
connection via STARTTLS. |
public void setEncoding(Encoding encoding) {
this.encoding = encoding.getValue();
}
Set the preferred encoding method. |
public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}
Set whether BuildExceptions should be passed back to the core. |
public void setFiles(String filenames) {
StringTokenizer t = new StringTokenizer(filenames, ", ");
while (t.hasMoreTokens()) {
createAttachments()
.add(new FileResource(getProject().resolveFile(t.nextToken())));
}
}
Set the list of files to be attached. |
public void setFrom(String address) {
if (this.from != null) {
throw new BuildException("Emails can only be from one address");
}
this.from = new EmailAddress(address);
}
Shorthand to set the from address element. |
public void setIgnoreInvalidRecipients(boolean b) {
ignoreInvalidRecipients = b;
}
Whether invalid recipients should be ignored (but a warning
will be logged) instead of making the task fail.
Even with this property set to true the task will still fail
if the mail couldn't be sent to any recipient at all. |
public void setIncludefilenames(boolean includeFileNames) {
this.includeFileNames = includeFileNames;
}
Set whether to include filenames. |
public void setMailhost(String host) {
this.host = host;
}
|
public void setMailport(int port) {
this.port = port;
}
Set the mail server port. |
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);
this.message.setProject(getProject());
}
Shorthand method to set 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);
this.message.setProject(getProject());
}
Shorthand method to set the message from a file. |
public void setMessageMimeType(String type) {
this.messageMimeType = type;
}
Shorthand method to set type of the text message, text/plain by default
but text/html or text/xml is quite feasible. |
public void setPassword(String password) {
this.password = password;
}
Set the password for SMTP auth; this requires JavaMail. |
public void setReplyTo(String address) {
this.replyToList.add(new EmailAddress(address));
}
Shorthand to set the replyto address element. |
public void setSSL(boolean ssl) {
this.ssl = ssl;
}
Set whether to send data over SSL. |
public void setSubject(String subject) {
this.subject = subject;
}
Set the subject line of the email. |
public void setToList(String list) {
StringTokenizer tokens = new StringTokenizer(list, ",");
while (tokens.hasMoreTokens()) {
toList.addElement(new EmailAddress(tokens.nextToken()));
}
}
Shorthand to set the "to" address element. |
public void setUser(String user) {
this.user = user;
}
Set the user for SMTP auth; this requires JavaMail. |