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