View Javadoc
1 package org.apache.turbine.util.mail; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.net.URL; 58 import java.util.Vector; 59 import javax.activation.DataHandler; 60 import javax.activation.DataSource; 61 import javax.activation.URLDataSource; 62 import javax.mail.MessagingException; 63 import javax.mail.internet.MimeBodyPart; 64 import javax.mail.internet.MimeMultipart; 65 import org.apache.torque.util.Criteria; 66 67 /*** 68 * A multipart email. 69 * 70 * <p>This class is used to send multi-part internet email like 71 * messages with attachments. 72 * 73 * <p>To create a multi-part email, call the default constructor and 74 * then you can call setMsg() to set the message and call the 75 * different attach() methods. 76 * 77 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 78 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> 79 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> 80 * @author <a href="mailto:unknown">Regis Koenig</a> 81 * @version $Id: MultiPartEmail.java,v 1.3 2002/07/11 16:53:20 mpoeschl Exp $ 82 */ 83 public class MultiPartEmail extends Email 84 { 85 /*** Body portion of the email. */ 86 protected MimeMultipart emailBody; 87 88 /*** The message container. */ 89 protected MimeBodyPart main; 90 91 /*** The file server if it exists. */ 92 private String fileServer = null; 93 94 /*** 95 * Initialize the multipart email. 96 * 97 * @exception MessagingException. 98 */ 99 public MultiPartEmail() 100 throws MessagingException 101 { 102 this.init(); 103 } 104 105 /*** 106 * Constructor used to initialize attributes. 107 * 108 * <p>This method uses the criteria object to set the different 109 * fields of the e-mail. The expected fields of the Criteria are: 110 * 111 * <ul> 112 * <li>SENDER_EMAIL</li> 113 * <li>RECEIVER_EMAIL</li> 114 * <li>EMAIL_SUBJECT</li> 115 * <li>EMAIL_BODY</li> 116 * <li>ATTACHMENTS - A Vector of EmailAttachment.</li> 117 * <li>FILE_SERVER - Where the files are located. If not given, 118 * they are assumed to be local.</li> 119 * </ul> 120 * 121 * Deprecated, since Criteria is deprecated in mail API. 122 * 123 * @param criteria A Criteria. 124 * @exception MessagingException. 125 */ 126 public MultiPartEmail(Criteria criteria) 127 throws MessagingException 128 { 129 this.init(); 130 this.initCriteria(criteria); 131 } 132 133 /*** 134 * Initialize the multipart email. 135 * 136 * @exception MessagingException. 137 */ 138 protected void init() 139 throws MessagingException 140 { 141 super.init(); 142 143 fileServer = null; 144 145 /* The body of the mail. */ 146 emailBody = new MimeMultipart(); 147 message.setContent(emailBody); 148 149 /* The main message content. */ 150 main = new MimeBodyPart(); 151 emailBody.addBodyPart(main); 152 } 153 154 155 /*** 156 * Uses the criteria to set the fields. 157 * 158 * <p>This method uses the criteria object to set the different 159 * fields of the e-mail. The expected fields of the Criteria are: 160 * 161 * <ul> 162 * <li>SENDER_EMAIL</li> 163 * <li>RECEIVER_EMAIL</li> 164 * <li>EMAIL_SUBJECT</li> 165 * <li>EMAIL_BODY</li> 166 * <li>ATTACHMENTS - A Vector of EmailAttachment.</li> 167 * <li>FILE_SERVER - Where the files are located. If not given, 168 * they are assumed to be local.</li> 169 * </ul> 170 * 171 * Deprecated, since the Criteria is deprecated. 172 * 173 * @param criteria A Criteria. 174 * @exception MessagingException. 175 */ 176 protected void initCriteria( Criteria criteria ) 177 throws MessagingException 178 { 179 super.initCriteria(criteria); 180 181 if (criteria.containsKey(EMAIL_BODY)) 182 { 183 setMsg(criteria.getString(EMAIL_BODY)); 184 } 185 else 186 { 187 setMsg("NO MESSAGE"); 188 } 189 190 Vector attachments; 191 192 if (criteria.containsKey(ATTACHMENTS)) 193 { 194 attachments = (Vector) criteria.get(ATTACHMENTS); 195 } 196 else 197 { 198 attachments = new Vector(); 199 } 200 201 if (criteria.containsKey(FILE_SERVER)) 202 { 203 fileServer = criteria.getString(FILE_SERVER); 204 } 205 206 for (int i = 0; i < attachments.size(); i++) 207 { 208 EmailAttachment attachment = 209 (EmailAttachment) attachments.elementAt(i); 210 attach(attachment); 211 } 212 } 213 214 /*** 215 * Set the message of the email. 216 * 217 * @param msg A String. 218 * @return An Email. 219 * @exception MessagingException. 220 */ 221 public Email setMsg(String msg) 222 throws MessagingException 223 { 224 if (charset != null) 225 { 226 main.setText(msg, charset); 227 } 228 else 229 { 230 main.setText(msg); 231 } 232 return this; 233 } 234 235 /*** 236 * Attach an EmailAttachement. 237 * 238 * @param attachment An EmailAttachment. 239 * @return A MultiPartEmail. 240 * @exception MessagingException. 241 */ 242 public MultiPartEmail attach(EmailAttachment attachment) 243 throws MessagingException 244 { 245 URL url = attachment.getURL(); 246 if (url == null) 247 { 248 try 249 { 250 String file = attachment.getPath(); 251 url = new URL("file", fileServer, file); 252 } 253 catch (Exception e) 254 { 255 throw new MessagingException("Cannot find file", e); 256 } 257 } 258 259 return attach(url, attachment.getName(), 260 attachment.getDescription(), 261 attachment.getDisposition()); 262 } 263 264 /*** 265 * Attach a file located by its URL. The disposition of the file 266 * is set to mixed. 267 * 268 * @param url The URL of the file (may be any valid URL). 269 * @param name The name field for the attachment. 270 * @param description A description for the attachment. 271 * @return A MultiPartEmail. 272 * @exception MessagingException. 273 */ 274 public MultiPartEmail attach(URL url, String name, String description) 275 throws MessagingException 276 { 277 return attach(url, name, description, EmailAttachment.ATTACHMENT); 278 } 279 280 /*** 281 * Attach a file located by its URL. 282 * 283 * @param url The URL of the file (may be any valid URL). 284 * @param name The name field for the attachment. 285 * @param description A description for the attachment. 286 * @param disposition Either mixed or inline. 287 * @return A MultiPartEmail. 288 * @exception MessagingException. 289 */ 290 public MultiPartEmail attach(URL url, 291 String name, 292 String description, 293 String disposition) 294 throws MessagingException 295 { 296 return attach(new URLDataSource(url), name, description, disposition); 297 } 298 299 /*** 300 * Attach a file specified as a DataSource interface. 301 * 302 * @param ds A DataSource interface for the file. 303 * @param name The name field for the attachment. 304 * @param description A description for the attachment. 305 * @return A MultiPartEmail. 306 * @exception MessagingException. 307 */ 308 public MultiPartEmail attach(DataSource ds, 309 String name, 310 String description) 311 throws MessagingException 312 { 313 return attach(ds, name, description, EmailAttachment.ATTACHMENT); 314 } 315 316 /*** 317 * Attach a file specified as a DataSource interface. 318 * 319 * @param ds A DataSource interface for the file. 320 * @param name The name field for the attachment. 321 * @param description A description for the attachement. 322 * @param disposition Either mixed or inline. 323 * @return A MultiPartEmail. 324 * @exception MessagingException. 325 */ 326 public MultiPartEmail attach(DataSource ds, 327 String name, 328 String description, 329 String disposition) 330 throws MessagingException 331 { 332 MimeBodyPart mbp = new MimeBodyPart(); 333 emailBody.addBodyPart(mbp); 334 335 mbp.setDisposition(disposition); 336 mbp.setFileName(name); 337 mbp.setDescription(description); 338 mbp.setDataHandler(new DataHandler(ds)); 339 340 return this; 341 } 342 }

This page was automatically generated by Maven