1
1
package org .simplejavamail .email ;
2
2
3
- import org .simplejavamail .internal .util .MimeMessageParser ;
4
3
import org .simplejavamail .internal .util .MiscUtil ;
5
4
6
5
import javax .activation .DataSource ;
7
6
import javax .mail .Message .RecipientType ;
8
- import javax .mail .MessagingException ;
9
- import javax .mail .internet .InternetAddress ;
10
- import javax .mail .internet .MimeMessage ;
11
7
import javax .mail .util .ByteArrayDataSource ;
12
8
import java .io .File ;
13
- import java .io .IOException ;
14
9
import java .io .InputStream ;
15
10
import java .util .*;
16
- import java .util .regex .Pattern ;
17
11
18
- import static java .lang .String .format ;
19
12
import static org .simplejavamail .util .ConfigLoader .Property .*;
20
13
import static org .simplejavamail .util .ConfigLoader .getProperty ;
21
14
import static org .simplejavamail .util .ConfigLoader .hasProperty ;
@@ -84,28 +77,34 @@ public class Email {
84
77
* Constructor, creates all internal lists. Populates default from, reply-to, to, cc and bcc if provided in the config file.
85
78
*/
86
79
public Email () {
80
+ this (true );
81
+ }
82
+
83
+ public Email (boolean readFromDefaults ) {
87
84
recipients = new ArrayList <>();
88
85
embeddedImages = new ArrayList <>();
89
86
attachments = new ArrayList <>();
90
87
headers = new HashMap <>();
91
88
92
- if (hasProperty (DEFAULT_FROM_ADDRESS )) {
93
- setFromAddress ((String ) getProperty (DEFAULT_FROM_NAME ), (String ) getProperty (DEFAULT_FROM_ADDRESS ));
94
- }
95
- if (hasProperty (DEFAULT_REPLYTO_ADDRESS )) {
96
- setReplyToAddress ((String ) getProperty (DEFAULT_REPLYTO_NAME ), (String ) getProperty (DEFAULT_REPLYTO_ADDRESS ));
97
- }
98
- if (hasProperty (DEFAULT_TO_ADDRESS )) {
99
- addRecipient ((String ) getProperty (DEFAULT_TO_NAME ), (String ) getProperty (DEFAULT_TO_ADDRESS ), RecipientType .TO );
100
- }
101
- if (hasProperty (DEFAULT_CC_ADDRESS )) {
102
- addRecipient ((String ) getProperty (DEFAULT_CC_NAME ), (String ) getProperty (DEFAULT_CC_ADDRESS ), RecipientType .CC );
103
- }
104
- if (hasProperty (DEFAULT_BCC_ADDRESS )) {
105
- addRecipient ((String ) getProperty (DEFAULT_BCC_NAME ), (String ) getProperty (DEFAULT_BCC_ADDRESS ), RecipientType .BCC );
106
- }
107
- if (hasProperty (DEFAULT_SUBJECT )) {
108
- setSubject ((String ) getProperty (DEFAULT_SUBJECT ));
89
+ if (readFromDefaults ) {
90
+ if (hasProperty (DEFAULT_FROM_ADDRESS )) {
91
+ setFromAddress ((String ) getProperty (DEFAULT_FROM_NAME ), (String ) getProperty (DEFAULT_FROM_ADDRESS ));
92
+ }
93
+ if (hasProperty (DEFAULT_REPLYTO_ADDRESS )) {
94
+ setReplyToAddress ((String ) getProperty (DEFAULT_REPLYTO_NAME ), (String ) getProperty (DEFAULT_REPLYTO_ADDRESS ));
95
+ }
96
+ if (hasProperty (DEFAULT_TO_ADDRESS )) {
97
+ addRecipient ((String ) getProperty (DEFAULT_TO_NAME ), (String ) getProperty (DEFAULT_TO_ADDRESS ), RecipientType .TO );
98
+ }
99
+ if (hasProperty (DEFAULT_CC_ADDRESS )) {
100
+ addRecipient ((String ) getProperty (DEFAULT_CC_NAME ), (String ) getProperty (DEFAULT_CC_ADDRESS ), RecipientType .CC );
101
+ }
102
+ if (hasProperty (DEFAULT_BCC_ADDRESS )) {
103
+ addRecipient ((String ) getProperty (DEFAULT_BCC_NAME ), (String ) getProperty (DEFAULT_BCC_ADDRESS ), RecipientType .BCC );
104
+ }
105
+ if (hasProperty (DEFAULT_SUBJECT )) {
106
+ setSubject ((String ) getProperty (DEFAULT_SUBJECT ));
107
+ }
109
108
}
110
109
}
111
110
@@ -395,66 +394,4 @@ public String toString() {
395
394
signWithDomainKey (builder .getDkimPrivateKeyInputStream (), builder .getSigningDomain (), builder .getSelector ());
396
395
}
397
396
}
398
-
399
- /*
400
- * Email from MimeMessage
401
- *
402
- * @author Benny Bottema
403
- */
404
-
405
- /**
406
- * Constructor for {@link javax.mail.internet.MimeMessage}.
407
- * <p>
408
- * <strong>Doen add default recipient that may have been provided in a config file.</strong>
409
- *
410
- * @param mimeMessage The MimeMessage from which to create the email.
411
- */
412
- public Email (final MimeMessage mimeMessage ) {
413
- recipients = new ArrayList <>();
414
- embeddedImages = new ArrayList <>();
415
- attachments = new ArrayList <>();
416
- headers = new HashMap <>();
417
-
418
- try {
419
- fillEmailFromMimeMessage (new MimeMessageParser (mimeMessage ).parse ());
420
- } catch (MessagingException | IOException e ) {
421
- throw new EmailException (format (EmailException .PARSE_ERROR_MIMEMESSAGE , e .getMessage ()), e );
422
- }
423
- }
424
-
425
- private void fillEmailFromMimeMessage (final MimeMessageParser parser )
426
- throws MessagingException {
427
- final InternetAddress from = parser .getFrom ();
428
- this .setFromAddress (from .getPersonal (), from .getAddress ());
429
- final InternetAddress replyTo = parser .getReplyTo ();
430
- this .setReplyToAddress (replyTo .getPersonal (), replyTo .getAddress ());
431
- for (final Map .Entry <String , Object > header : parser .getHeaders ().entrySet ()) {
432
- this .addHeader (header .getKey (), header .getValue ());
433
- }
434
- for (final InternetAddress to : parser .getTo ()) {
435
- this .addRecipient (to .getPersonal (), to .getAddress (), RecipientType .TO );
436
- }
437
- //noinspection QuestionableName
438
- for (final InternetAddress cc : parser .getCc ()) {
439
- this .addRecipient (cc .getPersonal (), cc .getAddress (), RecipientType .CC );
440
- }
441
- for (final InternetAddress bcc : parser .getBcc ()) {
442
- this .addRecipient (bcc .getPersonal (), bcc .getAddress (), RecipientType .BCC );
443
- }
444
- this .setSubject (parser .getSubject ());
445
- this .setText (parser .getPlainContent ());
446
- this .setTextHTML (parser .getHtmlContent ());
447
- for (final Map .Entry <String , DataSource > cid : parser .getCidMap ().entrySet ()) {
448
- this .addEmbeddedImage (extractCID (cid .getKey ()), cid .getValue ());
449
- }
450
- for (final Map .Entry <String , DataSource > attachment : parser .getAttachmentList ().entrySet ()) {
451
- this .addAttachment (extractCID (attachment .getKey ()), attachment .getValue ());
452
- }
453
- }
454
-
455
- private static final Pattern MATCH_INSIDE_CIDBRACKETS = Pattern .compile ("<?([^>]*)>?" );
456
-
457
- static String extractCID (final String cid ) {
458
- return (cid != null ) ? MATCH_INSIDE_CIDBRACKETS .matcher (cid ).replaceAll ("$1" ) : null ;
459
- }
460
397
}
0 commit comments