source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 17.3 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs.email;
18
19// Ant imports
20
21import java.io.File;
22import java.util.Enumeration;
23import java.util.StringTokenizer;
24import java.util.Vector;
25import org.apache.tools.ant.BuildException;
26import org.apache.tools.ant.DirectoryScanner;
27import org.apache.tools.ant.Project;
28import org.apache.tools.ant.Task;
29import org.apache.tools.ant.types.EnumeratedAttribute;
30import org.apache.tools.ant.types.FileSet;
31
32/**
33 * A task to send SMTP email. This is a refactoring of the SendMail and
34 * MimeMail tasks such that both are within a single task.
35 *
36 * @since Ant 1.5
37 * @ant.task name="mail" category="network"
38 */
39public class EmailTask
40 extends Task {
41 /** Constant to show that the best available mailer should be used. */
42 public static final String AUTO = "auto";
43 /** Constant to allow the Mime mailer to be requested */
44 public static final String MIME = "mime";
45 /** Constant to allow the UU mailer to be requested */
46 public static final String UU = "uu";
47 /** Constant to allow the plaintext mailer to be requested */
48 public static final String PLAIN = "plain";
49
50
51 /**
52 * Enumerates the encoding constants
53 */
54 public static class Encoding extends EnumeratedAttribute {
55 /**
56 * finds the valid encoding values
57 *
58 * @return a list of valid entries
59 */
60 public String[] getValues() {
61 return new String[] {AUTO, MIME, UU, PLAIN};
62 }
63 }
64
65 private String encoding = AUTO;
66 /** host running SMTP */
67 private String host = "localhost";
68 private int port = 25;
69 /** subject field */
70 private String subject = null;
71 /** any text */
72 private Message message = null;
73 /** failure flag */
74 private boolean failOnError = true;
75 private boolean includeFileNames = false;
76 private String messageMimeType = null;
77 /** special headers */
78 /** sender */
79 private EmailAddress from = null;
80 /** replyto */
81 private Vector replyToList = new Vector();
82 /** TO recipients */
83 private Vector toList = new Vector();
84 /** CC (Carbon Copy) recipients */
85 private Vector ccList = new Vector();
86 /** BCC (Blind Carbon Copy) recipients */
87 private Vector bccList = new Vector();
88
89 /** file list */
90 private Vector files = new Vector();
91 private Vector filesets = new Vector();
92 /** Character set for MimeMailer*/
93 private String charset = null;
94 /** User for SMTP auth */
95 private String user = null;
96 /** Password for SMTP auth */
97 private String password = null;
98 /** indicate if the user wishes SSL-TLS */
99 private boolean SSL = false;
100
101 /**
102 * sets the user for SMTP auth; this requires JavaMail
103 * @param user
104 * @since ant 1.6
105 */
106 public void setUser(String user) {
107 this.user = user;
108 }
109
110 /**
111 * sets the password for SMTP auth; this requires JavaMail
112 * @param password
113 * @since ant 1.6
114 */
115 public void setPassword(String password) {
116 this.password = password;
117 }
118
119 /**
120 * tells if the user needs to send his data over SSL
121 * @param SSL
122 * @since ant 1.6
123 */
124 public void setSSL(boolean SSL) {
125 this.SSL = SSL;
126 }
127
128 /**
129 * Allows the build writer to choose the preferred encoding method
130 *
131 * @param encoding The encoding (one of AUTO,MIME,UU,PLAIN)
132 */
133 public void setEncoding(Encoding encoding) {
134 this.encoding = encoding.getValue();
135 }
136
137
138 /**
139 * Sets the mail server port
140 *
141 * @param port The port to use
142 */
143 public void setMailport(int port) {
144 this.port = port;
145 }
146
147
148 /**
149 * Sets the host
150 *
151 * @param host The host to connect to
152 */
153 public void setMailhost(String host) {
154 this.host = host;
155 }
156
157
158 /**
159 * Sets the subject line of the email
160 *
161 * @param subject Subject of this email.
162 */
163 public void setSubject(String subject) {
164 this.subject = subject;
165 }
166
167
168 /**
169 * Shorthand method to set the message
170 *
171 * @param message Message body of this email.
172 */
173 public void setMessage(String message) {
174 if (this.message != null) {
175 throw new BuildException("Only one message can be sent in an "
176 + "email");
177 }
178
179 this.message = new Message(message);
180 this.message.setProject(getProject());
181 }
182
183
184 /**
185 * Shorthand method to set the message from a file
186 *
187 * @param file The file from which to take the message
188 */
189 public void setMessageFile(File file) {
190 if (this.message != null) {
191 throw new BuildException("Only one message can be sent in an "
192 + "email");
193 }
194
195 this.message = new Message(file);
196 this.message.setProject(getProject());
197 }
198
199
200 /**
201 * Shorthand method to set type of the text message, text/plain by default
202 * but text/html or text/xml is quite feasible.
203 *
204 * @param type The new MessageMimeType value
205 */
206 public void setMessageMimeType(String type) {
207 this.messageMimeType = type;
208 }
209
210
211 /**
212 * Add a message element
213 *
214 * @param message The message object
215 * @throws BuildException if a message has already been added
216 */
217 public void addMessage(Message message)
218 throws BuildException {
219 if (this.message != null) {
220 throw new BuildException("Only one message can be sent in an "
221 + "email");
222 }
223
224 this.message = message;
225 }
226
227
228 /**
229 * Adds a from address element
230 *
231 * @param address The address to send from
232 */
233 public void addFrom(EmailAddress address) {
234 if (this.from != null) {
235 throw new BuildException("Emails can only be from one address");
236 }
237
238 this.from = address;
239 }
240
241
242 /**
243 * Shorthand to set the from address element
244 *
245 * @param address The address to send mail from
246 */
247 public void setFrom(String address) {
248 if (this.from != null) {
249 throw new BuildException("Emails can only be from one address");
250 }
251
252 this.from = new EmailAddress(address);
253 }
254
255
256 /**
257 * Adds a replyto address element
258 *
259 * @param address The address to reply to
260 * @since ant 1.6
261 */
262 public void addReplyTo(EmailAddress address) {
263 this.replyToList.add(address);
264 }
265
266
267 /**
268 * Shorthand to set the replyto address element
269 *
270 * @param address The address to which replies should be directed
271 * @since ant 1.6
272 */
273 public void setReplyTo(String address) {
274 this.replyToList.add(new EmailAddress(address));
275 }
276
277
278 /**
279 * Adds a to address element
280 *
281 * @param address An email address
282 */
283 public void addTo(EmailAddress address) {
284 toList.addElement(address);
285 }
286
287
288 /**
289 * Adds "to" address elements
290 *
291 * @param list Comma separated list of addresses
292 */
293 public void setToList(String list) {
294 StringTokenizer tokens = new StringTokenizer(list, ",");
295
296 while (tokens.hasMoreTokens()) {
297 toList.addElement(new EmailAddress(tokens.nextToken()));
298 }
299 }
300
301
302 /**
303 * Adds "cc" address element
304 *
305 * @param address The email address
306 */
307 public void addCc(EmailAddress address) {
308 ccList.addElement(address);
309 }
310
311
312 /**
313 * Adds "cc" address elements
314 *
315 * @param list Comma separated list of addresses
316 */
317 public void setCcList(String list) {
318 StringTokenizer tokens = new StringTokenizer(list, ",");
319
320 while (tokens.hasMoreTokens()) {
321 ccList.addElement(new EmailAddress(tokens.nextToken()));
322 }
323 }
324
325
326 /**
327 * Adds "bcc" address elements
328 *
329 * @param address The email address
330 */
331 public void addBcc(EmailAddress address) {
332 bccList.addElement(address);
333 }
334
335
336 /**
337 * Adds "bcc" address elements
338 *
339 * @param list comma separated list of addresses
340 */
341 public void setBccList(String list) {
342 StringTokenizer tokens = new StringTokenizer(list, ",");
343
344 while (tokens.hasMoreTokens()) {
345 bccList.addElement(new EmailAddress(tokens.nextToken()));
346 }
347 }
348
349
350 /**
351 * Indicates whether BuildExceptions should be passed back to the core
352 *
353 * @param failOnError The new FailOnError value
354 */
355 public void setFailOnError(boolean failOnError) {
356 this.failOnError = failOnError;
357 }
358
359
360 /**
361 * Adds a list of files to be attached
362 *
363 * @param filenames Comma separated list of files
364 */
365 public void setFiles(String filenames) {
366 StringTokenizer t = new StringTokenizer(filenames, ", ");
367
368 while (t.hasMoreTokens()) {
369 files.addElement(getProject().resolveFile(t.nextToken()));
370 }
371 }
372
373
374 /**
375 * Adds a set of files (nested fileset attribute).
376 *
377 * @param fs The fileset
378 */
379 public void addFileset(FileSet fs) {
380 filesets.addElement(fs);
381 }
382
383
384 /**
385 * Sets Includefilenames attribute
386 *
387 * @param includeFileNames Whether to include filenames in the text of the
388 * message
389 */
390 public void setIncludefilenames(boolean includeFileNames) {
391 this.includeFileNames = includeFileNames;
392 }
393
394
395 /**
396 * Identifies whether file names should be included
397 *
398 * @return Identifies whether file names should be included
399 */
400 public boolean getIncludeFileNames() {
401 return includeFileNames;
402 }
403
404
405 /** Sends an email */
406 public void execute() {
407 Message savedMessage = message;
408 Vector savedFiles = (Vector) files.clone();
409
410 try {
411 Mailer mailer = null;
412
413 // prepare for the auto select mechanism
414 boolean autoFound = false;
415 // try MIME format
416 if (encoding.equals(MIME)
417 || (encoding.equals(AUTO) && !autoFound)) {
418 try {
419 mailer =
420 (Mailer) Class.forName("org.apache.tools.ant.taskdefs.email.MimeMailer")
421 .newInstance();
422 autoFound = true;
423 log("Using MIME mail", Project.MSG_VERBOSE);
424 } catch (Throwable e) {
425 log("Failed to initialise MIME mail: "
426 + e.getMessage(), Project.MSG_WARN);
427 }
428 }
429 // SMTP auth only allowed with MIME mail
430 if (autoFound == false && ((user != null) || (password != null))
431 && (encoding.equals(UU) || encoding.equals(PLAIN))) {
432 throw new BuildException("SMTP auth only possible with MIME mail");
433 }
434 // SSL only allowed with MIME mail
435 if (autoFound == false && (SSL)
436 && (encoding.equals(UU) || encoding.equals(PLAIN))) {
437 throw new BuildException("SSL only possible with MIME mail");
438 }
439
440
441 // try UU format
442 if (encoding.equals(UU)
443 || (encoding.equals(AUTO) && !autoFound)) {
444 try {
445 mailer =
446 (Mailer) Class.forName("org.apache.tools.ant.taskdefs.email.UUMailer")
447 .newInstance();
448 autoFound = true;
449 log("Using UU mail", Project.MSG_VERBOSE);
450 } catch (Throwable e) {
451 log("Failed to initialise UU mail", Project.MSG_WARN);
452 }
453 }
454
455 // try plain format
456 if (encoding.equals(PLAIN)
457 || (encoding.equals(AUTO) && !autoFound)) {
458 mailer = new PlainMailer();
459 autoFound = true;
460 log("Using plain mail", Project.MSG_VERBOSE);
461 }
462
463 // a valid mailer must be present by now
464 if (mailer == null) {
465 throw new BuildException("Failed to initialise encoding: "
466 + encoding);
467 }
468
469 // a valid message is required
470 if (message == null) {
471 message = new Message();
472 message.setProject(getProject());
473 }
474
475 // an address to send from is required
476 if (from == null || from.getAddress() == null) {
477 throw new BuildException("A from element is required");
478 }
479
480 // at least one address to send to/cc/bcc is required
481 if (toList.isEmpty() && ccList.isEmpty() && bccList.isEmpty()) {
482 throw new BuildException("At least one of to,cc or bcc must "
483 + "be supplied");
484 }
485
486 // set the mimetype if not done already (and required)
487 if (messageMimeType != null) {
488 if (message.isMimeTypeSpecified()) {
489 throw new BuildException("The mime type can only be "
490 + "specified in one location");
491 } else {
492 message.setMimeType(messageMimeType);
493 }
494 }
495 // set the character set if not done already (and required)
496 if (charset != null) {
497 if (message.getCharset() != null) {
498 throw new BuildException("The charset can only be "
499 + "specified in one location");
500 } else {
501 message.setCharset(charset);
502 }
503 }
504
505 // identify which files should be attached
506 Enumeration e = filesets.elements();
507
508 while (e.hasMoreElements()) {
509 FileSet fs = (FileSet) e.nextElement();
510
511 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
512 String[] includedFiles = ds.getIncludedFiles();
513 File baseDir = ds.getBasedir();
514
515 for (int j = 0; j < includedFiles.length; ++j) {
516 File file = new File(baseDir, includedFiles[j]);
517
518 files.addElement(file);
519 }
520 }
521
522 // let the user know what's going to happen
523 log("Sending email: " + subject, Project.MSG_INFO);
524 log("From " + from, Project.MSG_VERBOSE);
525 log("ReplyTo " + replyToList, Project.MSG_VERBOSE);
526 log("To " + toList, Project.MSG_VERBOSE);
527 log("Cc " + ccList, Project.MSG_VERBOSE);
528 log("Bcc " + bccList, Project.MSG_VERBOSE);
529
530 // pass the params to the mailer
531 mailer.setHost(host);
532 mailer.setPort(port);
533 mailer.setUser(user);
534 mailer.setPassword(password);
535 mailer.setSSL(SSL);
536 mailer.setMessage(message);
537 mailer.setFrom(from);
538 mailer.setReplyToList(replyToList);
539 mailer.setToList(toList);
540 mailer.setCcList(ccList);
541 mailer.setBccList(bccList);
542 mailer.setFiles(files);
543 mailer.setSubject(subject);
544 mailer.setTask(this);
545 mailer.setIncludeFileNames(includeFileNames);
546
547 // send the email
548 mailer.send();
549
550 // let the user know what happened
551 int count = files.size();
552
553 log("Sent email with " + count + " attachment"
554 + (count == 1 ? "" : "s"), Project.MSG_INFO);
555 } catch (BuildException e) {
556 log("Failed to send email", Project.MSG_WARN);
557 if (failOnError) {
558 throw e;
559 }
560 } catch (Exception e) {
561 log("Failed to send email", Project.MSG_WARN);
562 if (failOnError) {
563 throw new BuildException(e);
564 }
565 } finally {
566 message = savedMessage;
567 files = savedFiles;
568 }
569 }
570 /**
571 * Sets the character set of mail message.
572 * Will be ignored if mimeType contains ....; Charset=... substring or
573 * encoding is not a <code>mime</code>
574 * @since Ant 1.6
575 */
576 public void setCharset(String charset) {
577 this.charset = charset;
578 }
579 /**
580 * Returns the character set of mail message.
581 *
582 * @return Charset of mail message.
583 * @since Ant 1.6
584 */
585 public String getCharset() {
586 return charset;
587 }
588}
589
Note: See TracBrowser for help on using the repository browser.