source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.2 KB
Line 
1/*
2 * Copyright 2002-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
19import java.io.BufferedInputStream;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.IOException;
23import java.io.PrintStream;
24import java.util.Enumeration;
25import org.apache.tools.ant.BuildException;
26import org.apache.tools.mail.MailMessage;
27
28/**
29 * Class responsible for sending email through raw protocol methods.
30 *
31 * @since Ant 1.5
32 */
33class PlainMailer extends Mailer {
34 /**
35 * Sends the email using the apache MailMessage class.
36 *
37 * @see org.apache.tools.mail.MailMessage
38 */
39 public void send() {
40 try {
41 MailMessage mailMessage = new MailMessage(host, port);
42
43 mailMessage.from(from.toString());
44
45 Enumeration e;
46
47 e = replyToList.elements();
48 while (e.hasMoreElements()) {
49 mailMessage.replyto(e.nextElement().toString());
50 }
51
52 e = toList.elements();
53 while (e.hasMoreElements()) {
54 mailMessage.to(e.nextElement().toString());
55 }
56
57 e = ccList.elements();
58 while (e.hasMoreElements()) {
59 mailMessage.cc(e.nextElement().toString());
60 }
61
62 e = bccList.elements();
63 while (e.hasMoreElements()) {
64 mailMessage.bcc(e.nextElement().toString());
65 }
66
67 if (subject != null) {
68 mailMessage.setSubject(subject);
69 }
70
71 mailMessage.setHeader("Date", getDate());
72 if (message.getCharset() != null) {
73 mailMessage.setHeader("Content-Type", message.getMimeType()
74 + "; charset=\"" + message.getCharset() + "\"");
75
76 } else {
77 mailMessage.setHeader("Content-Type", message.getMimeType());
78 }
79 PrintStream out = mailMessage.getPrintStream();
80 message.print(out);
81
82 e = files.elements();
83 while (e.hasMoreElements()) {
84 File file = (File) e.nextElement();
85
86 attach(file, out);
87 }
88
89 mailMessage.sendAndClose();
90 } catch (IOException ioe) {
91 throw new BuildException("IO error sending mail", ioe);
92 }
93
94 }
95
96 /**
97 * Attaches a file to this email
98 *
99 * @param file The file to attache
100 * @param out The message stream to add to
101 * @throws IOException if errors occur
102 */
103 protected void attach(File file, PrintStream out)
104 throws IOException {
105 if (!file.exists() || !file.canRead()) {
106 throw new BuildException("File \"" + file.getName()
107 + "\" does not exist or is not "
108 + "readable.");
109 }
110
111 if (includeFileNames) {
112 out.println();
113
114 String filename = file.getName();
115 int filenamelength = filename.length();
116
117 out.println(filename);
118 for (int star = 0; star < filenamelength; star++) {
119 out.print('=');
120 }
121 out.println();
122 }
123
124 int length;
125 final int maxBuf = 1024;
126 byte[] buf = new byte[maxBuf];
127 FileInputStream finstr = new FileInputStream(file);
128
129 try {
130 BufferedInputStream in = new BufferedInputStream(finstr, buf.length);
131
132 while ((length = in.read(buf)) != -1) {
133 out.write(buf, 0, length);
134 }
135 } finally {
136 finstr.close();
137 }
138 }
139}
140
Note: See TracBrowser for help on using the repository browser.