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