1 package org.apache.turbine.util.mail; 2 3 18 19 import java.net.URL ; 20 21 import java.util.Vector ; 22 23 import javax.activation.DataHandler ; 24 import javax.activation.DataSource ; 25 import javax.activation.URLDataSource ; 26 27 import javax.mail.MessagingException ; 28 import javax.mail.internet.MimeBodyPart ; 29 import javax.mail.internet.MimeMultipart ; 30 31 import org.apache.torque.util.Criteria; 32 33 50 public class MultiPartEmail extends Email 51 { 52 53 protected MimeMultipart emailBody; 54 55 56 protected MimeBodyPart main; 57 58 59 private String fileServer = null; 60 61 66 public MultiPartEmail() 67 throws MessagingException 68 { 69 this.init(); 70 } 71 72 93 public MultiPartEmail(Criteria criteria) 94 throws MessagingException 95 { 96 this.init(); 97 this.initCriteria(criteria); 98 } 99 100 105 protected void init() 106 throws MessagingException 107 { 108 super.init(); 109 110 fileServer = null; 111 112 113 emailBody = new MimeMultipart (); 114 message.setContent(emailBody); 115 116 117 main = new MimeBodyPart (); 118 emailBody.addBodyPart(main); 119 } 120 121 142 protected void initCriteria(Criteria criteria) 143 throws MessagingException 144 { 145 super.initCriteria(criteria); 146 147 if (criteria.containsKey(EMAIL_BODY)) 148 { 149 setMsg(criteria.getString(EMAIL_BODY)); 150 } 151 else 152 { 153 setMsg("NO MESSAGE"); 154 } 155 156 Vector attachments; 157 158 if (criteria.containsKey(ATTACHMENTS)) 159 { 160 attachments = (Vector ) criteria.get(ATTACHMENTS); 161 } 162 else 163 { 164 attachments = new Vector (); 165 } 166 167 if (criteria.containsKey(FILE_SERVER)) 168 { 169 fileServer = criteria.getString(FILE_SERVER); 170 } 171 172 for (int i = 0; i < attachments.size(); i++) 173 { 174 EmailAttachment attachment = 175 (EmailAttachment) attachments.elementAt(i); 176 attach(attachment); 177 } 178 } 179 180 187 public Email setMsg(String msg) 188 throws MessagingException 189 { 190 if (charset != null) 191 { 192 main.setText(msg, charset); 193 } 194 else 195 { 196 main.setText(msg); 197 } 198 return this; 199 } 200 201 208 public MultiPartEmail attach(EmailAttachment attachment) 209 throws MessagingException 210 { 211 URL url = attachment.getURL(); 212 if (url == null) 213 { 214 try 215 { 216 String file = attachment.getPath(); 217 url = new URL ("file", fileServer, file); 218 } 219 catch (Exception e) 220 { 221 throw new MessagingException ("Cannot find file", e); 222 } 223 } 224 225 return attach(url, attachment.getName(), 226 attachment.getDescription(), 227 attachment.getDisposition()); 228 } 229 230 240 public MultiPartEmail attach(URL url, String name, String description) 241 throws MessagingException 242 { 243 return attach(url, name, description, EmailAttachment.ATTACHMENT); 244 } 245 246 256 public MultiPartEmail attach(URL url, 257 String name, 258 String description, 259 String disposition) 260 throws MessagingException 261 { 262 return attach(new URLDataSource (url), name, description, disposition); 263 } 264 265 274 public MultiPartEmail attach(DataSource ds, 275 String name, 276 String description) 277 throws MessagingException 278 { 279 return attach(ds, name, description, EmailAttachment.ATTACHMENT); 280 } 281 282 292 public MultiPartEmail attach(DataSource ds, 293 String name, 294 String description, 295 String disposition) 296 throws MessagingException 297 { 298 MimeBodyPart mbp = new MimeBodyPart (); 299 emailBody.addBodyPart(mbp); 300 301 mbp.setDisposition(disposition); 302 mbp.setFileName(name); 303 mbp.setDescription(description); 304 mbp.setDataHandler(new DataHandler (ds)); 305 306 return this; 307 } 308 } 309 | Popular Tags |