in src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java [443:594]
public void execute() {
Message savedMessage = message;
try {
Mailer mailer = null;
// prepare for the auto select mechanism
boolean autoFound = false;
// try MIME format
if (MIME.equals(encoding) || AUTO.equals(encoding)) {
try {
mailer = ClasspathUtils.newInstance(getMailerImplementation(),
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))
&& (UU.equals(encoding) || PLAIN.equals(encoding))) {
throw new BuildException("SMTP auth only possible with MIME mail");
}
// SSL only allowed with MIME mail
if (!autoFound && (ssl || starttls)
&& (UU.equals(encoding) || PLAIN.equals(encoding))) {
throw new BuildException(
"SSL and STARTTLS only possible with MIME mail");
}
// try UU format
if (UU.equals(encoding)
|| (AUTO.equals(encoding) && !autoFound)) {
try {
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 (PLAIN.equals(encoding)
|| (AUTO.equals(encoding) && !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: %s",
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);
}
message.setInputEncoding(messageFileInputEncoding);
// identify which files should be attached
Vector<File> files = new Vector<>();
if (attachments != null) {
for (Resource r : attachments) {
files.add(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);
if (port != null) {
mailer.setPort(port);
mailer.setPortExplicitlySpecified(true);
} else {
mailer.setPort(SMTP_PORT);
mailer.setPortExplicitlySpecified(false);
}
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;
}
}