1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 package org.apache.tools.ant.taskdefs.email;
19
20 import java.io.BufferedInputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.PrintStream;
25 import java.util.Enumeration;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.mail.MailMessage;
29
30 /**
31 * Class responsible for sending email through raw protocol methods.
32 *
33 * @since Ant 1.5
34 */
35 class PlainMailer extends Mailer {
36 /**
37 * Sends the email using the apache MailMessage class.
38 *
39 * @see org.apache.tools.mail.MailMessage
40 */
41 public void send() {
42 try {
43 MailMessage mailMessage = new MailMessage(host, port);
44
45 mailMessage.from(from.toString());
46
47 Enumeration e;
48 boolean atLeastOneRcptReached = false;
49
50 e = replyToList.elements();
51 while (e.hasMoreElements()) {
52 mailMessage.replyto(e.nextElement().toString());
53 }
54 e = toList.elements();
55 while (e.hasMoreElements()) {
56 String to = e.nextElement().toString();
57 try {
58 mailMessage.to(to);
59 atLeastOneRcptReached = true;
60 } catch (IOException ex) {
61 badRecipient(to, ex);
62 }
63 }
64 e = ccList.elements();
65 while (e.hasMoreElements()) {
66 String to = e.nextElement().toString();
67 try {
68 mailMessage.cc(to);
69 atLeastOneRcptReached = true;
70 } catch (IOException ex) {
71 badRecipient(to, ex);
72 }
73 }
74 e = bccList.elements();
75 while (e.hasMoreElements()) {
76 String to = e.nextElement().toString();
77 try {
78 mailMessage.bcc(to);
79 atLeastOneRcptReached = true;
80 } catch (IOException ex) {
81 badRecipient(to, ex);
82 }
83 }
84 if (!atLeastOneRcptReached) {
85 throw new BuildException("Couldn't reach any recipient");
86 }
87 if (subject != null) {
88 mailMessage.setSubject(subject);
89 }
90 mailMessage.setHeader("Date", getDate());
91 if (message.getCharset() != null) {
92 mailMessage.setHeader("Content-Type", message.getMimeType()
93 + "; charset=\"" + message.getCharset() + "\"");
94 } else {
95 mailMessage.setHeader("Content-Type", message.getMimeType());
96 }
97 if (headers != null) {
98 e = headers.elements();
99 while (e.hasMoreElements()) {
100 Header h = (Header) e.nextElement();
101 mailMessage.setHeader(h.getName(), h.getValue());
102 }
103 }
104 PrintStream out = mailMessage.getPrintStream();
105 message.print(out);
106
107 e = files.elements();
108 while (e.hasMoreElements()) {
109 attach((File) e.nextElement(), out);
110 }
111 mailMessage.sendAndClose();
112 } catch (IOException ioe) {
113 throw new BuildException("IO error sending mail", ioe);
114 }
115
116 }
117
118 /**
119 * Attaches a file to this email
120 *
121 * @param file The file to attache
122 * @param out The message stream to add to
123 * @throws IOException if errors occur
124 */
125 protected void attach(File file, PrintStream out)
126 throws IOException {
127 if (!file.exists() || !file.canRead()) {
128 throw new BuildException("File \"" + file.getName()
129 + "\" does not exist or is not "
130 + "readable.");
131 }
132
133 if (includeFileNames) {
134 out.println();
135
136 String filename = file.getName();
137 int filenamelength = filename.length();
138
139 out.println(filename);
140 for (int star = 0; star < filenamelength; star++) {
141 out.print('=');
142 }
143 out.println();
144 }
145
146 int length;
147 final int maxBuf = 1024;
148 byte[] buf = new byte[maxBuf];
149 FileInputStream finstr = new FileInputStream(file);
150
151 try {
152 BufferedInputStream in = new BufferedInputStream(finstr, buf.length);
153
154 while ((length = in.read(buf)) != -1) {
155 out.write(buf, 0, length);
156 }
157 } finally {
158 finstr.close();
159 }
160 }
161
162 private void badRecipient(String rcpt, IOException reason) {
163 String msg = "Failed to send mail to " + rcpt;
164 if (shouldIgnoreInvalidRecipients()) {
165 msg += " because of :" + reason.getMessage();
166 if (task != null) {
167 task.log(msg, Project.MSG_WARN);
168 } else {
169 System.err.println(msg);
170 }
171 } else {
172 throw new BuildException(msg, reason);
173 }
174 }
175 }
176