1 18 package org.apache.tools.ant.taskdefs.email; 19 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 import java.io.PrintStream ; 24 import java.io.OutputStream ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.UnsupportedEncodingException ; 28 29 import java.util.Vector ; 30 import java.util.Iterator ; 31 import java.util.Properties ; 32 import java.util.Enumeration ; 33 import java.util.StringTokenizer ; 34 35 import java.security.Provider ; 36 import java.security.Security ; 37 38 import javax.activation.DataHandler ; 39 import javax.activation.FileDataSource ; 40 41 import javax.mail.Message ; 42 import javax.mail.Session ; 43 import javax.mail.Transport ; 44 import javax.mail.Authenticator ; 45 import javax.mail.MessagingException ; 46 import javax.mail.PasswordAuthentication ; 47 import javax.mail.internet.MimeMessage ; 48 import javax.mail.internet.MimeBodyPart ; 49 import javax.mail.internet.MimeMultipart ; 50 import javax.mail.internet.InternetAddress ; 51 import javax.mail.internet.AddressException ; 52 53 import org.apache.tools.ant.BuildException; 54 55 60 public class MimeMailer extends Mailer { 61 private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 62 63 64 private static final String DEFAULT_CHARSET 65 = System.getProperty("file.encoding"); 66 67 73 class StringDataSource implements javax.activation.DataSource { 74 private String data = null; 75 private String type = null; 76 private String charset = null; 77 private ByteArrayOutputStream out; 78 79 public InputStream getInputStream() throws IOException { 80 if (data == null && out == null) { 81 throw new IOException ("No data"); 82 } 83 if (out != null) { 84 String encodedOut = out.toString(charset); 85 data = (data != null) ? data.concat(encodedOut) : encodedOut; 86 out = null; 87 } 88 return new ByteArrayInputStream (data.getBytes(charset)); 89 } 90 91 public OutputStream getOutputStream() throws IOException { 92 out = (out == null) ? new ByteArrayOutputStream () : out; 93 return out; 94 } 95 96 public void setContentType(String type) { 97 this.type = type.toLowerCase(); 98 } 99 100 public String getContentType() { 101 if (type != null && type.indexOf("charset") > 0 102 && type.startsWith("text/")) { 103 return type; 104 } 105 return new StringBuffer (type != null ? type : "text/plain").append( 107 "; charset=").append(charset).toString(); 108 } 109 110 public String getName() { 111 return "StringDataSource"; 112 } 113 114 public void setCharset(String charset) { 115 this.charset = charset; 116 } 117 118 public String getCharset() { 119 return charset; 120 } 121 } 122 123 128 public void send() { 129 try { 130 Properties props = new Properties (); 131 132 props.put("mail.smtp.host", host); 133 props.put("mail.smtp.port", String.valueOf(port)); 134 135 Session sesh; 139 Authenticator auth; 140 if (SSL) { 141 try { 142 Provider p = (Provider ) Class.forName( 143 "com.sun.net.ssl.internal.ssl.Provider").newInstance(); 144 Security.addProvider(p); 145 } catch (Exception e) { 146 throw new BuildException("could not instantiate ssl " 147 + "security provider, check that you have JSSE in " 148 + "your classpath"); 149 } 150 props.put("mail.smtp.socketFactory.class", SSL_FACTORY); 152 props.put("mail.smtp.socketFactory.fallback", "false"); 153 } 154 if (user == null && password == null) { 155 sesh = Session.getDefaultInstance(props, null); 156 } else { 157 props.put("mail.smtp.auth", "true"); 158 auth = new SimpleAuthenticator(user, password); 159 sesh = Session.getInstance(props, auth); 160 } 161 MimeMessage msg = new MimeMessage (sesh); 163 MimeMultipart attachments = new MimeMultipart (); 164 165 if (from.getName() == null) { 167 msg.setFrom(new InternetAddress (from.getAddress())); 168 } else { 169 msg.setFrom(new InternetAddress (from.getAddress(), 170 from.getName())); 171 } 172 msg.setReplyTo(internetAddresses(replyToList)); 174 msg.setRecipients(Message.RecipientType.TO, 175 internetAddresses(toList)); 176 msg.setRecipients(Message.RecipientType.CC, 177 internetAddresses(ccList)); 178 msg.setRecipients(Message.RecipientType.BCC, 179 internetAddresses(bccList)); 180 181 String charset = parseCharSetFromMimeType(message.getMimeType()); 184 if (charset != null) { 185 message.setCharset(charset); 187 } else { 188 charset = message.getCharset(); 190 if (charset == null) { 191 charset = DEFAULT_CHARSET; 193 message.setCharset(charset); 194 } 195 } 196 StringDataSource sds = new StringDataSource(); 198 sds.setContentType(message.getMimeType()); 199 sds.setCharset(charset); 200 201 if (subject != null) { 202 msg.setSubject(subject, charset); 203 } 204 msg.addHeader("Date", getDate()); 205 206 for (Iterator iter = headers.iterator(); iter.hasNext();) { 207 Header h = (Header) iter.next(); 208 msg.addHeader(h.getName(), h.getValue()); 209 } 210 PrintStream out = new PrintStream (sds.getOutputStream()); 211 message.print(out); 212 out.close(); 213 214 MimeBodyPart textbody = new MimeBodyPart (); 215 textbody.setDataHandler(new DataHandler (sds)); 216 attachments.addBodyPart(textbody); 217 218 Enumeration e = files.elements(); 219 220 while (e.hasMoreElements()) { 221 File file = (File ) e.nextElement(); 222 223 MimeBodyPart body; 224 225 body = new MimeBodyPart (); 226 if (!file.exists() || !file.canRead()) { 227 throw new BuildException("File \"" + file.getAbsolutePath() 228 + "\" does not exist or is not " 229 + "readable."); 230 } 231 FileDataSource fileData = new FileDataSource (file); 232 DataHandler fileDataHandler = new DataHandler (fileData); 233 234 body.setDataHandler(fileDataHandler); 235 body.setFileName(file.getName()); 236 attachments.addBodyPart(body); 237 } 238 msg.setContent(attachments); 239 Transport.send(msg); 240 } catch (MessagingException e) { 241 throw new BuildException("Problem while sending mime mail:", e); 242 } catch (IOException e) { 243 throw new BuildException("Problem while sending mime mail:", e); 244 } 245 } 246 247 private static InternetAddress [] internetAddresses(Vector list) 248 throws AddressException , UnsupportedEncodingException { 249 InternetAddress [] addrs = new InternetAddress [list.size()]; 250 251 for (int i = 0; i < list.size(); ++i) { 252 EmailAddress addr = (EmailAddress) list.elementAt(i); 253 254 String name = addr.getName(); 255 addrs[i] = (name == null) 256 ? new InternetAddress (addr.getAddress()) 257 : new InternetAddress (addr.getAddress(), name); 258 } 259 return addrs; 260 } 261 262 private String parseCharSetFromMimeType(String type) { 263 int pos; 264 if (type == null || (pos = type.indexOf("charset")) < 0) { 265 return null; 266 } 267 StringTokenizer token = new StringTokenizer (type.substring(pos), "=; "); 269 token.nextToken(); return token.nextToken(); 271 } 272 273 static class SimpleAuthenticator extends Authenticator { 274 private String user = null; 275 private String password = null; 276 public SimpleAuthenticator(String user, String password) { 277 this.user = user; 278 this.password = password; 279 } 280 public PasswordAuthentication getPasswordAuthentication() { 281 282 return new PasswordAuthentication (user, password); 283 } 284 } 285 } 286 287 | Popular Tags |