1 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.mail.MailMessage; 28 29 34 class PlainMailer extends Mailer { 35 40 public void send() { 41 try { 42 MailMessage mailMessage = new MailMessage(host, port); 43 44 mailMessage.from(from.toString()); 45 46 Enumeration 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 out = mailMessage.getPrintStream(); 80 message.print(out); 81 82 e = files.elements(); 83 while (e.hasMoreElements()) { 84 attach((File ) e.nextElement(), out); 85 } 86 mailMessage.sendAndClose(); 87 } catch (IOException ioe) { 88 throw new BuildException("IO error sending mail", ioe); 89 } 90 91 } 92 93 100 protected void attach(File file, PrintStream out) 101 throws IOException { 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 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 finstr = new FileInputStream (file); 125 126 try { 127 BufferedInputStream in = new BufferedInputStream (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 |