📄 emailtask.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs.email;import java.io.File;import java.util.Iterator;import java.util.StringTokenizer;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.types.EnumeratedAttribute;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.resources.FileResource;import org.apache.tools.ant.util.ClasspathUtils;/** * A task to send SMTP email. This is a refactoring of the SendMail and * MimeMail tasks such that both are within a single task. * * @since Ant 1.5 * @ant.task name="mail" category="network" */public class EmailTask extends Task { private static final int SMTP_PORT = 25; /** Constant to show that the best available mailer should be used. */ public static final String AUTO = "auto"; /** Constant to allow the Mime mailer to be requested */ public static final String MIME = "mime"; /** Constant to allow the UU mailer to be requested */ public static final String UU = "uu"; /** Constant to allow the plaintext mailer to be requested */ public static final String PLAIN = "plain"; /** * Enumerates the encoding constants. */ public static class Encoding extends EnumeratedAttribute { /** * finds the valid encoding values * * @return a list of valid entries */ public String[] getValues() { return new String[] {AUTO, MIME, UU, PLAIN}; } } private String encoding = AUTO; /** host running SMTP */ private String host = "localhost"; private int port = SMTP_PORT; /** subject field */ private String subject = null; /** any text */ private Message message = null; /** failure flag */ private boolean failOnError = true; private boolean includeFileNames = false; private String messageMimeType = null; /* special headers */ /** sender */ private EmailAddress from = null; /** replyto */ private Vector replyToList = new Vector(); /** TO recipients */ private Vector toList = new Vector(); /** CC (Carbon Copy) recipients */ private Vector ccList = new Vector(); /** BCC (Blind Carbon Copy) recipients */ private Vector bccList = new Vector(); /** generic headers */ private Vector headers = new Vector(); /** file list */ private Path attachments = null; /** Character set for MimeMailer*/ private String charset = null; /** User for SMTP auth */ private String user = null; /** Password for SMTP auth */ private String password = null; /** indicate if the user wishes SSL-TLS */ private boolean ssl = false; /** * Set the user for SMTP auth; this requires JavaMail. * @param user the String username. * @since Ant 1.6 */ public void setUser(String user) { this.user = user; } /** * Set the password for SMTP auth; this requires JavaMail. * @param password the String password. * @since Ant 1.6 */ public void setPassword(String password) { this.password = password; } /** * Set whether to send data over SSL. * @param ssl boolean; if true SSL will be used. * @since Ant 1.6 */ public void setSSL(boolean ssl) { this.ssl = ssl; } /** * Set the preferred encoding method. * * @param encoding The encoding (one of AUTO, MIME, UU, PLAIN). */ public void setEncoding(Encoding encoding) { this.encoding = encoding.getValue(); } /** * Set the mail server port. * * @param port The port to use. */ public void setMailport(int port) { this.port = port; } /** * Set the host. * * @param host The host to connect to. */ public void setMailhost(String host) { this.host = host; } /** * Set the subject line of the email. * * @param subject Subject of this email. */ public void setSubject(String subject) { this.subject = subject; } /** * Shorthand method to set the message. * * @param message Message body of this email. */ 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 from a file. * * @param file The file from which to take 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 type of the text message, text/plain by default * but text/html or text/xml is quite feasible. * * @param type The new MessageMimeType value. */ public void setMessageMimeType(String type) { this.messageMimeType = type; } /** * Add a message element. * * @param message The message object. * @throws BuildException if a message has already been added. */ 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; } /** * Add a from address element. * * @param address The address to send from. */ public void addFrom(EmailAddress address) { if (this.from != null) { throw new BuildException("Emails can only be from one address"); } this.from = address; } /** * Shorthand to set the from address element. * * @param address The address to send mail from. */ public void setFrom(String address) { if (this.from != null) { throw new BuildException("Emails can only be from one address"); } this.from = new EmailAddress(address); } /** * Add a replyto address element. * * @param address The address to reply to. * @since Ant 1.6 */ public void addReplyTo(EmailAddress address) { this.replyToList.add(address); } /** * Shorthand to set the replyto address element. * * @param address The address to which replies should be directed. * @since Ant 1.6 */ public void setReplyTo(String address) { this.replyToList.add(new EmailAddress(address)); } /** * Add a to address element. * * @param address An email address. */ public void addTo(EmailAddress address) { toList.addElement(address); } /** * Shorthand to set the "to" address element. * * @param list Comma-separated list of addresses. */ public void setToList(String list) { StringTokenizer tokens = new StringTokenizer(list, ","); while (tokens.hasMoreTokens()) { toList.addElement(new EmailAddress(tokens.nextToken())); } } /** * Add a "cc" address element. * * @param address The email address. */ public void addCc(EmailAddress address) { ccList.addElement(address);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -