14
14
import javax .mail .util .ByteArrayDataSource ;
15
15
import java .io .ByteArrayInputStream ;
16
16
import java .io .File ;
17
+ import java .io .IOException ;
17
18
import java .io .InputStream ;
18
19
import java .util .ArrayList ;
19
20
import java .util .Collection ;
23
24
import java .util .Map ;
24
25
import java .util .Set ;
25
26
27
+ import static java .lang .String .format ;
26
28
import static java .nio .charset .StandardCharsets .UTF_8 ;
27
29
import static java .util .Arrays .asList ;
28
30
import static java .util .Collections .singletonList ;
@@ -415,6 +417,21 @@ EmailPopulatingBuilder withForward(@Nullable final MimeMessage emailMessageToFor
415
417
return this ;
416
418
}
417
419
420
+ /**
421
+ * Delegates to {@link #withPlainText(String)}.
422
+ *
423
+ * @param textFile Plain text to set as email body (overwrites any previous plain text body). If no HTML body is included as well, plain text
424
+ * would be used instead by the email client.
425
+ */
426
+ @ Cli .OptionNameOverride ("withPlainTextFromFile" )
427
+ public EmailPopulatingBuilder withPlainText (@ Nonnull final File textFile ) {
428
+ try {
429
+ return withPlainText (MiscUtil .readFileContent (textFile ));
430
+ } catch (IOException e ) {
431
+ throw new EmailException (format (EmailException .ERROR_READING_FROM_FILE , textFile ), e );
432
+ }
433
+ }
434
+
418
435
/**
419
436
* Sets the optional email message body in plain text.
420
437
* <p>
@@ -424,6 +441,7 @@ EmailPopulatingBuilder withForward(@Nullable final MimeMessage emailMessageToFor
424
441
* @param text Plain text to set as email body (overwrites any previous plain text body). If no HTML body is included as well, plain text
425
442
* would be used instead by the email client.
426
443
*
444
+ * @see #withPlainText(File)
427
445
* @see #prependText(String)
428
446
* @see #appendText(String)
429
447
*/
@@ -432,30 +450,77 @@ public EmailPopulatingBuilder withPlainText(@Nullable final String text) {
432
450
return this ;
433
451
}
434
452
453
+ /**
454
+ * Delegates to {@link #prependText(String)}.
455
+ *
456
+ * @param textFile The plain text to prepend to whatever plain text is already there.
457
+ */
458
+ @ Cli .OptionNameOverride ("prependTextFromFile" )
459
+ public EmailPopulatingBuilder prependText (@ Nonnull final File textFile ) {
460
+ try {
461
+ return prependText (MiscUtil .readFileContent (textFile ));
462
+ } catch (IOException e ) {
463
+ throw new EmailException (format (EmailException .ERROR_READING_FROM_FILE , textFile ), e );
464
+ }
465
+ }
466
+
435
467
/**
436
468
* Prepends text to the current plain text body (or starts it if plain text body is missing).
437
469
*
438
470
* @param text The plain text to prepend to whatever plain text is already there.
439
471
*
472
+ * @see #prependText(File)
473
+ * @see #appendText(String)
440
474
* @see #withPlainText(String)
441
475
*/
442
476
public EmailPopulatingBuilder prependText (@ Nonnull final String text ) {
443
477
this .text = text + defaultTo (this .text , "" );
444
478
return this ;
445
479
}
446
480
481
+ /**
482
+ * Delegates to {@link #appendText(String)}.
483
+ *
484
+ * @param textFile The plain text to append to whatever plain text is already there.
485
+ */
486
+ @ Cli .OptionNameOverride ("appendTextFromFile" )
487
+ public EmailPopulatingBuilder appendText (@ Nonnull final File textFile ) {
488
+ try {
489
+ return appendText (MiscUtil .readFileContent (textFile ));
490
+ } catch (IOException e ) {
491
+ throw new EmailException (format (EmailException .ERROR_READING_FROM_FILE , textFile ), e );
492
+ }
493
+ }
494
+
447
495
/**
448
496
* Appends text to the current plain text body (or starts it if plain text body is missing).
449
497
*
450
498
* @param text The plain text to append to whatever plain text is already there.
451
499
*
500
+ * @see #appendText(File)
501
+ * @see #prependText(String)
452
502
* @see #withPlainText(String)
453
503
*/
454
504
public EmailPopulatingBuilder appendText (@ Nonnull final String text ) {
455
505
this .text = defaultTo (this .text , "" ) + text ;
456
506
return this ;
457
507
}
458
508
509
+ /**
510
+ * Delegates to {@link #withHTMLText(String)}.
511
+ *
512
+ * @param textHTMLFile HTML text to set as email body (overwrites any previous HTML text body). If no HTML body is included, plain text
513
+ * would be used instead by the email client if provided.
514
+ */
515
+ @ Cli .OptionNameOverride ("withHTMLTextFromFile" )
516
+ public EmailPopulatingBuilder withHTMLText (@ Nonnull final File textHTMLFile ) {
517
+ try {
518
+ return withHTMLText (MiscUtil .readFileContent (textHTMLFile ));
519
+ } catch (IOException e ) {
520
+ throw new EmailException (format (EmailException .ERROR_READING_FROM_FILE , textHTMLFile ), e );
521
+ }
522
+ }
523
+
459
524
/**
460
525
* Sets the optional email message body in HTML text.
461
526
* <p>
@@ -465,6 +530,7 @@ public EmailPopulatingBuilder appendText(@Nonnull final String text) {
465
530
* @param textHTML HTML text to set as email body (overwrites any previous HTML text body). If no HTML body is included, plain text
466
531
* would be used instead by the email client if provided.
467
532
*
533
+ * @see #withHTMLText(File)
468
534
* @see #prependTextHTML(String)
469
535
* @see #appendTextHTML(String)
470
536
*/
@@ -473,23 +539,55 @@ public EmailPopulatingBuilder withHTMLText(@Nullable final String textHTML) {
473
539
return this ;
474
540
}
475
541
542
+ /**
543
+ * Delegates to {@link #prependTextHTML(String)}.
544
+ *
545
+ * @param textHTMLFile The HTML text to prepend to whatever is already there in the body.
546
+ */
547
+ @ Cli .OptionNameOverride ("prependTextHTMLFromFile" )
548
+ public EmailPopulatingBuilder prependTextHTML (@ Nonnull final File textHTMLFile ) {
549
+ try {
550
+ return prependTextHTML (MiscUtil .readFileContent (textHTMLFile ));
551
+ } catch (IOException e ) {
552
+ throw new EmailException (format (EmailException .ERROR_READING_FROM_FILE , textHTMLFile ), e );
553
+ }
554
+ }
555
+
476
556
/**
477
557
* Prepends HTML text to the current HTML text body (or starts it if HTML text body is missing).
478
558
*
479
559
* @param textHTML The HTML text to prepend to whatever is already there in the body.
480
560
*
561
+ * @see #prependTextHTML(File)
562
+ * @see #appendTextHTML(String)
481
563
* @see #withHTMLText(String)
482
564
*/
483
565
public EmailPopulatingBuilder prependTextHTML (@ Nonnull final String textHTML ) {
484
566
this .textHTML = textHTML + defaultTo (this .textHTML , "" );
485
567
return this ;
486
568
}
487
569
570
+ /**
571
+ * Delegates to {@link #appendTextHTML(String)}.
572
+ *
573
+ * @param textHTMLFile The HTML text to append to whatever is already there in the body.
574
+ */
575
+ @ Cli .OptionNameOverride ("appendTextHTMLFromFile" )
576
+ public EmailPopulatingBuilder appendTextHTML (@ Nonnull final File textHTMLFile ) {
577
+ try {
578
+ return appendTextHTML (MiscUtil .readFileContent (textHTMLFile ));
579
+ } catch (IOException e ) {
580
+ throw new EmailException (format (EmailException .ERROR_READING_FROM_FILE , textHTMLFile ), e );
581
+ }
582
+ }
583
+
488
584
/**
489
585
* Appends HTML text to the current HTML text body (or starts it if HTML text body is missing).
490
586
*
491
587
* @param textHTML The HTML text to append to whatever is already there in the body.
492
588
*
589
+ * @see #appendTextHTML(File)
590
+ * @see #prependTextHTML(String)
493
591
* @see #withHTMLText(String)
494
592
*/
495
593
public EmailPopulatingBuilder appendTextHTML (@ Nonnull final String textHTML ) {
0 commit comments