KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > email > PlainMailer


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 JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.mail.MailMessage;
28
29 /**
30  * Class responsible for sending email through raw protocol methods.
31  *
32  * @since Ant 1.5
33  */

34 class PlainMailer extends Mailer {
35     /**
36      * Sends the email using the apache MailMessage class.
37      *
38      * @see org.apache.tools.mail.MailMessage
39      */

40     public void send() {
41         try {
42             MailMessage mailMessage = new MailMessage(host, port);
43
44             mailMessage.from(from.toString());
45
46             Enumeration JavaDoc e;
47
48             e = replyToList.elements();
49             while (e.hasMoreElements()) {
50                 mailMessage.replyto(e.nextElement().toString());
51             }
52             e = toList.elements();
53             while (e.hasMoreElements()) {
54                 mailMessage.to(e.nextElement().toString());
55             }
56             e = ccList.elements();
57             while (e.hasMoreElements()) {
58                 mailMessage.cc(e.nextElement().toString());
59             }
60             e = bccList.elements();
61             while (e.hasMoreElements()) {
62                 mailMessage.bcc(e.nextElement().toString());
63             }
64             if (subject != null) {
65                 mailMessage.setSubject(subject);
66             }
67             mailMessage.setHeader("Date", getDate());
68             if (message.getCharset() != null) {
69                 mailMessage.setHeader("Content-Type", message.getMimeType()
70                     + "; charset=\"" + message.getCharset() + "\"");
71             } else {
72                 mailMessage.setHeader("Content-Type", message.getMimeType());
73             }
74             e = headers.elements();
75             while (e.hasMoreElements()) {
76                 Header h = (Header) e.nextElement();
77                 mailMessage.setHeader(h.getName(), h.getValue());
78             }
79             PrintStream JavaDoc out = mailMessage.getPrintStream();
80             message.print(out);
81
82             e = files.elements();
83             while (e.hasMoreElements()) {
84                 attach((File JavaDoc) e.nextElement(), out);
85             }
86             mailMessage.sendAndClose();
87         } catch (IOException JavaDoc ioe) {
88             throw new BuildException("IO error sending mail", ioe);
89         }
90
91     }
92
93     /**
94      * Attaches a file to this email
95      *
96      * @param file The file to attache
97      * @param out The message stream to add to
98      * @throws IOException if errors occur
99      */

100     protected void attach(File JavaDoc file, PrintStream JavaDoc out)
101          throws IOException JavaDoc {
102         if (!file.exists() || !file.canRead()) {
103             throw new BuildException("File \"" + file.getName()
104                  + "\" does not exist or is not "
105                  + "readable.");
106         }
107
108         if (includeFileNames) {
109             out.println();
110
111             String JavaDoc filename = file.getName();
112             int filenamelength = filename.length();
113
114             out.println(filename);
115             for (int star = 0; star < filenamelength; star++) {
116                 out.print('=');
117             }
118             out.println();
119         }
120
121         int length;
122         final int maxBuf = 1024;
123         byte[] buf = new byte[maxBuf];
124         FileInputStream JavaDoc finstr = new FileInputStream JavaDoc(file);
125
126         try {
127             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(finstr, buf.length);
128
129             while ((length = in.read(buf)) != -1) {
130                 out.write(buf, 0, length);
131             }
132         } finally {
133             finstr.close();
134         }
135     }
136 }
137
138
Popular Tags