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.UnsupportedEncodingException;
20 import java.nio.charset.Charset;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import javax.mail.Authenticator;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32 import javax.mail.Session;
33 import javax.mail.Store;
34 import javax.mail.Transport;
35 import javax.mail.internet.AddressException;
36 import javax.mail.internet.InternetAddress;
37 import javax.mail.internet.MimeMessage;
38 import javax.mail.internet.MimeMultipart;
39 import javax.mail.internet.MimeUtility;
40 import javax.naming.Context;
41 import javax.naming.InitialContext;
42 import javax.naming.NamingException;
43
44 import org.apache.commons.mail.util.IDNEmailAddressConverter;
45
46
47
48
49
50
51
52
53
54
55 public abstract class Email
56 {
57
58 @Deprecated
59 public static final String SENDER_EMAIL = EmailConstants.SENDER_EMAIL;
60
61
62 @Deprecated
63 public static final String SENDER_NAME = EmailConstants.SENDER_NAME;
64
65
66 @Deprecated
67 public static final String RECEIVER_EMAIL = EmailConstants.RECEIVER_EMAIL;
68
69
70 @Deprecated
71 public static final String RECEIVER_NAME = EmailConstants.RECEIVER_NAME;
72
73
74 @Deprecated
75 public static final String EMAIL_SUBJECT = EmailConstants.EMAIL_SUBJECT;
76
77
78 @Deprecated
79 public static final String EMAIL_BODY = EmailConstants.EMAIL_BODY;
80
81
82 @Deprecated
83 public static final String CONTENT_TYPE = EmailConstants.CONTENT_TYPE;
84
85
86 @Deprecated
87 public static final String ATTACHMENTS = EmailConstants.ATTACHMENTS;
88
89
90 @Deprecated
91 public static final String FILE_SERVER = EmailConstants.FILE_SERVER;
92
93
94 @Deprecated
95 public static final String KOI8_R = EmailConstants.KOI8_R;
96
97
98 @Deprecated
99 public static final String ISO_8859_1 = EmailConstants.ISO_8859_1;
100
101
102 @Deprecated
103 public static final String US_ASCII = EmailConstants.US_ASCII;
104
105
106 @Deprecated
107 public static final String MAIL_DEBUG = EmailConstants.MAIL_DEBUG;
108
109
110 @Deprecated
111 public static final String MAIL_HOST = EmailConstants.MAIL_HOST;
112
113
114 @Deprecated
115 public static final String MAIL_PORT = EmailConstants.MAIL_PORT;
116
117
118 @Deprecated
119 public static final String MAIL_SMTP_FROM = EmailConstants.MAIL_SMTP_FROM;
120
121
122 @Deprecated
123 public static final String MAIL_SMTP_AUTH = EmailConstants.MAIL_SMTP_AUTH;
124
125
126 @Deprecated
127 public static final String MAIL_SMTP_USER = EmailConstants.MAIL_SMTP_USER;
128
129
130 @Deprecated
131 public static final String MAIL_SMTP_PASSWORD = EmailConstants.MAIL_SMTP_PASSWORD;
132
133
134 @Deprecated
135 public static final String MAIL_TRANSPORT_PROTOCOL = EmailConstants.MAIL_TRANSPORT_PROTOCOL;
136
137
138 @Deprecated
139 public static final String SMTP = EmailConstants.SMTP;
140
141
142 @Deprecated
143 public static final String TEXT_HTML = EmailConstants.TEXT_HTML;
144
145
146 @Deprecated
147 public static final String TEXT_PLAIN = EmailConstants.TEXT_PLAIN;
148
149
150 @Deprecated
151 public static final String MAIL_TRANSPORT_TLS = EmailConstants.MAIL_TRANSPORT_TLS;
152
153
154 @Deprecated
155 public static final String MAIL_SMTP_SOCKET_FACTORY_FALLBACK = EmailConstants.MAIL_SMTP_SOCKET_FACTORY_FALLBACK;
156
157
158 @Deprecated
159 public static final String MAIL_SMTP_SOCKET_FACTORY_CLASS = EmailConstants.MAIL_SMTP_SOCKET_FACTORY_CLASS;
160
161
162 @Deprecated
163 public static final String MAIL_SMTP_SOCKET_FACTORY_PORT = EmailConstants.MAIL_SMTP_SOCKET_FACTORY_PORT;
164
165
166 @Deprecated
167 public static final String MAIL_SMTP_CONNECTIONTIMEOUT = EmailConstants.MAIL_SMTP_CONNECTIONTIMEOUT;
168
169
170 @Deprecated
171 public static final String MAIL_SMTP_TIMEOUT = EmailConstants.MAIL_SMTP_TIMEOUT;
172
173
174 protected MimeMessage message;
175
176
177 protected String charset;
178
179
180 protected InternetAddress fromAddress;
181
182
183 protected String subject;
184
185
186 protected MimeMultipart emailBody;
187
188
189 protected Object content;
190
191
192 protected String contentType;
193
194
195 protected boolean debug;
196
197
198 protected Date sentDate;
199
200
201
202
203
204 protected Authenticator authenticator;
205
206
207
208
209
210 protected String hostName;
211
212
213
214
215
216 protected String smtpPort = "25";
217
218
219
220
221
222 protected String sslSmtpPort = "465";
223
224
225 protected List<InternetAddress> toList = new ArrayList<InternetAddress>();
226
227
228 protected List<InternetAddress> ccList = new ArrayList<InternetAddress>();
229
230
231 protected List<InternetAddress> bccList = new ArrayList<InternetAddress>();
232
233
234 protected List<InternetAddress> replyList = new ArrayList<InternetAddress>();
235
236
237
238
239
240
241
242 protected String bounceAddress;
243
244
245
246
247
248
249
250
251 protected Map<String, String> headers = new HashMap<String, String>();
252
253
254
255
256 protected boolean popBeforeSmtp;
257
258
259 protected String popHost;
260
261
262 protected String popUsername;
263
264
265 protected String popPassword;
266
267
268
269
270
271 @Deprecated
272 protected boolean tls;
273
274
275
276
277
278 @Deprecated
279 protected boolean ssl;
280
281
282 protected int socketTimeout = EmailConstants.SOCKET_TIMEOUT_MS;
283
284
285 protected int socketConnectionTimeout = EmailConstants.SOCKET_TIMEOUT_MS;
286
287
288
289
290
291
292
293
294
295 private boolean startTlsEnabled;
296
297
298
299
300
301
302
303 private boolean startTlsRequired;
304
305
306 private boolean sslOnConnect;
307
308
309
310
311
312
313
314 private boolean sslCheckServerIdentity;
315
316
317
318
319
320
321
322
323 private boolean sendPartial;
324
325
326 private Session session;
327
328
329
330
331
332
333
334 public void setDebug(final boolean d)
335 {
336 this.debug = d;
337 }
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 public void setAuthentication(final String userName, final String password)
353 {
354 this.setAuthenticator(new DefaultAuthenticator(userName, password));
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368 public void setAuthenticator(final Authenticator newAuthenticator)
369 {
370 this.authenticator = newAuthenticator;
371 }
372
373
374
375
376
377
378
379
380
381
382
383 public void setCharset(final String newCharset)
384 {
385 final Charset set = Charset.forName(newCharset);
386 this.charset = set.name();
387 }
388
389
390
391
392
393
394
395 public void setContent(final MimeMultipart aMimeMultipart)
396 {
397 this.emailBody = aMimeMultipart;
398 }
399
400
401
402
403
404
405
406
407 public void setContent(final Object aObject, final String aContentType)
408 {
409 this.content = aObject;
410 this.updateContentType(aContentType);
411 }
412
413
414
415
416
417
418
419 public void updateContentType(final String aContentType)
420 {
421 if (EmailUtils.isEmpty(aContentType))
422 {
423 this.contentType = null;
424 }
425 else
426 {
427
428 this.contentType = aContentType;
429
430
431 final String strMarker = "; charset=";
432 int charsetPos = aContentType.toLowerCase().indexOf(strMarker);
433
434 if (charsetPos != -1)
435 {
436
437 charsetPos += strMarker.length();
438 final int intCharsetEnd =
439 aContentType.toLowerCase().indexOf(" ", charsetPos);
440
441 if (intCharsetEnd != -1)
442 {
443 this.charset =
444 aContentType.substring(charsetPos, intCharsetEnd);
445 }
446 else
447 {
448 this.charset = aContentType.substring(charsetPos);
449 }
450 }
451 else
452 {
453
454
455 if (this.contentType.startsWith("text/") && EmailUtils.isNotEmpty(this.charset))
456 {
457 final StringBuffer contentTypeBuf = new StringBuffer(this.contentType);
458 contentTypeBuf.append(strMarker);
459 contentTypeBuf.append(this.charset);
460 this.contentType = contentTypeBuf.toString();
461 }
462 }
463 }
464 }
465
466
467
468
469
470
471
472
473 public void setHostName(final String aHostName)
474 {
475 checkSessionAlreadyInitialized();
476 this.hostName = aHostName;
477 }
478
479
480
481
482
483
484
485
486
487 @Deprecated
488 public void setTLS(final boolean withTLS)
489 {
490 setStartTLSEnabled(withTLS);
491 }
492
493
494
495
496
497
498
499
500
501 public Email setStartTLSEnabled(final boolean startTlsEnabled)
502 {
503 checkSessionAlreadyInitialized();
504 this.startTlsEnabled = startTlsEnabled;
505 this.tls = startTlsEnabled;
506 return this;
507 }
508
509
510
511
512
513
514
515
516
517
518
519 public Email setStartTLSRequired(final boolean startTlsRequired)
520 {
521 checkSessionAlreadyInitialized();
522 this.startTlsRequired = startTlsRequired;
523 return this;
524 }
525
526
527
528
529
530
531
532
533
534
535 public void setSmtpPort(final int aPortNumber)
536 {
537 checkSessionAlreadyInitialized();
538
539 if (aPortNumber < 1)
540 {
541 throw new IllegalArgumentException(
542 "Cannot connect to a port number that is less than 1 ( "
543 + aPortNumber
544 + " )");
545 }
546
547 this.smtpPort = Integer.toString(aPortNumber);
548 }
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564 public void setMailSession(final Session aSession)
565 {
566 EmailUtils.notNull(aSession, "no mail session supplied");
567
568 final Properties sessionProperties = aSession.getProperties();
569 final String auth = sessionProperties.getProperty(EmailConstants.MAIL_SMTP_AUTH);
570
571 if ("true".equalsIgnoreCase(auth))
572 {
573 final String userName = sessionProperties.getProperty(EmailConstants.MAIL_SMTP_USER);
574 final String password = sessionProperties.getProperty(EmailConstants.MAIL_SMTP_PASSWORD);
575
576 if (EmailUtils.isNotEmpty(userName) && EmailUtils.isNotEmpty(password))
577 {
578
579
580 this.authenticator = new DefaultAuthenticator(userName, password);
581 this.session = Session.getInstance(sessionProperties, this.authenticator);
582 }
583 else
584 {
585
586 this.session = aSession;
587 }
588 }
589 else
590 {
591 this.session = aSession;
592 }
593 }
594
595
596
597
598
599
600
601
602
603
604 public void setMailSessionFromJNDI(final String jndiName) throws NamingException
605 {
606 if (EmailUtils.isEmpty(jndiName))
607 {
608 throw new IllegalArgumentException("JNDI name missing");
609 }
610 Context ctx = null;
611 if (jndiName.startsWith("java:"))
612 {
613 ctx = new InitialContext();
614 }
615 else
616 {
617 ctx = (Context) new InitialContext().lookup("java:comp/env");
618
619 }
620 this.setMailSession((Session) ctx.lookup(jndiName));
621 }
622
623
624
625
626
627
628
629
630
631
632
633 public Session getMailSession() throws EmailException
634 {
635 if (this.session == null)
636 {
637 final Properties properties = new Properties(System.getProperties());
638 properties.setProperty(EmailConstants.MAIL_TRANSPORT_PROTOCOL, EmailConstants.SMTP);
639
640 if (EmailUtils.isEmpty(this.hostName))
641 {
642 this.hostName = properties.getProperty(EmailConstants.MAIL_HOST);
643 }
644
645 if (EmailUtils.isEmpty(this.hostName))
646 {
647 throw new EmailException("Cannot find valid hostname for mail session");
648 }
649
650 properties.setProperty(EmailConstants.MAIL_PORT, this.smtpPort);
651 properties.setProperty(EmailConstants.MAIL_HOST, this.hostName);
652 properties.setProperty(EmailConstants.MAIL_DEBUG, String.valueOf(this.debug));
653
654 properties.setProperty(EmailConstants.MAIL_TRANSPORT_STARTTLS_ENABLE,
655 isStartTLSEnabled() ? "true" : "false");
656 properties.setProperty(EmailConstants.MAIL_TRANSPORT_STARTTLS_REQUIRED,
657 isStartTLSRequired() ? "true" : "false");
658
659 properties.setProperty(EmailConstants.MAIL_SMTP_SEND_PARTIAL,
660 isSendPartial() ? "true" : "false");
661 properties.setProperty(EmailConstants.MAIL_SMTPS_SEND_PARTIAL,
662 isSendPartial() ? "true" : "false");
663
664 if (this.authenticator != null)
665 {
666 properties.setProperty(EmailConstants.MAIL_SMTP_AUTH, "true");
667 }
668
669 if (isSSLOnConnect())
670 {
671 properties.setProperty(EmailConstants.MAIL_PORT, this.sslSmtpPort);
672 properties.setProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_PORT, this.sslSmtpPort);
673 properties.setProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory");
674 properties.setProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_FALLBACK, "false");
675 }
676
677 if ((isSSLOnConnect() || isStartTLSEnabled()) && isSSLCheckServerIdentity())
678 {
679 properties.setProperty(EmailConstants.MAIL_SMTP_SSL_CHECKSERVERIDENTITY, "true");
680 }
681
682 if (this.bounceAddress != null)
683 {
684 properties.setProperty(EmailConstants.MAIL_SMTP_FROM, this.bounceAddress);
685 }
686
687 if (this.socketTimeout > 0)
688 {
689 properties.setProperty(EmailConstants.MAIL_SMTP_TIMEOUT, Integer.toString(this.socketTimeout));
690 }
691
692 if (this.socketConnectionTimeout > 0)
693 {
694 properties.setProperty(EmailConstants.MAIL_SMTP_CONNECTIONTIMEOUT, Integer.toString(this.socketConnectionTimeout));
695 }
696
697
698
699 this.session = Session.getInstance(properties, this.authenticator);
700 }
701 return this.session;
702 }
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717 public Email setFrom(final String email)
718 throws EmailException
719 {
720 return setFrom(email, null);
721 }
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737 public Email setFrom(final String email, final String name)
738 throws EmailException
739 {
740 return setFrom(email, name, this.charset);
741 }
742
743
744
745
746
747
748
749
750
751
752
753
754 public Email setFrom(final String email, final String name, final String charset)
755 throws EmailException
756 {
757 this.fromAddress = createInternetAddress(email, name, charset);
758 return this;
759 }
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775 public Email addTo(final String email)
776 throws EmailException
777 {
778 return addTo(email, null);
779 }
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795 public Email addTo(final String... emails)
796 throws EmailException
797 {
798 if (emails == null || emails.length == 0)
799 {
800 throw new EmailException("Address List provided was invalid");
801 }
802
803 for (final String email : emails)
804 {
805 addTo(email, null);
806 }
807
808 return this;
809 }
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826 public Email addTo(final String email, final String name)
827 throws EmailException
828 {
829 return addTo(email, name, this.charset);
830 }
831
832
833
834
835
836
837
838
839
840
841
842
843 public Email addTo(final String email, final String name, final String charset)
844 throws EmailException
845 {
846 this.toList.add(createInternetAddress(email, name, charset));
847 return this;
848 }
849
850
851
852
853
854
855
856
857
858
859
860
861 public Email setTo(final Collection<InternetAddress> aCollection) throws EmailException
862 {
863 if (aCollection == null || aCollection.isEmpty())
864 {
865 throw new EmailException("Address List provided was invalid");
866 }
867
868 this.toList = new ArrayList<InternetAddress>(aCollection);
869 return this;
870 }
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885 public Email addCc(final String email)
886 throws EmailException
887 {
888 return this.addCc(email, null);
889 }
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905 public Email addCc(final String... emails)
906 throws EmailException
907 {
908 if (emails == null || emails.length == 0)
909 {
910 throw new EmailException("Address List provided was invalid");
911 }
912
913 for (final String email : emails)
914 {
915 addCc(email, null);
916 }
917
918 return this;
919 }
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935 public Email addCc(final String email, final String name)
936 throws EmailException
937 {
938 return addCc(email, name, this.charset);
939 }
940
941
942
943
944
945
946
947
948
949
950
951
952 public Email addCc(final String email, final String name, final String charset)
953 throws EmailException
954 {
955 this.ccList.add(createInternetAddress(email, name, charset));
956 return this;
957 }
958
959
960
961
962
963
964
965
966
967
968
969
970 public Email setCc(final Collection<InternetAddress> aCollection) throws EmailException
971 {
972 if (aCollection == null || aCollection.isEmpty())
973 {
974 throw new EmailException("Address List provided was invalid");
975 }
976
977 this.ccList = new ArrayList<InternetAddress>(aCollection);
978 return this;
979 }
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994 public Email addBcc(final String email)
995 throws EmailException
996 {
997 return this.addBcc(email, null);
998 }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014 public Email addBcc(final String... emails)
1015 throws EmailException
1016 {
1017 if (emails == null || emails.length == 0)
1018 {
1019 throw new EmailException("Address List provided was invalid");
1020 }
1021
1022 for (final String email : emails)
1023 {
1024 addBcc(email, null);
1025 }
1026
1027 return this;
1028 }
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044 public Email addBcc(final String email, final String name)
1045 throws EmailException
1046 {
1047 return addBcc(email, name, this.charset);
1048 }
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 public Email addBcc(final String email, final String name, final String charset)
1062 throws EmailException
1063 {
1064 this.bccList.add(createInternetAddress(email, name, charset));
1065 return this;
1066 }
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079 public Email setBcc(final Collection<InternetAddress> aCollection) throws EmailException
1080 {
1081 if (aCollection == null || aCollection.isEmpty())
1082 {
1083 throw new EmailException("Address List provided was invalid");
1084 }
1085
1086 this.bccList = new ArrayList<InternetAddress>(aCollection);
1087 return this;
1088 }
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 public Email addReplyTo(final String email)
1104 throws EmailException
1105 {
1106 return this.addReplyTo(email, null);
1107 }
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123 public Email addReplyTo(final String email, final String name)
1124 throws EmailException
1125 {
1126 return addReplyTo(email, name, this.charset);
1127 }
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140 public Email addReplyTo(final String email, final String name, final String charset)
1141 throws EmailException
1142 {
1143 this.replyList.add(createInternetAddress(email, name, charset));
1144 return this;
1145 }
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 public Email setReplyTo(final Collection<InternetAddress> aCollection) throws EmailException
1159 {
1160 if (aCollection == null || aCollection.isEmpty())
1161 {
1162 throw new EmailException("Address List provided was invalid");
1163 }
1164
1165 this.replyList = new ArrayList<InternetAddress>(aCollection);
1166 return this;
1167 }
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180 public void setHeaders(final Map<String, String> map)
1181 {
1182 this.headers.clear();
1183
1184 for (final Map.Entry<String, String> entry : map.entrySet())
1185 {
1186 addHeader(entry.getKey(), entry.getValue());
1187 }
1188 }
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198 public void addHeader(final String name, final String value)
1199 {
1200 if (EmailUtils.isEmpty(name))
1201 {
1202 throw new IllegalArgumentException("name can not be null or empty");
1203 }
1204 if (EmailUtils.isEmpty(value))
1205 {
1206 throw new IllegalArgumentException("value can not be null or empty");
1207 }
1208
1209 this.headers.put(name, value);
1210 }
1211
1212
1213
1214
1215
1216
1217
1218
1219 public String getHeader(final String header)
1220 {
1221 return this.headers.get(header);
1222 }
1223
1224
1225
1226
1227
1228
1229
1230 public Map<String, String> getHeaders()
1231 {
1232 return this.headers;
1233 }
1234
1235
1236
1237
1238
1239
1240
1241
1242 public Email setSubject(final String aSubject)
1243 {
1244 this.subject = EmailUtils.replaceEndOfLineCharactersWithSpaces(aSubject);
1245 return this;
1246 }
1247
1248
1249
1250
1251
1252
1253
1254 public String getBounceAddress()
1255 {
1256 return this.bounceAddress;
1257 }
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 public Email setBounceAddress(final String email)
1271 {
1272 checkSessionAlreadyInitialized();
1273
1274 if (email != null && !email.isEmpty())
1275 {
1276 try
1277 {
1278 this.bounceAddress = createInternetAddress(email, null, this.charset).getAddress();
1279 }
1280 catch (final EmailException e)
1281 {
1282
1283 throw new IllegalArgumentException("Failed to set the bounce address : " + email, e);
1284 }
1285 }
1286 else
1287 {
1288 this.bounceAddress = email;
1289 }
1290
1291 return this;
1292 }
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303 public abstract Email setMsg(String msg) throws EmailException;
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315 public void buildMimeMessage() throws EmailException
1316 {
1317 if (this.message != null)
1318 {
1319
1320
1321 throw new IllegalStateException("The MimeMessage is already built.");
1322 }
1323
1324 try
1325 {
1326 this.message = this.createMimeMessage(this.getMailSession());
1327
1328 if (EmailUtils.isNotEmpty(this.subject))
1329 {
1330 if (EmailUtils.isNotEmpty(this.charset))
1331 {
1332 this.message.setSubject(this.subject, this.charset);
1333 }
1334 else
1335 {
1336 this.message.setSubject(this.subject);
1337 }
1338 }
1339
1340
1341 this.updateContentType(this.contentType);
1342
1343 if (this.content != null)
1344 {
1345 if (EmailConstants.TEXT_PLAIN.equalsIgnoreCase(this.contentType)
1346 && this.content instanceof String)
1347 {
1348
1349
1350 this.message.setText(this.content.toString(), this.charset);
1351 }
1352 else
1353 {
1354 this.message.setContent(this.content, this.contentType);
1355 }
1356 }
1357 else if (this.emailBody != null)
1358 {
1359 if (this.contentType == null)
1360 {
1361 this.message.setContent(this.emailBody);
1362 }
1363 else
1364 {
1365 this.message.setContent(this.emailBody, this.contentType);
1366 }
1367 }
1368 else
1369 {
1370 this.message.setText("");
1371 }
1372
1373 if (this.fromAddress != null)
1374 {
1375 this.message.setFrom(this.fromAddress);
1376 }
1377 else
1378 {
1379 if (session.getProperty(EmailConstants.MAIL_SMTP_FROM) == null
1380 && session.getProperty(EmailConstants.MAIL_FROM) == null)
1381 {
1382 throw new EmailException("From address required");
1383 }
1384 }
1385
1386 if (this.toList.size() + this.ccList.size() + this.bccList.size() == 0)
1387 {
1388 throw new EmailException("At least one receiver address required");
1389 }
1390
1391 if (this.toList.size() > 0)
1392 {
1393 this.message.setRecipients(
1394 Message.RecipientType.TO,
1395 this.toInternetAddressArray(this.toList));
1396 }
1397
1398 if (this.ccList.size() > 0)
1399 {
1400 this.message.setRecipients(
1401 Message.RecipientType.CC,
1402 this.toInternetAddressArray(this.ccList));
1403 }
1404
1405 if (this.bccList.size() > 0)
1406 {
1407 this.message.setRecipients(
1408 Message.RecipientType.BCC,
1409 this.toInternetAddressArray(this.bccList));
1410 }
1411
1412 if (this.replyList.size() > 0)
1413 {
1414 this.message.setReplyTo(
1415 this.toInternetAddressArray(this.replyList));
1416 }
1417
1418
1419 if (this.headers.size() > 0)
1420 {
1421 for (final Map.Entry<String, String> entry : this.headers.entrySet())
1422 {
1423 final String foldedValue = createFoldedHeaderValue(entry.getKey(), entry.getValue());
1424 this.message.addHeader(entry.getKey(), foldedValue);
1425 }
1426 }
1427
1428 if (this.message.getSentDate() == null)
1429 {
1430 this.message.setSentDate(getSentDate());
1431 }
1432
1433 if (this.popBeforeSmtp)
1434 {
1435 final Store store = session.getStore("pop3");
1436 store.connect(this.popHost, this.popUsername, this.popPassword);
1437 }
1438 }
1439 catch (final MessagingException me)
1440 {
1441 throw new EmailException(me);
1442 }
1443 }
1444
1445
1446
1447
1448
1449
1450
1451
1452 public String sendMimeMessage()
1453 throws EmailException
1454 {
1455 EmailUtils.notNull(this.message, "MimeMessage has not been created yet");
1456
1457 try
1458 {
1459 Transport.send(this.message);
1460 return this.message.getMessageID();
1461 }
1462 catch (final Throwable t)
1463 {
1464 final String msg = "Sending the email to the following server failed : "
1465 + this.getHostName()
1466 + ":"
1467 + this.getSmtpPort();
1468
1469 throw new EmailException(msg, t);
1470 }
1471 }
1472
1473
1474
1475
1476
1477
1478
1479 public MimeMessage getMimeMessage()
1480 {
1481 return this.message;
1482 }
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493 public String send() throws EmailException
1494 {
1495 this.buildMimeMessage();
1496 return this.sendMimeMessage();
1497 }
1498
1499
1500
1501
1502
1503
1504
1505
1506 public void setSentDate(final Date date)
1507 {
1508 if (date != null)
1509 {
1510
1511 this.sentDate = new Date(date.getTime());
1512 }
1513 }
1514
1515
1516
1517
1518
1519
1520
1521 public Date getSentDate()
1522 {
1523 if (this.sentDate == null)
1524 {
1525 return new Date();
1526 }
1527 return new Date(this.sentDate.getTime());
1528 }
1529
1530
1531
1532
1533
1534
1535 public String getSubject()
1536 {
1537 return this.subject;
1538 }
1539
1540
1541
1542
1543
1544
1545 public InternetAddress getFromAddress()
1546 {
1547 return this.fromAddress;
1548 }
1549
1550
1551
1552
1553
1554
1555 public String getHostName()
1556 {
1557 if (this.session != null)
1558 {
1559 return this.session.getProperty(EmailConstants.MAIL_HOST);
1560 }
1561 else if (EmailUtils.isNotEmpty(this.hostName))
1562 {
1563 return this.hostName;
1564 }
1565 return null;
1566 }
1567
1568
1569
1570
1571
1572
1573 public String getSmtpPort()
1574 {
1575 if (this.session != null)
1576 {
1577 return this.session.getProperty(EmailConstants.MAIL_PORT);
1578 }
1579 else if (EmailUtils.isNotEmpty(this.smtpPort))
1580 {
1581 return this.smtpPort;
1582 }
1583 return null;
1584 }
1585
1586
1587
1588
1589
1590
1591
1592 public boolean isStartTLSRequired()
1593 {
1594 return this.startTlsRequired;
1595 }
1596
1597
1598
1599
1600
1601
1602
1603 public boolean isStartTLSEnabled()
1604 {
1605 return this.startTlsEnabled || tls;
1606 }
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616 @Deprecated
1617 public boolean isTLS()
1618 {
1619 return isStartTLSEnabled();
1620 }
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630 protected InternetAddress[] toInternetAddressArray(final List<InternetAddress> list)
1631 {
1632 return list.toArray(new InternetAddress[list.size()]);
1633 }
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644 public void setPopBeforeSmtp(
1645 final boolean newPopBeforeSmtp,
1646 final String newPopHost,
1647 final String newPopUsername,
1648 final String newPopPassword)
1649 {
1650 this.popBeforeSmtp = newPopBeforeSmtp;
1651 this.popHost = newPopHost;
1652 this.popUsername = newPopUsername;
1653 this.popPassword = newPopPassword;
1654 }
1655
1656
1657
1658
1659
1660
1661
1662
1663 @Deprecated
1664 public boolean isSSL()
1665 {
1666 return isSSLOnConnect();
1667 }
1668
1669
1670
1671
1672
1673
1674
1675 public boolean isSSLOnConnect()
1676 {
1677 return sslOnConnect || ssl;
1678 }
1679
1680
1681
1682
1683
1684
1685
1686
1687 @Deprecated
1688 public void setSSL(final boolean ssl)
1689 {
1690 setSSLOnConnect(ssl);
1691 }
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704 public Email setSSLOnConnect(final boolean ssl)
1705 {
1706 checkSessionAlreadyInitialized();
1707 this.sslOnConnect = ssl;
1708 this.ssl = ssl;
1709 return this;
1710 }
1711
1712
1713
1714
1715
1716
1717
1718 public boolean isSSLCheckServerIdentity()
1719 {
1720 return sslCheckServerIdentity;
1721 }
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731 public Email setSSLCheckServerIdentity(final boolean sslCheckServerIdentity)
1732 {
1733 checkSessionAlreadyInitialized();
1734 this.sslCheckServerIdentity = sslCheckServerIdentity;
1735 return this;
1736 }
1737
1738
1739
1740
1741
1742
1743 public String getSslSmtpPort()
1744 {
1745 if (this.session != null)
1746 {
1747 return this.session.getProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_PORT);
1748 }
1749 else if (EmailUtils.isNotEmpty(this.sslSmtpPort))
1750 {
1751 return this.sslSmtpPort;
1752 }
1753 return null;
1754 }
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764 public void setSslSmtpPort(final String sslSmtpPort)
1765 {
1766 checkSessionAlreadyInitialized();
1767 this.sslSmtpPort = sslSmtpPort;
1768 }
1769
1770
1771
1772
1773
1774
1775
1776 public boolean isSendPartial()
1777 {
1778 return sendPartial;
1779 }
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795 public Email setSendPartial(final boolean sendPartial)
1796 {
1797 checkSessionAlreadyInitialized();
1798 this.sendPartial = sendPartial;
1799 return this;
1800 }
1801
1802
1803
1804
1805
1806
1807 public List<InternetAddress> getToAddresses()
1808 {
1809 return this.toList;
1810 }
1811
1812
1813
1814
1815
1816
1817 public List<InternetAddress> getCcAddresses()
1818 {
1819 return this.ccList;
1820 }
1821
1822
1823
1824
1825
1826
1827 public List<InternetAddress> getBccAddresses()
1828 {
1829 return this.bccList;
1830 }
1831
1832
1833
1834
1835
1836
1837 public List<InternetAddress> getReplyToAddresses()
1838 {
1839 return this.replyList;
1840 }
1841
1842
1843
1844
1845
1846
1847
1848 public int getSocketConnectionTimeout()
1849 {
1850 return this.socketConnectionTimeout;
1851 }
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861 public void setSocketConnectionTimeout(final int socketConnectionTimeout)
1862 {
1863 checkSessionAlreadyInitialized();
1864 this.socketConnectionTimeout = socketConnectionTimeout;
1865 }
1866
1867
1868
1869
1870
1871
1872
1873 public int getSocketTimeout()
1874 {
1875 return this.socketTimeout;
1876 }
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886 public void setSocketTimeout(final int socketTimeout)
1887 {
1888 checkSessionAlreadyInitialized();
1889 this.socketTimeout = socketTimeout;
1890 }
1891
1892
1893
1894
1895
1896
1897
1898
1899 protected MimeMessage createMimeMessage(final Session aSession)
1900 {
1901 return new MimeMessage(aSession);
1902 }
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912 private String createFoldedHeaderValue(final String name, final String value)
1913 {
1914 if (EmailUtils.isEmpty(name))
1915 {
1916 throw new IllegalArgumentException("name can not be null or empty");
1917 }
1918 if (value == null || EmailUtils.isEmpty(value))
1919 {
1920 throw new IllegalArgumentException("value can not be null or empty");
1921 }
1922
1923 try
1924 {
1925 return MimeUtility.fold(name.length() + 2, MimeUtility.encodeText(value, this.charset, null));
1926 }
1927 catch (final UnsupportedEncodingException e)
1928 {
1929 return value;
1930 }
1931 }
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942 private InternetAddress createInternetAddress(final String email, final String name, final String charsetName)
1943 throws EmailException
1944 {
1945 InternetAddress address;
1946
1947 try
1948 {
1949 address = new InternetAddress(new IDNEmailAddressConverter().toASCII(email));
1950
1951
1952 if (EmailUtils.isNotEmpty(name))
1953 {
1954
1955 if (EmailUtils.isEmpty(charsetName))
1956 {
1957 address.setPersonal(name);
1958 }
1959 else
1960 {
1961
1962
1963 final Charset set = Charset.forName(charsetName);
1964 address.setPersonal(name, set.name());
1965 }
1966 }
1967
1968
1969
1970 address.validate();
1971 }
1972 catch (final AddressException e)
1973 {
1974 throw new EmailException(e);
1975 }
1976 catch (final UnsupportedEncodingException e)
1977 {
1978 throw new EmailException(e);
1979 }
1980 return address;
1981 }
1982
1983
1984
1985
1986
1987
1988
1989
1990 private void checkSessionAlreadyInitialized()
1991 {
1992 if (this.session != null)
1993 {
1994 throw new IllegalStateException("The mail session is already initialized");
1995 }
1996 }
1997 }