📄 emailtask.java
字号:
} /** * Shorthand to set the "cc" address element. * * @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())); } } /** * Add a "bcc" address element. * * @param address The email address. */ public void addBcc(EmailAddress address) { bccList.addElement(address); } /** * Shorthand to set the "bcc" address element. * * @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())); } } /** * Set whether BuildExceptions should be passed back to the core. * * @param failOnError The new FailOnError value. */ public void setFailOnError(boolean failOnError) { this.failOnError = failOnError; } /** * Set the 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()) { createAttachments() .add(new FileResource(getProject().resolveFile(t.nextToken()))); } } /** * Add a set of files (nested fileset attribute). * * @param fs The fileset. */ public void addFileset(FileSet fs) { createAttachments().add(fs); } /** * Creates a Path as container for attachments. Supports any * filesystem resource-collections that way. * @return the path to be configured. * @since Ant 1.7 */ public Path createAttachments() { if (attachments == null) { attachments = new Path(getProject()); } return attachments.createPath(); } /** * Create a nested header element. * @return a Header instance. */ public Header createHeader() { Header h = new Header(); headers.add(h); return h; } /** * Set whether to include filenames. * * @param includeFileNames Whether to include filenames in the text of the * message. */ public void setIncludefilenames(boolean includeFileNames) { this.includeFileNames = includeFileNames; } /** * Get whether file names should be included. * * @return Identifies whether file names should be included. */ public boolean getIncludeFileNames() { return includeFileNames; } /** * Send an email. */ 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 { 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); 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) { logBuildException("Failed to initialise UU mail: ", e); 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) { 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; } } private void logBuildException(String reason, BuildException e) { Throwable t = e.getCause() == null ? e : e.getCause(); log(reason + t.getMessage(), Project.MSG_WARN); } /** * Sets the character set of mail message. * Will be ignored if mimeType contains ....; Charset=... substring or * encoding is not a <code>mime</code>. * @param charset the character encoding to use. * @since Ant 1.6 */ public void setCharset(String charset) { this.charset = charset; } /** * Returns the character set of mail message. * * @return Charset of mail message. * @since Ant 1.6 */ public String getCharset() { return charset; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -