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