001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.email;
019:
020: import java.io.BufferedInputStream;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024: import java.io.PrintStream;
025: import java.util.Enumeration;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.mail.MailMessage;
028:
029: /**
030: * Class responsible for sending email through raw protocol methods.
031: *
032: * @since Ant 1.5
033: */
034: class PlainMailer extends Mailer {
035: /**
036: * Sends the email using the apache MailMessage class.
037: *
038: * @see org.apache.tools.mail.MailMessage
039: */
040: public void send() {
041: try {
042: MailMessage mailMessage = new MailMessage(host, port);
043:
044: mailMessage.from(from.toString());
045:
046: Enumeration e;
047:
048: e = replyToList.elements();
049: while (e.hasMoreElements()) {
050: mailMessage.replyto(e.nextElement().toString());
051: }
052: e = toList.elements();
053: while (e.hasMoreElements()) {
054: mailMessage.to(e.nextElement().toString());
055: }
056: e = ccList.elements();
057: while (e.hasMoreElements()) {
058: mailMessage.cc(e.nextElement().toString());
059: }
060: e = bccList.elements();
061: while (e.hasMoreElements()) {
062: mailMessage.bcc(e.nextElement().toString());
063: }
064: if (subject != null) {
065: mailMessage.setSubject(subject);
066: }
067: mailMessage.setHeader("Date", getDate());
068: if (message.getCharset() != null) {
069: mailMessage.setHeader("Content-Type", message
070: .getMimeType()
071: + "; charset=\"" + message.getCharset() + "\"");
072: } else {
073: mailMessage.setHeader("Content-Type", message
074: .getMimeType());
075: }
076: e = headers.elements();
077: while (e.hasMoreElements()) {
078: Header h = (Header) e.nextElement();
079: mailMessage.setHeader(h.getName(), h.getValue());
080: }
081: PrintStream out = mailMessage.getPrintStream();
082: message.print(out);
083:
084: e = files.elements();
085: while (e.hasMoreElements()) {
086: attach((File) e.nextElement(), out);
087: }
088: mailMessage.sendAndClose();
089: } catch (IOException ioe) {
090: throw new BuildException("IO error sending mail", ioe);
091: }
092:
093: }
094:
095: /**
096: * Attaches a file to this email
097: *
098: * @param file The file to attache
099: * @param out The message stream to add to
100: * @throws IOException if errors occur
101: */
102: protected void attach(File file, PrintStream out)
103: throws IOException {
104: if (!file.exists() || !file.canRead()) {
105: throw new BuildException("File \"" + file.getName()
106: + "\" does not exist or is not " + "readable.");
107: }
108:
109: if (includeFileNames) {
110: out.println();
111:
112: String filename = file.getName();
113: int filenamelength = filename.length();
114:
115: out.println(filename);
116: for (int star = 0; star < filenamelength; star++) {
117: out.print('=');
118: }
119: out.println();
120: }
121:
122: int length;
123: final int maxBuf = 1024;
124: byte[] buf = new byte[maxBuf];
125: FileInputStream finstr = new FileInputStream(file);
126:
127: try {
128: BufferedInputStream in = new BufferedInputStream(finstr,
129: buf.length);
130:
131: while ((length = in.read(buf)) != -1) {
132: out.write(buf, 0, length);
133: }
134: } finally {
135: finstr.close();
136: }
137: }
138: }
|