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