|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package javax.mail.internet; |
|
19 |
| |
|
20 |
| import java.io.BufferedInputStream; |
|
21 |
| import java.io.ByteArrayInputStream; |
|
22 |
| import java.io.ByteArrayOutputStream; |
|
23 |
| import java.io.IOException; |
|
24 |
| import java.io.InputStream; |
|
25 |
| import java.io.ObjectStreamException; |
|
26 |
| import java.io.OutputStream; |
|
27 |
| import java.io.UnsupportedEncodingException; |
|
28 |
| import java.util.ArrayList; |
|
29 |
| import java.util.Arrays; |
|
30 |
| import java.util.Date; |
|
31 |
| import java.util.Enumeration; |
|
32 |
| import java.util.HashMap; |
|
33 |
| import java.util.List; |
|
34 |
| import java.util.Map; |
|
35 |
| |
|
36 |
| import javax.activation.DataHandler; |
|
37 |
| import javax.mail.Address; |
|
38 |
| import javax.mail.Flags; |
|
39 |
| import javax.mail.Folder; |
|
40 |
| import javax.mail.Message; |
|
41 |
| import javax.mail.MessagingException; |
|
42 |
| import javax.mail.Multipart; |
|
43 |
| import javax.mail.Part; |
|
44 |
| import javax.mail.Session; |
|
45 |
| import javax.mail.internet.HeaderTokenizer.Token; |
|
46 |
| |
|
47 |
| import org.apache.geronimo.mail.util.ASCIIUtil; |
|
48 |
| import org.apache.geronimo.mail.util.SessionUtil; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| public class MimeMessage extends Message implements MimePart { |
|
54 |
| private static final String MIME_ADDRESS_STRICT = "mail.mime.address.strict"; |
|
55 |
| private static final String MIME_DECODEFILENAME = "mail.mime.decodefilename"; |
|
56 |
| private static final String MIME_ENCODEFILENAME = "mail.mime.encodefilename"; |
|
57 |
| |
|
58 |
| private static final String MAIL_ALTERNATES = "mail.alternates"; |
|
59 |
| private static final String MAIL_REPLYALLCC = "mail.replyallcc"; |
|
60 |
| |
|
61 |
| |
|
62 |
| private static int messageID = 0; |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| public static class RecipientType extends Message.RecipientType { |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| public static final RecipientType NEWSGROUPS = new RecipientType("Newsgroups"); |
|
73 |
| |
|
74 |
1
| protected RecipientType(String type) {
|
|
75 |
1
| super(type);
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
0
| protected Object readResolve() throws ObjectStreamException {
|
|
84 |
0
| if (this.type.equals("Newsgroups")) {
|
|
85 |
0
| return NEWSGROUPS;
|
|
86 |
| } else { |
|
87 |
0
| return super.readResolve();
|
|
88 |
| } |
|
89 |
| } |
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| protected DataHandler dh; |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| protected byte[] content; |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| protected InputStream contentStream; |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| protected InternetHeaders headers; |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| protected Flags flags; |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| protected boolean modified; |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| protected boolean saved; |
|
123 |
| |
|
124 |
| private final MailDateFormat dateFormat = new MailDateFormat(); |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
20
| public MimeMessage(Session session) {
|
|
134 |
20
| super(session);
|
|
135 |
20
| headers = new InternetHeaders();
|
|
136 |
20
| flags = new Flags();
|
|
137 |
| |
|
138 |
20
| modified = true;
|
|
139 |
20
| saved = false;
|
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
3
| public MimeMessage(Session session, InputStream in) throws MessagingException {
|
|
150 |
3
| this(session);
|
|
151 |
3
| parse(in);
|
|
152 |
| |
|
153 |
3
| modified = false;
|
|
154 |
| |
|
155 |
3
| saved = true;
|
|
156 |
| } |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
1
| public MimeMessage(MimeMessage message) throws MessagingException {
|
|
165 |
1
| super(message.session);
|
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
1
| ByteArrayOutputStream copy = new ByteArrayOutputStream();
|
|
175 |
| |
|
176 |
1
| try {
|
|
177 |
| |
|
178 |
1
| message.writeTo(copy);
|
|
179 |
1
| copy.close();
|
|
180 |
| |
|
181 |
| |
|
182 |
1
| ByteArrayInputStream inData = new ByteArrayInputStream(copy.toByteArray());
|
|
183 |
| |
|
184 |
1
| inData.close();
|
|
185 |
1
| parse (inData);
|
|
186 |
| |
|
187 |
1
| saved = true;
|
|
188 |
| |
|
189 |
1
| modified = false;
|
|
190 |
| } catch (IOException e) { |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
0
| throw new MessagingException("Error copying MimeMessage data", e);
|
|
195 |
| } |
|
196 |
| } |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
3
| protected MimeMessage(Folder folder, int number) {
|
|
205 |
3
| super(folder, number);
|
|
206 |
3
| headers = new InternetHeaders();
|
|
207 |
3
| flags = new Flags();
|
|
208 |
| |
|
209 |
| |
|
210 |
3
| saved = true;
|
|
211 |
| |
|
212 |
3
| modified = true;
|
|
213 |
| } |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
0
| protected MimeMessage(Folder folder, InputStream in, int number) throws MessagingException {
|
|
224 |
0
| this(folder, number);
|
|
225 |
0
| parse(in);
|
|
226 |
| |
|
227 |
0
| modified = false;
|
|
228 |
| |
|
229 |
0
| saved = true;
|
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
0
| protected MimeMessage(Folder folder, InternetHeaders headers, byte[] content, int number) throws MessagingException {
|
|
243 |
0
| this(folder, number);
|
|
244 |
0
| this.headers = headers;
|
|
245 |
0
| this.content = content;
|
|
246 |
| |
|
247 |
0
| modified = false;
|
|
248 |
| } |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
4
| protected void parse(InputStream in) throws MessagingException {
|
|
257 |
4
| in = new BufferedInputStream(in);
|
|
258 |
| |
|
259 |
4
| headers = new InternetHeaders(in);
|
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
4
| ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
264 |
4
| try {
|
|
265 |
4
| byte buffer[] = new byte[1024];
|
|
266 |
4
| int count;
|
|
267 |
?
| while ((count = in.read(buffer, 0, 1024)) != -1) {
|
|
268 |
4
| baos.write(buffer, 0, count);
|
|
269 |
| } |
|
270 |
| } catch (Exception e) { |
|
271 |
0
| throw new MessagingException(e.toString(), e);
|
|
272 |
| } |
|
273 |
| |
|
274 |
4
| content = baos.toByteArray();
|
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
8
| public Address[] getFrom() throws MessagingException {
|
|
287 |
| |
|
288 |
8
| boolean strict = isStrictAddressing();
|
|
289 |
8
| Address[] result = getHeaderAsInternetAddresses("From", strict);
|
|
290 |
8
| if (result == null) {
|
|
291 |
5
| result = getHeaderAsInternetAddresses("Sender", strict);
|
|
292 |
| } |
|
293 |
8
| return result;
|
|
294 |
| } |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
4
| public void setFrom(Address address) throws MessagingException {
|
|
306 |
4
| setHeader("From", address);
|
|
307 |
| } |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
1
| public void setFrom() throws MessagingException {
|
|
315 |
1
| InternetAddress address = InternetAddress.getLocalAddress(session);
|
|
316 |
| |
|
317 |
1
| if (address == null) {
|
|
318 |
0
| throw new MessagingException("No local address defined");
|
|
319 |
| } |
|
320 |
1
| setFrom(address);
|
|
321 |
| } |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
1
| public void addFrom(Address[] addresses) throws MessagingException {
|
|
331 |
1
| addHeader("From", addresses);
|
|
332 |
| } |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
4
| public Address getSender() throws MessagingException {
|
|
341 |
4
| Address[] addrs = getHeaderAsInternetAddresses("Sender", isStrictAddressing());
|
|
342 |
4
| return addrs != null && addrs.length > 0 ? addrs[0] : null;
|
|
343 |
| } |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
5
| public void setSender(Address address) throws MessagingException {
|
|
355 |
5
| setHeader("Sender", address);
|
|
356 |
| } |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
32
| public Address[] getRecipients(Message.RecipientType type) throws MessagingException {
|
|
373 |
| |
|
374 |
| |
|
375 |
32
| if (type == RecipientType.NEWSGROUPS) {
|
|
376 |
8
| return getHeaderAsNewsAddresses(getHeaderForRecipientType(type));
|
|
377 |
| } |
|
378 |
| |
|
379 |
24
| return getHeaderAsInternetAddresses(getHeaderForRecipientType(type), isStrictAddressing());
|
|
380 |
| } |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
6
| public Address[] getAllRecipients() throws MessagingException {
|
|
397 |
6
| List recipients = new ArrayList();
|
|
398 |
6
| addRecipientsToList(recipients, RecipientType.TO);
|
|
399 |
6
| addRecipientsToList(recipients, RecipientType.CC);
|
|
400 |
6
| addRecipientsToList(recipients, RecipientType.BCC);
|
|
401 |
6
| addRecipientsToList(recipients, RecipientType.NEWSGROUPS);
|
|
402 |
| |
|
403 |
| |
|
404 |
6
| if (recipients.isEmpty()) {
|
|
405 |
1
| return null;
|
|
406 |
| } |
|
407 |
5
| return (Address[]) recipients.toArray(new Address[recipients.size()]);
|
|
408 |
| } |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
24
| private void addRecipientsToList(List list, Message.RecipientType type) throws MessagingException {
|
|
420 |
| |
|
421 |
24
| Address[] recipients;
|
|
422 |
24
| if (type == RecipientType.NEWSGROUPS) {
|
|
423 |
6
| recipients = getHeaderAsNewsAddresses(getHeaderForRecipientType(type));
|
|
424 |
| } |
|
425 |
| else { |
|
426 |
18
| recipients = getHeaderAsInternetAddresses(getHeaderForRecipientType(type), isStrictAddressing());
|
|
427 |
| } |
|
428 |
24
| if (recipients != null) {
|
|
429 |
13
| list.addAll(Arrays.asList(recipients));
|
|
430 |
| } |
|
431 |
| } |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
14
| public void setRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException {
|
|
443 |
14
| setHeader(getHeaderForRecipientType(type), addresses);
|
|
444 |
| } |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
9
| public void setRecipients(Message.RecipientType type, String address) throws MessagingException {
|
|
458 |
9
| setOrRemoveHeader(getHeaderForRecipientType(type), address);
|
|
459 |
| } |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
7
| public void addRecipients(Message.RecipientType type, Address[] address) throws MessagingException {
|
|
471 |
7
| addHeader(getHeaderForRecipientType(type), address);
|
|
472 |
| } |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
| |
|
482 |
4
| public void addRecipients(Message.RecipientType type, String address) throws MessagingException {
|
|
483 |
4
| addHeader(getHeaderForRecipientType(type), address);
|
|
484 |
| } |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
| |
|
491 |
| |
|
492 |
| |
|
493 |
| |
|
494 |
3
| public Address[] getReplyTo() throws MessagingException {
|
|
495 |
3
| Address[] addresses = getHeaderAsInternetAddresses("Reply-To", isStrictAddressing());
|
|
496 |
3
| if (addresses == null) {
|
|
497 |
1
| addresses = getFrom();
|
|
498 |
| } |
|
499 |
3
| return addresses;
|
|
500 |
| } |
|
501 |
| |
|
502 |
| |
|
503 |
| |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
3
| public void setReplyTo(Address[] address) throws MessagingException {
|
|
511 |
3
| setHeader("Reply-To", address);
|
|
512 |
| } |
|
513 |
| |
|
514 |
| |
|
515 |
| |
|
516 |
| |
|
517 |
| |
|
518 |
| |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
3
| public String getSubject() throws MessagingException {
|
|
524 |
3
| String subject = getSingleHeader("Subject");
|
|
525 |
3
| if (subject == null) {
|
|
526 |
1
| return null;
|
|
527 |
| } else { |
|
528 |
2
| try {
|
|
529 |
| |
|
530 |
2
| return MimeUtility.decodeText(MimeUtility.unfold(subject));
|
|
531 |
| } catch (UnsupportedEncodingException e) { |
|
532 |
| |
|
533 |
| } |
|
534 |
| } |
|
535 |
| |
|
536 |
0
| return subject;
|
|
537 |
| } |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
| |
|
543 |
| |
|
544 |
| |
|
545 |
| |
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
3
| public void setSubject(String subject) throws MessagingException {
|
|
551 |
| |
|
552 |
3
| setSubject(subject, null);
|
|
553 |
| } |
|
554 |
| |
|
555 |
4
| public void setSubject(String subject, String charset) throws MessagingException {
|
|
556 |
| |
|
557 |
4
| if (subject == null) {
|
|
558 |
1
| removeHeader("Subject");
|
|
559 |
| } |
|
560 |
| else { |
|
561 |
3
| try {
|
|
562 |
3
| String s = MimeUtility.fold(9, MimeUtility.encodeText(subject, charset, null));
|
|
563 |
| |
|
564 |
3
| setHeader("Subject", MimeUtility.fold(9, MimeUtility.encodeText(subject, charset, null)));
|
|
565 |
| } catch (UnsupportedEncodingException e) { |
|
566 |
0
| throw new MessagingException("Encoding error", e);
|
|
567 |
| } |
|
568 |
| } |
|
569 |
| } |
|
570 |
| |
|
571 |
| |
|
572 |
| |
|
573 |
| |
|
574 |
| |
|
575 |
| |
|
576 |
| |
|
577 |
| |
|
578 |
0
| public Date getSentDate() throws MessagingException {
|
|
579 |
0
| String value = getSingleHeader("Date");
|
|
580 |
0
| if (value == null) {
|
|
581 |
0
| return null;
|
|
582 |
| } |
|
583 |
0
| try {
|
|
584 |
0
| return dateFormat.parse(value);
|
|
585 |
| } catch (java.text.ParseException e) { |
|
586 |
0
| return null;
|
|
587 |
| } |
|
588 |
| } |
|
589 |
| |
|
590 |
| |
|
591 |
| |
|
592 |
| |
|
593 |
| |
|
594 |
| |
|
595 |
| |
|
596 |
| |
|
597 |
| |
|
598 |
0
| public void setSentDate(Date sent) throws MessagingException {
|
|
599 |
0
| setOrRemoveHeader("Date", dateFormat.format(sent));
|
|
600 |
| } |
|
601 |
| |
|
602 |
| |
|
603 |
| |
|
604 |
| |
|
605 |
| |
|
606 |
| |
|
607 |
| |
|
608 |
| |
|
609 |
0
| public Date getReceivedDate() throws MessagingException {
|
|
610 |
0
| return null;
|
|
611 |
| } |
|
612 |
| |
|
613 |
| |
|
614 |
| |
|
615 |
| |
|
616 |
| |
|
617 |
| |
|
618 |
| |
|
619 |
| |
|
620 |
| |
|
621 |
| |
|
622 |
0
| public int getSize() throws MessagingException {
|
|
623 |
0
| if (content != null) {
|
|
624 |
0
| return content.length;
|
|
625 |
| } |
|
626 |
0
| if (contentStream != null) {
|
|
627 |
0
| try {
|
|
628 |
0
| int size = contentStream.available();
|
|
629 |
0
| if (size > 0) {
|
|
630 |
0
| return size;
|
|
631 |
| } |
|
632 |
| } catch (IOException e) { |
|
633 |
| |
|
634 |
| } |
|
635 |
| } |
|
636 |
0
| return -1;
|
|
637 |
| } |
|
638 |
| |
|
639 |
| |
|
640 |
| |
|
641 |
| |
|
642 |
| |
|
643 |
| |
|
644 |
| |
|
645 |
| |
|
646 |
| |
|
647 |
| |
|
648 |
| |
|
649 |
0
| public int getLineCount() throws MessagingException {
|
|
650 |
0
| return -1;
|
|
651 |
| } |
|
652 |
| |
|
653 |
| |
|
654 |
| |
|
655 |
| |
|
656 |
| |
|
657 |
| |
|
658 |
| |
|
659 |
| |
|
660 |
14
| public String getContentType() throws MessagingException {
|
|
661 |
14
| String value = getSingleHeader("Content-Type");
|
|
662 |
14
| if (value == null) {
|
|
663 |
1
| value = "text/plain";
|
|
664 |
| } |
|
665 |
14
| return value;
|
|
666 |
| } |
|
667 |
| |
|
668 |
| |
|
669 |
| |
|
670 |
| |
|
671 |
| |
|
672 |
| |
|
673 |
| |
|
674 |
| |
|
675 |
| |
|
676 |
| |
|
677 |
| |
|
678 |
0
| public boolean isMimeType(String type) throws MessagingException {
|
|
679 |
0
| return new ContentType(getContentType()).match(type);
|
|
680 |
| } |
|
681 |
| |
|
682 |
| |
|
683 |
| |
|
684 |
| |
|
685 |
| |
|
686 |
| |
|
687 |
| |
|
688 |
| |
|
689 |
| |
|
690 |
0
| public String getDisposition() throws MessagingException {
|
|
691 |
0
| String disp = getSingleHeader("Content-Disposition");
|
|
692 |
0
| if (disp != null) {
|
|
693 |
0
| return new ContentDisposition(disp).getDisposition();
|
|
694 |
| } |
|
695 |
0
| return null;
|
|
696 |
| } |
|
697 |
| |
|
698 |
| |
|
699 |
| |
|
700 |
| |
|
701 |
| |
|
702 |
| |
|
703 |
| |
|
704 |
| |
|
705 |
| |
|
706 |
| |
|
707 |
| |
|
708 |
0
| public void setDisposition(String disposition) throws MessagingException {
|
|
709 |
0
| if (disposition == null) {
|
|
710 |
0
| removeHeader("Content-Disposition");
|
|
711 |
| } |
|
712 |
| else { |
|
713 |
| |
|
714 |
0
| String currentHeader = getSingleHeader("Content-Disposition");
|
|
715 |
0
| if (currentHeader != null) {
|
|
716 |
0
| ContentDisposition content = new ContentDisposition(currentHeader);
|
|
717 |
0
| content.setDisposition(disposition);
|
|
718 |
0
| setHeader("Content-Disposition", content.toString());
|
|
719 |
| } |
|
720 |
| else { |
|
721 |
| |
|
722 |
0
| setHeader("Content-Disposition", disposition);
|
|
723 |
| } |
|
724 |
| } |
|
725 |
| } |
|
726 |
| |
|
727 |
| |
|
728 |
| |
|
729 |
| |
|
730 |
| |
|
731 |
| |
|
732 |
| |
|
733 |
| |
|
734 |
6
| public String getEncoding() throws MessagingException {
|
|
735 |
| |
|
736 |
6
| String encoding = getSingleHeader("Content-Transfer-Encoding");
|
|
737 |
6
| if (encoding != null) {
|
|
738 |
| |
|
739 |
| |
|
740 |
0
| HeaderTokenizer tokenizer = new HeaderTokenizer(encoding, HeaderTokenizer.MIME);
|
|
741 |
| |
|
742 |
0
| Token token = tokenizer.next();
|
|
743 |
0
| while (token.getType() != Token.EOF) {
|
|
744 |
| |
|
745 |
0
| if (token.getType() == Token.ATOM) {
|
|
746 |
0
| return token.getValue();
|
|
747 |
| } |
|
748 |
| } |
|
749 |
| |
|
750 |
| |
|
751 |
0
| return encoding;
|
|
752 |
| } |
|
753 |
| |
|
754 |
6
| return null;
|
|
755 |
| } |
|
756 |
| |
|
757 |
| |
|
758 |
| |
|
759 |
| |
|
760 |
| |
|
761 |
| |
|
762 |
| |
|
763 |
| |
|
764 |
0
| public String getContentID() throws MessagingException {
|
|
765 |
0
| return getSingleHeader("Content-ID");
|
|
766 |
| } |
|
767 |
| |
|
768 |
0
| public void setContentID(String cid) throws MessagingException {
|
|
769 |
0
| setOrRemoveHeader("Content-ID", cid);
|
|
770 |
| } |
|
771 |
| |
|
772 |
0
| public String getContentMD5() throws MessagingException {
|
|
773 |
0
| return getSingleHeader("Content-MD5");
|
|
774 |
| } |
|
775 |
| |
|
776 |
0
| public void setContentMD5(String md5) throws MessagingException {
|
|
777 |
0
| setOrRemoveHeader("Content-MD5", md5);
|
|
778 |
| } |
|
779 |
| |
|
780 |
3
| public String getDescription() throws MessagingException {
|
|
781 |
3
| String description = getSingleHeader("Content-Description");
|
|
782 |
3
| if (description != null) {
|
|
783 |
2
| try {
|
|
784 |
| |
|
785 |
2
| return MimeUtility.decodeText(MimeUtility.unfold(description));
|
|
786 |
| } catch (UnsupportedEncodingException e) { |
|
787 |
| |
|
788 |
| } |
|
789 |
| } |
|
790 |
| |
|
791 |
1
| return description;
|
|
792 |
| } |
|
793 |
| |
|
794 |
2
| public void setDescription(String description) throws MessagingException {
|
|
795 |
2
| setDescription(description, null);
|
|
796 |
| } |
|
797 |
| |
|
798 |
3
| public void setDescription(String description, String charset) throws MessagingException {
|
|
799 |
3
| if (description == null) {
|
|
800 |
1
| removeHeader("Content-Description");
|
|
801 |
| } |
|
802 |
| else { |
|
803 |
2
| try {
|
|
804 |
2
| setHeader("Content-Description", MimeUtility.fold(21, MimeUtility.encodeText(description, charset, null)));
|
|
805 |
| } catch (UnsupportedEncodingException e) { |
|
806 |
0
| throw new MessagingException(e.getMessage(), e);
|
|
807 |
| } |
|
808 |
| } |
|
809 |
| |
|
810 |
| } |
|
811 |
| |
|
812 |
0
| public String[] getContentLanguage() throws MessagingException {
|
|
813 |
0
| return getHeader("Content-Language");
|
|
814 |
| } |
|
815 |
| |
|
816 |
0
| public void setContentLanguage(String[] languages) throws MessagingException {
|
|
817 |
0
| if (languages == null) {
|
|
818 |
0
| removeHeader("Content-Language");
|
|
819 |
0
| } else if (languages.length == 1) {
|
|
820 |
0
| setHeader("Content-Language", languages[0]);
|
|
821 |
| } else { |
|
822 |
0
| StringBuffer buf = new StringBuffer(languages.length * 20);
|
|
823 |
0
| buf.append(languages[0]);
|
|
824 |
0
| for (int i = 1; i < languages.length; i++) {
|
|
825 |
0
| buf.append(',').append(languages[i]);
|
|
826 |
| } |
|
827 |
0
| setHeader("Content-Language", buf.toString());
|
|
828 |
| } |
|
829 |
| } |
|
830 |
| |
|
831 |
0
| public String getMessageID() throws MessagingException {
|
|
832 |
0
| return getSingleHeader("Message-ID");
|
|
833 |
| } |
|
834 |
| |
|
835 |
0
| public String getFileName() throws MessagingException {
|
|
836 |
| |
|
837 |
0
| String disposition = getDisposition();
|
|
838 |
0
| String filename = null;
|
|
839 |
| |
|
840 |
0
| if (disposition != null) {
|
|
841 |
0
| filename = new ContentDisposition(disposition).getParameter("filename");
|
|
842 |
| } |
|
843 |
| |
|
844 |
| |
|
845 |
| |
|
846 |
0
| if (filename == null) {
|
|
847 |
0
| String type = getContentType();
|
|
848 |
0
| if (type != null) {
|
|
849 |
0
| try {
|
|
850 |
0
| filename = new ContentType(type).getParameter("name");
|
|
851 |
| } catch (ParseException e) { |
|
852 |
| } |
|
853 |
| } |
|
854 |
| } |
|
855 |
| |
|
856 |
0
| if (filename != null && SessionUtil.getBooleanProperty(session, MIME_DECODEFILENAME, false)) {
|
|
857 |
0
| try {
|
|
858 |
0
| filename = MimeUtility.decodeText(filename);
|
|
859 |
| } catch (UnsupportedEncodingException e) { |
|
860 |
0
| throw new MessagingException("Unable to decode filename", e);
|
|
861 |
| } |
|
862 |
| } |
|
863 |
| |
|
864 |
0
| return filename;
|
|
865 |
| } |
|
866 |
| |
|
867 |
| |
|
868 |
0
| public void setFileName(String name) throws MessagingException {
|
|
869 |
| |
|
870 |
| |
|
871 |
0
| if (name != null && SessionUtil.getBooleanProperty(session, MIME_ENCODEFILENAME, false)) {
|
|
872 |
0
| try {
|
|
873 |
0
| name = MimeUtility.encodeText(name);
|
|
874 |
| } catch (UnsupportedEncodingException e) { |
|
875 |
0
| throw new MessagingException("Unable to encode filename", e);
|
|
876 |
| } |
|
877 |
| } |
|
878 |
| |
|
879 |
| |
|
880 |
0
| String disposition = getDisposition();
|
|
881 |
| |
|
882 |
0
| if (disposition == null) {
|
|
883 |
0
| disposition = Part.ATTACHMENT;
|
|
884 |
| } |
|
885 |
| |
|
886 |
0
| ContentDisposition contentDisposition = new ContentDisposition(disposition);
|
|
887 |
0
| contentDisposition.setParameter("filename", name);
|
|
888 |
| |
|
889 |
| |
|
890 |
0
| setDisposition(contentDisposition.toString());
|
|
891 |
| } |
|
892 |
| |
|
893 |
0
| public InputStream getInputStream() throws MessagingException, IOException {
|
|
894 |
0
| return getDataHandler().getInputStream();
|
|
895 |
| } |
|
896 |
| |
|
897 |
2
| protected InputStream getContentStream() throws MessagingException {
|
|
898 |
2
| if (contentStream != null) {
|
|
899 |
0
| return contentStream;
|
|
900 |
| } |
|
901 |
| |
|
902 |
2
| if (content != null) {
|
|
903 |
2
| return new ByteArrayInputStream(content);
|
|
904 |
| } else { |
|
905 |
0
| throw new MessagingException("No content");
|
|
906 |
| } |
|
907 |
| } |
|
908 |
| |
|
909 |
0
| public InputStream getRawInputStream() throws MessagingException {
|
|
910 |
0
| return getContentStream();
|
|
911 |
| } |
|
912 |
| |
|
913 |
9
| public synchronized DataHandler getDataHandler() throws MessagingException {
|
|
914 |
9
| if (dh == null) {
|
|
915 |
2
| dh = new DataHandler(new MimePartDataSource(this));
|
|
916 |
| } |
|
917 |
9
| return dh;
|
|
918 |
| } |
|
919 |
| |
|
920 |
3
| public Object getContent() throws MessagingException, IOException {
|
|
921 |
3
| return getDataHandler().getContent();
|
|
922 |
| } |
|
923 |
| |
|
924 |
6
| public void setDataHandler(DataHandler handler) throws MessagingException {
|
|
925 |
6
| dh = handler;
|
|
926 |
| |
|
927 |
| |
|
928 |
| |
|
929 |
6
| removeHeader("Content-Type");
|
|
930 |
6
| removeHeader("Content-Transfer-Encoding");
|
|
931 |
| } |
|
932 |
| |
|
933 |
3
| public void setContent(Object content, String type) throws MessagingException {
|
|
934 |
3
| setDataHandler(new DataHandler(content, type));
|
|
935 |
| } |
|
936 |
| |
|
937 |
1
| public void setText(String text) throws MessagingException {
|
|
938 |
1
| setText(text, null, "plain");
|
|
939 |
| } |
|
940 |
| |
|
941 |
1
| public void setText(String text, String charset) throws MessagingException {
|
|
942 |
1
| setText(text, charset, "plain");
|
|
943 |
| } |
|
944 |
| |
|
945 |
| |
|
946 |
3
| public void setText(String text, String charset, String subtype) throws MessagingException {
|
|
947 |
| |
|
948 |
3
| if (charset == null) {
|
|
949 |
| |
|
950 |
1
| if (!ASCIIUtil.isAscii(text)) {
|
|
951 |
0
| charset = MimeUtility.getDefaultMIMECharset();
|
|
952 |
| } |
|
953 |
| else { |
|
954 |
1
| charset = "us-ascii";
|
|
955 |
| } |
|
956 |
| } |
|
957 |
3
| setContent(text, "text/" + subtype + "; charset=" + MimeUtility.quote(charset, HeaderTokenizer.MIME));
|
|
958 |
| } |
|
959 |
| |
|
960 |
3
| public void setContent(Multipart part) throws MessagingException {
|
|
961 |
3
| setDataHandler(new DataHandler(part, part.getContentType()));
|
|
962 |
3
| part.setParent(this);
|
|
963 |
| } |
|
964 |
| |
|
965 |
0
| public Message reply(boolean replyToAll) throws MessagingException {
|
|
966 |
| |
|
967 |
0
| MimeMessage reply = createMimeMessage(session);
|
|
968 |
| |
|
969 |
| |
|
970 |
0
| String newSubject = getSubject();
|
|
971 |
0
| if (newSubject != null) {
|
|
972 |
| |
|
973 |
| |
|
974 |
0
| if (!newSubject.regionMatches(true, 0, "Re: ", 0, 4)) {
|
|
975 |
0
| newSubject = "Re: " + newSubject;
|
|
976 |
| } |
|
977 |
0
| reply.setSubject(newSubject);
|
|
978 |
| } |
|
979 |
| |
|
980 |
0
| Address[] toRecipients = getReplyTo();
|
|
981 |
| |
|
982 |
| |
|
983 |
0
| reply.setRecipients(Message.RecipientType.TO, getReplyTo());
|
|
984 |
| |
|
985 |
| |
|
986 |
0
| if (replyToAll) {
|
|
987 |
| |
|
988 |
| |
|
989 |
0
| HashMap masterList = new HashMap();
|
|
990 |
| |
|
991 |
| |
|
992 |
0
| InternetAddress localMail = InternetAddress.getLocalAddress(session);
|
|
993 |
0
| if (localMail != null) {
|
|
994 |
0
| masterList.put(localMail.getAddress(), localMail);
|
|
995 |
| } |
|
996 |
| |
|
997 |
0
| String alternates = session.getProperty(MAIL_ALTERNATES);
|
|
998 |
0
| if (alternates != null) {
|
|
999 |
| |
|
1000 |
0
| Address[] alternateList = InternetAddress.parse(alternates, false);
|
|
1001 |
0
| mergeAddressList(masterList, alternateList);
|
|
1002 |
| } |
|
1003 |
| |
|
1004 |
| |
|
1005 |
| |
|
1006 |
| |
|
1007 |
| |
|
1008 |
| |
|
1009 |
0
| Address[] toList = pruneAddresses(masterList, getRecipients(Message.RecipientType.TO));
|
|
1010 |
0
| if (toList.length != 0) {
|
|
1011 |
| |
|
1012 |
| |
|
1013 |
| |
|
1014 |
0
| if (SessionUtil.getBooleanProperty(session, MAIL_REPLYALLCC, false)) {
|
|
1015 |
0
| reply.addRecipients(Message.RecipientType.CC, toList);
|
|
1016 |
| } |
|
1017 |
| else { |
|
1018 |
0
| reply.addRecipients(Message.RecipientType.TO, toList);
|
|
1019 |
| } |
|
1020 |
| } |
|
1021 |
| |
|
1022 |
0
| toList = pruneAddresses(masterList, getRecipients(Message.RecipientType.CC));
|
|
1023 |
0
| if (toList.length != 0) {
|
|
1024 |
0
| reply.addRecipients(Message.RecipientType.CC, toList);
|
|
1025 |
| } |
|
1026 |
| |
|
1027 |
| |
|
1028 |
| |
|
1029 |
0
| toList = getRecipients(RecipientType.NEWSGROUPS);
|
|
1030 |
0
| if (toList != null && toList.length != 0) {
|
|
1031 |
0
| reply.addRecipients(RecipientType.NEWSGROUPS, toList);
|
|
1032 |
| } |
|
1033 |
| } |
|
1034 |
| |
|
1035 |
| |
|
1036 |
| |
|
1037 |
| |
|
1038 |
| |
|
1039 |
0
| setFlags(new Flags(Flags.Flag.ANSWERED), true);
|
|
1040 |
| |
|
1041 |
0
| return reply;
|
|
1042 |
| } |
|
1043 |
| |
|
1044 |
| |
|
1045 |
| |
|
1046 |
| |
|
1047 |
| |
|
1048 |
| |
|
1049 |
| |
|
1050 |
| |
|
1051 |
| |
|
1052 |
0
| private void mergeAddressList(Map master, Address[] list) {
|
|
1053 |
| |
|
1054 |
0
| if (list == null) {
|
|
1055 |
0
| return;
|
|
1056 |
| } |
|
1057 |
0
| for (int i = 0; i < list.length; i++) {
|
|
1058 |
0
| InternetAddress address = (InternetAddress)list[i];
|
|
1059 |
| |
|
1060 |
| |
|
1061 |
0
| if (!master.containsKey(address.getAddress())) {
|
|
1062 |
0
| master.put(address.getAddress(), address);
|
|
1063 |
| } |
|
1064 |
| } |
|
1065 |
| } |
|
1066 |
| |
|
1067 |
| |
|
1068 |
| |
|
1069 |
| |
|
1070 |
| |
|
1071 |
| |
|
1072 |
| |
|
1073 |
| |
|
1074 |
| |
|
1075 |
| |
|
1076 |
| |
|
1077 |
| |
|
1078 |
0
| private Address[] pruneAddresses(Map master, Address[] list) {
|
|
1079 |
| |
|
1080 |
0
| if (list == null) {
|
|
1081 |
0
| return new Address[0];
|
|
1082 |
| } |
|
1083 |
| |
|
1084 |
| |
|
1085 |
0
| ArrayList prunedList = new ArrayList(list.length);
|
|
1086 |
0
| for (int i = 0; i < list.length; i++) {
|
|
1087 |
0
| InternetAddress address = (InternetAddress)list[i];
|
|
1088 |
| |
|
1089 |
| |
|
1090 |
| |
|
1091 |
0
| if (!master.containsKey(address.getAddress())) {
|
|
1092 |
0
| master.put(address.getAddress(), address);
|
|
1093 |
0
| prunedList.add(address);
|
|
1094 |
| } |
|
1095 |
| } |
|
1096 |
| |
|
1097 |
0
| return (Address[])prunedList.toArray(new Address[0]);
|
|
1098 |
| } |
|
1099 |
| |
|
1100 |
| |
|
1101 |
| |
|
1102 |
| |
|
1103 |
| |
|
1104 |
| |
|
1105 |
| |
|
1106 |
| |
|
1107 |
| |
|
1108 |
| |
|
1109 |
4
| public void writeTo(OutputStream out) throws MessagingException, IOException {
|
|
1110 |
4
| writeTo(out, null);
|
|
1111 |
| } |
|
1112 |
| |
|
1113 |
| |
|
1114 |
| |
|
1115 |
| |
|
1116 |
| |
|
1117 |
| |
|
1118 |
| |
|
1119 |
| |
|
1120 |
| |
|
1121 |
| |
|
1122 |
| |
|
1123 |
| |
|
1124 |
| |
|
1125 |
4
| public void writeTo(OutputStream out, String[] ignoreHeaders) throws MessagingException, IOException {
|
|
1126 |
| |
|
1127 |
4
| if (!saved) {
|
|
1128 |
3
| saveChanges();
|
|
1129 |
| } |
|
1130 |
| |
|
1131 |
| |
|
1132 |
4
| headers.writeTo(out, ignoreHeaders);
|
|
1133 |
| |
|
1134 |
4
| out.write('\r');
|
|
1135 |
4
| out.write('\n');
|
|
1136 |
| |
|
1137 |
| |
|
1138 |
| |
|
1139 |
4
| if (modified) {
|
|
1140 |
4
| dh.writeTo(MimeUtility.encode(out, getEncoding()));
|
|
1141 |
| } else { |
|
1142 |
| |
|
1143 |
0
| if (content != null) {
|
|
1144 |
0
| out.write(content);
|
|
1145 |
| } |
|
1146 |
| else { |
|
1147 |
| |
|
1148 |
| |
|
1149 |
0
| InputStream in = getContentStream();
|
|
1150 |
| |
|
1151 |
0
| byte[] buffer = new byte[8192];
|
|
1152 |
0
| int length = in.read(buffer);
|
|
1153 |
| |
|
1154 |
0
| while (length > 0) {
|
|
1155 |
0
| out.write(buffer, 0, length);
|
|
1156 |
0
| length = in.read(buffer);
|
|
1157 |
| } |
|
1158 |
0
| in.close();
|
|
1159 |
| } |
|
1160 |
| } |
|
1161 |
| |
|
1162 |
| |
|
1163 |
4
| out.flush();
|
|
1164 |
| } |
|
1165 |
| |
|
1166 |
| |
|
1167 |
| |
|
1168 |
| |
|
1169 |
| |
|
1170 |
| |
|
1171 |
| |
|
1172 |
| |
|
1173 |
| |
|
1174 |
| |
|
1175 |
| |
|
1176 |
| |
|
1177 |
45
| public String[] getHeader(String name) throws MessagingException {
|
|
1178 |
45
| return headers.getHeader(name);
|
|
1179 |
| } |
|
1180 |
| |
|
1181 |
| |
|
1182 |
| |
|
1183 |
| |
|
1184 |
| |
|
1185 |
| |
|
1186 |
| |
|
1187 |
| |
|
1188 |
| |
|
1189 |
| |
|
1190 |
| |
|
1191 |
| |
|
1192 |
| |
|
1193 |
76
| public String getHeader(String name, String delimiter) throws MessagingException {
|
|
1194 |
76
| return headers.getHeader(name, delimiter);
|
|
1195 |
| } |
|
1196 |
| |
|
1197 |
| |
|
1198 |
| |
|
1199 |
| |
|
1200 |
| |
|
1201 |
| |
|
1202 |
| |
|
1203 |
| |
|
1204 |
| |
|
1205 |
34
| public void setHeader(String name, String value) throws MessagingException {
|
|
1206 |
34
| headers.setHeader(name, value);
|
|
1207 |
| } |
|
1208 |
| |
|
1209 |
| |
|
1210 |
| |
|
1211 |
| |
|
1212 |
| |
|
1213 |
| |
|
1214 |
| |
|
1215 |
| |
|
1216 |
| |
|
1217 |
| |
|
1218 |
| |
|
1219 |
9
| private void setOrRemoveHeader(String name, String value) throws MessagingException {
|
|
1220 |
9
| if (value == null) {
|
|
1221 |
5
| headers.removeHeader(name);
|
|
1222 |
| } |
|
1223 |
| else { |
|
1224 |
4
| headers.setHeader(name, value);
|
|
1225 |
| } |
|
1226 |
| } |
|
1227 |
| |
|
1228 |
| |
|
1229 |
| |
|
1230 |
| |
|
1231 |
| |
|
1232 |
| |
|
1233 |
| |
|
1234 |
| |
|
1235 |
| |
|
1236 |
| |
|
1237 |
4
| public void addHeader(String name, String value) throws MessagingException {
|
|
1238 |
4
| headers.addHeader(name, value);
|
|
1239 |
| } |
|
1240 |
| |
|
1241 |
| |
|
1242 |
| |
|
1243 |
| |
|
1244 |
| |
|
1245 |
| |
|
1246 |
| |
|
1247 |
| |
|
1248 |
17
| public void removeHeader(String name) throws MessagingException {
|
|
1249 |
17
| headers.removeHeader(name);
|
|
1250 |
| } |
|
1251 |
| |
|
1252 |
| |
|
1253 |
| |
|
1254 |
| |
|
1255 |
| |
|
1256 |
| |
|
1257 |
| |
|
1258 |
0
| public Enumeration getAllHeaders() throws MessagingException {
|
|
1259 |
0
| return headers.getAllHeaders();
|
|
1260 |
| } |
|
1261 |
| |
|
1262 |
0
| public Enumeration getMatchingHeaders(String[] names) throws MessagingException {
|
|
1263 |
0
| return headers.getMatchingHeaders(names);
|
|
1264 |
| } |
|
1265 |
| |
|
1266 |
0
| public Enumeration getNonMatchingHeaders(String[] names) throws MessagingException {
|
|
1267 |
0
| return headers.getNonMatchingHeaders(names);
|
|
1268 |
| } |
|
1269 |
| |
|
1270 |
0
| public void addHeaderLine(String line) throws MessagingException {
|
|
1271 |
0
| headers.addHeaderLine(line);
|
|
1272 |
| } |
|
1273 |
| |
|
1274 |
0
| public Enumeration getAllHeaderLines() throws MessagingException {
|
|
1275 |
0
| return headers.getAllHeaderLines();
|
|
1276 |
| } |
|
1277 |
| |
|
1278 |
0
| public Enumeration getMatchingHeaderLines(String[] names) throws MessagingException {
|
|
1279 |
0
| return headers.getMatchingHeaderLines(names);
|
|
1280 |
| } |
|
1281 |
| |
|
1282 |
0
| public Enumeration getNonMatchingHeaderLines(String[] names) throws MessagingException {
|
|
1283 |
0
| return headers.getNonMatchingHeaderLines(names);
|
|
1284 |
| } |
|
1285 |
| |
|
1286 |
0
| public synchronized Flags getFlags() throws MessagingException {
|
|
1287 |
0
| return (Flags) flags.clone();
|
|
1288 |
| } |
|
1289 |
| |
|
1290 |
0
| public synchronized boolean isSet(Flags.Flag flag) throws MessagingException {
|
|
1291 |
0
| return flags.contains(flag);
|
|
1292 |
| } |
|
1293 |
| |
|
1294 |
| |
|
1295 |
| |
|
1296 |
| |
|
1297 |
| |
|
1298 |
| |
|
1299 |
| |
|
1300 |
| |
|
1301 |
| |
|
1302 |
0
| public synchronized void setFlags(Flags flag, boolean set) throws MessagingException {
|
|
1303 |
0
| if (set) {
|
|
1304 |
0
| flags.add(flag);
|
|
1305 |
| } |
|
1306 |
| else { |
|
1307 |
0
| flags.remove(flag);
|
|
1308 |
| } |
|
1309 |
| } |
|
1310 |
| |
|
1311 |
| |
|
1312 |
| |
|
1313 |
| |
|
1314 |
| |
|
1315 |
| |
|
1316 |
| |
|
1317 |
| |
|
1318 |
6
| public void saveChanges() throws MessagingException {
|
|
1319 |
| |
|
1320 |
6
| modified = true;
|
|
1321 |
6
| saved = true;
|
|
1322 |
| |
|
1323 |
6
| updateHeaders();
|
|
1324 |
| } |
|
1325 |
| |
|
1326 |
| |
|
1327 |
| |
|
1328 |
| |
|
1329 |
| |
|
1330 |
| |
|
1331 |
| |
|
1332 |
| |
|
1333 |
6
| protected void updateHeaders() throws MessagingException {
|
|
1334 |
| |
|
1335 |
| |
|
1336 |
6
| setHeader("MIME-Version", "1.0");
|
|
1337 |
| |
|
1338 |
6
| DataHandler handler = getDataHandler();
|
|
1339 |
| |
|
1340 |
6
| try {
|
|
1341 |
| |
|
1342 |
6
| String type = dh.getContentType();
|
|
1343 |
| |
|
1344 |
6
| ContentType content = new ContentType(type);
|
|
1345 |
| |
|
1346 |
| |
|
1347 |
6
| if (content.match("multipart/*")) {
|
|
1348 |
| |
|
1349 |
3
| try {
|
|
1350 |
3
| MimeMultipart part = (MimeMultipart)handler.getContent();
|
|
1351 |
3
| part.updateHeaders();
|
|
1352 |
| } catch (ClassCastException e) { |
|
1353 |
0
| throw new MessagingException("Message content is not MimeMultipart", e);
|
|
1354 |
| } |
|
1355 |
| } |
|
1356 |
3
| else if (!content.match("message/rfc822")) {
|
|
1357 |
| |
|
1358 |
| |
|
1359 |
3
| if (getSingleHeader("Content-Transfer-Encoding") == null) {
|
|
1360 |
3
| setHeader("Content-Transfer-Encoding", MimeUtility.getEncoding(handler));
|
|
1361 |
| } |
|
1362 |
| |
|
1363 |
| |
|
1364 |
3
| if (getSingleHeader("Content-Type") == null) {
|
|
1365 |
3
| if (SessionUtil.getBooleanProperty(session, "MIME_MAIL_SETDEFAULTTEXTCHARSET", true)) {
|
|
1366 |
| |
|
1367 |
3
| if (content.match("text/*")) {
|
|
1368 |
| |
|
1369 |
| |
|
1370 |
3
| if (content.getParameter("charset") == null) {
|
|
1371 |
| |
|
1372 |
0
| String encoding = getEncoding();
|
|
1373 |
| |
|
1374 |
| |
|
1375 |
0
| if (encoding != null && encoding.equalsIgnoreCase("7bit")) {
|
|
1376 |
0
| content.setParameter("charset", "us-ascii");
|
|
1377 |
| } |
|
1378 |
| else { |
|
1379 |
| |
|
1380 |
0
| content.setParameter("charset", MimeUtility.getDefaultMIMECharset());
|
|
1381 |
| } |
|
1382 |
| } |
|
1383 |
| } |
|
1384 |
| } |
|
1385 |
| } |
|
1386 |
| } |
|
1387 |
| |
|
1388 |
| |
|
1389 |
6
| if (getSingleHeader("Content-Type") == null) {
|
|
1390 |
| |
|
1391 |
| |
|
1392 |
5
| String disp = getSingleHeader("Content-Disposition");
|
|
1393 |
5
| if (disp != null) {
|
|
1394 |
| |
|
1395 |
0
| ContentDisposition disposition = new ContentDisposition(disp);
|
|
1396 |
| |
|
1397 |
0
| String filename = disposition.getParameter("filename");
|
|
1398 |
| |
|
1399 |
0
| if (filename != null) {
|
|
1400 |
0
| content.setParameter("name", filename);
|
|
1401 |
| } |
|
1402 |
| } |
|
1403 |
| |
|
1404 |
5
| setHeader("Content-Type", content.toString());
|
|
1405 |
| } |
|
1406 |
| |
|
1407 |
| |
|
1408 |
6
| updateMessageID();
|
|
1409 |
| |
|
1410 |
| } catch (IOException e) { |
|
1411 |
0
| throw new MessagingException("Error updating message headers", e);
|
|
1412 |
| } |
|
1413 |
| } |
|
1414 |
| |
|
1415 |
| |
|
1416 |
0
| protected InternetHeaders createInternetHeaders(InputStream in) throws MessagingException {
|
|
1417 |
| |
|
1418 |
0
| return new InternetHeaders(in);
|
|
1419 |
| } |
|
1420 |
| |
|
1421 |
| |
|
1422 |
| |
|
1423 |
| |
|
1424 |
| |
|
1425 |
| |
|
1426 |
| |
|
1427 |
| |
|
1428 |
| |
|
1429 |
14
| private Address[] getHeaderAsNewsAddresses(String header) throws MessagingException {
|
|
1430 |
| |
|
1431 |
| |
|
1432 |
14
| String mergedHeader = getHeader(header, ",");
|
|
1433 |
14
| if (mergedHeader != null) {
|
|
1434 |
7
| return NewsAddress.parse(mergedHeader);
|
|
1435 |
| } |
|
1436 |
7
| return null;
|
|
1437 |
| } |
|
1438 |
| |
|
1439 |
62
| private Address[] getHeaderAsInternetAddresses(String header, boolean strict) throws MessagingException {
|
|
1440 |
| |
|
1441 |
| |
|
1442 |
62
| String mergedHeader = getHeader(header, ",");
|
|
1443 |
| |
|
1444 |
62
| if (mergedHeader != null) {
|
|
1445 |
37
| return InternetAddress.parseHeader(mergedHeader, strict);
|
|
1446 |
| } |
|
1447 |
25
| return null;
|
|
1448 |
| } |
|
1449 |
| |
|
1450 |
| |
|
1451 |
| |
|
1452 |
| |
|
1453 |
| |
|
1454 |
| |
|
1455 |
| |
|
1456 |
| |
|
1457 |
57
| private boolean isStrictAddressing() {
|
|
1458 |
57
| return SessionUtil.getBooleanProperty(session, MIME_ADDRESS_STRICT, true);
|
|
1459 |
| } |
|
1460 |
| |
|
1461 |
| |
|
1462 |
| |
|
1463 |
| |
|
1464 |
| |
|
1465 |
| |
|
1466 |
| |
|
1467 |
| |
|
1468 |
| |
|
1469 |
9
| private void setHeader(String header, Address address) throws MessagingException {
|
|
1470 |
9
| if (address == null) {
|
|
1471 |
3
| removeHeader(header);
|
|
1472 |
| } |
|
1473 |
| else { |
|
1474 |
6
| setHeader(header, address.toString());
|
|
1475 |
| } |
|
1476 |
| } |
|
1477 |
| |
|
1478 |
| |
|
1479 |
| |
|
1480 |
| |
|
1481 |
| |
|
1482 |
| |
|
1483 |
| |
|
1484 |
| |
|
1485 |
17
| private void setHeader(String header, Address[] addresses) {
|
|
1486 |
17
| if (addresses == null) {
|
|
1487 |
5
| headers.removeHeader(header);
|
|
1488 |
| } |
|
1489 |
| else { |
|
1490 |
12
| headers.setHeader(header, addresses);
|
|
1491 |
| } |
|
1492 |
| } |
|
1493 |
| |
|
1494 |
8
| private void addHeader(String header, Address[] addresses) throws MessagingException {
|
|
1495 |
8
| headers.addHeader(header, InternetAddress.toString(addresses));
|
|
1496 |
| } |
|
1497 |
| |
|
1498 |
90
| private String getHeaderForRecipientType(Message.RecipientType type) throws MessagingException {
|
|
1499 |
90
| if (RecipientType.TO == type) {
|
|
1500 |
23
| return "To";
|
|
1501 |
67
| } else if (RecipientType.CC == type) {
|
|
1502 |
23
| return "Cc";
|
|
1503 |
44
| } else if (RecipientType.BCC == type) {
|
|
1504 |
22
| return "Bcc";
|
|
1505 |
22
| } else if (RecipientType.NEWSGROUPS == type) {
|
|
1506 |
22
| return "Newsgroups";
|
|
1507 |
| } else { |
|
1508 |
0
| throw new MessagingException("Unsupported recipient type: " + type.toString());
|
|
1509 |
| } |
|
1510 |
| } |
|
1511 |
| |
|
1512 |
| |
|
1513 |
| |
|
1514 |
| |
|
1515 |
| |
|
1516 |
| |
|
1517 |
| |
|
1518 |
| |
|
1519 |
| |
|
1520 |
| |
|
1521 |
| |
|
1522 |
43
| private String getSingleHeader(String name) throws MessagingException {
|
|
1523 |
43
| String[] values = getHeader(name);
|
|
1524 |
43
| if (values == null || values.length == 0) {
|
|
1525 |
25
| return null;
|
|
1526 |
| } else { |
|
1527 |
18
| return values[0];
|
|
1528 |
| } |
|
1529 |
| } |
|
1530 |
| |
|
1531 |
| |
|
1532 |
| |
|
1533 |
| |
|
1534 |
| |
|
1535 |
| |
|
1536 |
| |
|
1537 |
| |
|
1538 |
| |
|
1539 |
| |
|
1540 |
| |
|
1541 |
| |
|
1542 |
| |
|
1543 |
| |
|
1544 |
6
| public void updateMessageID() throws MessagingException {
|
|
1545 |
6
| StringBuffer id = new StringBuffer();
|
|
1546 |
| |
|
1547 |
6
| id.append('<');
|
|
1548 |
6
| id.append(new Object().hashCode());
|
|
1549 |
6
| id.append('.');
|
|
1550 |
6
| id.append(messageID++);
|
|
1551 |
6
| id.append(System.currentTimeMillis());
|
|
1552 |
6
| id.append('.');
|
|
1553 |
6
| id.append("JavaMail.");
|
|
1554 |
| |
|
1555 |
| |
|
1556 |
| |
|
1557 |
6
| InternetAddress localAddress = InternetAddress.getLocalAddress(session);
|
|
1558 |
6
| if (localAddress != null) {
|
|
1559 |
6
| id.append(localAddress.getAddress());
|
|
1560 |
| } |
|
1561 |
| else { |
|
1562 |
0
| id.append("javamailuser@localhost");
|
|
1563 |
| } |
|
1564 |
6
| id.append('>');
|
|
1565 |
| |
|
1566 |
6
| setHeader("Message-ID", id.toString());
|
|
1567 |
| } |
|
1568 |
| |
|
1569 |
| |
|
1570 |
| |
|
1571 |
| |
|
1572 |
| |
|
1573 |
| |
|
1574 |
| |
|
1575 |
| |
|
1576 |
| |
|
1577 |
| |
|
1578 |
| |
|
1579 |
| |
|
1580 |
0
| protected MimeMessage createMimeMessage(Session session) {
|
|
1581 |
0
| return new MimeMessage(session);
|
|
1582 |
| } |
|
1583 |
| |
|
1584 |
| } |