1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.mail;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22
23 import javax.activation.DataHandler;
24 import javax.activation.DataSource;
25 import javax.activation.FileDataSource;
26 import javax.activation.URLDataSource;
27 import javax.mail.BodyPart;
28 import javax.mail.MessagingException;
29 import javax.mail.internet.MimeBodyPart;
30 import javax.mail.internet.MimeMultipart;
31 import javax.mail.internet.MimePart;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class MultiPartEmail extends Email
53 {
54
55 private MimeMultipart container;
56
57
58 private BodyPart primaryBodyPart = null;
59
60
61 private String subType;
62
63
64 private boolean initialized;
65
66
67 private boolean boolHasAttachments;
68
69
70
71
72
73
74
75 public void setSubType(String aSubType)
76 {
77 this.subType = aSubType;
78 }
79
80
81
82
83
84
85
86 public String getSubType()
87 {
88 return subType;
89 }
90
91
92
93
94
95
96
97
98
99
100
101 public Email addPart(String partContent, String partContentType)
102 throws EmailException
103 {
104 BodyPart bodyPart = createBodyPart();
105 try
106 {
107 bodyPart.setContent(partContent, partContentType);
108 getContainer().addBodyPart(bodyPart);
109 }
110 catch (MessagingException me)
111 {
112 throw new EmailException(me);
113 }
114
115 return this;
116 }
117
118
119
120
121
122
123
124
125
126
127 public Email addPart(MimeMultipart multipart) throws EmailException
128 {
129 try
130 {
131 return addPart(multipart, getContainer().getCount());
132 }
133 catch (MessagingException me)
134 {
135 throw new EmailException(me);
136 }
137 }
138
139
140
141
142
143
144
145
146
147
148 public Email addPart(MimeMultipart multipart, int index) throws EmailException
149 {
150 BodyPart bodyPart = createBodyPart();
151 try
152 {
153 bodyPart.setContent(multipart);
154 getContainer().addBodyPart(bodyPart, index);
155 }
156 catch (MessagingException me)
157 {
158 throw new EmailException(me);
159 }
160
161 return this;
162 }
163
164
165
166
167
168 protected void init()
169 {
170 if (initialized)
171 {
172 throw new IllegalStateException("Already initialized");
173 }
174
175 container = createMimeMultipart();
176 super.setContent(container);
177
178 initialized = true;
179 }
180
181
182
183
184
185
186
187
188
189
190 public Email setMsg(String msg) throws EmailException
191 {
192
193 if (EmailUtils.isEmpty(msg))
194 {
195 throw new EmailException("Invalid message supplied");
196 }
197 try
198 {
199 BodyPart primary = getPrimaryBodyPart();
200
201 if ((primary instanceof MimePart) && EmailUtils.isNotEmpty(charset))
202 {
203 ((MimePart) primary).setText(msg, charset);
204 }
205 else
206 {
207 primary.setText(msg);
208 }
209 }
210 catch (MessagingException me)
211 {
212 throw new EmailException(me);
213 }
214 return this;
215 }
216
217
218
219
220
221
222
223
224 public void buildMimeMessage() throws EmailException
225 {
226 try
227 {
228 if (primaryBodyPart != null)
229 {
230
231
232
233
234 BodyPart body = this.getPrimaryBodyPart();
235 try
236 {
237 body.getContent();
238 }
239 catch (IOException e)
240 {
241
242
243 }
244 }
245
246 if (subType != null)
247 {
248 getContainer().setSubType(subType);
249 }
250
251 super.buildMimeMessage();
252 }
253 catch (MessagingException me)
254 {
255 throw new EmailException(me);
256 }
257 }
258
259
260
261
262
263
264
265
266
267
268 public MultiPartEmail attach(EmailAttachment attachment)
269 throws EmailException
270 {
271 MultiPartEmail result = null;
272
273 if (attachment == null)
274 {
275 throw new EmailException("Invalid attachment supplied");
276 }
277
278 URL url = attachment.getURL();
279
280 if (url == null)
281 {
282 String fileName = null;
283 try
284 {
285 fileName = attachment.getPath();
286 File file = new File(fileName);
287 if (!file.exists())
288 {
289 throw new IOException(
290 "\"" + fileName + "\" does not exist");
291 }
292 result =
293 attach(
294 new FileDataSource(file),
295 attachment.getName(),
296 attachment.getDescription(),
297 attachment.getDisposition());
298 }
299 catch (Exception e)
300 {
301 throw new EmailException(
302 "Cannot attach file \"" + fileName + "\"",
303 e);
304 }
305 }
306 else
307 {
308 result =
309 attach(
310 url,
311 attachment.getName(),
312 attachment.getDescription(),
313 attachment.getDisposition());
314 }
315
316 return result;
317 }
318
319
320
321
322
323
324
325
326
327
328
329
330
331 public MultiPartEmail attach(URL url, String name, String description)
332 throws EmailException
333 {
334 return attach(url, name, description, EmailAttachment.ATTACHMENT);
335 }
336
337
338
339
340
341
342
343
344
345
346
347
348
349 public MultiPartEmail attach(
350 URL url,
351 String name,
352 String description,
353 String disposition)
354 throws EmailException
355 {
356
357 try
358 {
359 InputStream is = url.openStream();
360 is.close();
361 }
362 catch (IOException e)
363 {
364 throw new EmailException("Invalid URL set");
365 }
366
367 return attach(new URLDataSource(url), name, description, disposition);
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381 public MultiPartEmail attach(
382 DataSource ds,
383 String name,
384 String description)
385 throws EmailException
386 {
387
388 try
389 {
390 if (ds == null || ds.getInputStream() == null)
391 {
392 throw new EmailException("Invalid Datasource");
393 }
394 }
395 catch (IOException e)
396 {
397 throw new EmailException("Invalid Datasource");
398 }
399
400 return attach(ds, name, description, EmailAttachment.ATTACHMENT);
401 }
402
403
404
405
406
407
408
409
410
411
412
413
414
415 public MultiPartEmail attach(
416 DataSource ds,
417 String name,
418 String description,
419 String disposition)
420 throws EmailException
421 {
422 if (EmailUtils.isEmpty(name))
423 {
424 name = ds.getName();
425 }
426 BodyPart bodyPart = createBodyPart();
427 try
428 {
429 getContainer().addBodyPart(bodyPart);
430
431 bodyPart.setDisposition(disposition);
432 bodyPart.setFileName(name);
433 bodyPart.setDescription(description);
434 bodyPart.setDataHandler(new DataHandler(ds));
435 }
436 catch (MessagingException me)
437 {
438 throw new EmailException(me);
439 }
440 setBoolHasAttachments(true);
441
442 return this;
443 }
444
445
446
447
448
449
450
451
452 protected BodyPart getPrimaryBodyPart() throws MessagingException
453 {
454 if (!initialized)
455 {
456 init();
457 }
458
459
460 if (this.primaryBodyPart == null)
461 {
462 primaryBodyPart = createBodyPart();
463 getContainer().addBodyPart(primaryBodyPart, 0);
464 }
465
466 return primaryBodyPart;
467 }
468
469
470
471
472
473
474
475 protected MimeMultipart getContainer()
476 {
477 if (!initialized)
478 {
479 init();
480 }
481 return container;
482 }
483
484
485
486
487
488
489 protected BodyPart createBodyPart()
490 {
491 BodyPart bodyPart = new MimeBodyPart();
492 return bodyPart;
493 }
494
495
496
497
498 protected MimeMultipart createMimeMultipart()
499 {
500 MimeMultipart mmp = new MimeMultipart();
501 return mmp;
502 }
503
504
505
506
507
508 public boolean isBoolHasAttachments()
509 {
510 return boolHasAttachments;
511 }
512
513
514
515
516
517 public void setBoolHasAttachments(boolean b)
518 {
519 boolHasAttachments = b;
520 }
521
522
523
524
525
526 protected boolean isInitialized()
527 {
528 return initialized;
529 }
530
531
532
533
534
535 protected void setInitialized(boolean b)
536 {
537 initialized = b;
538 }
539
540 }