Skip to content

Commit 7474b5b

Browse files
committed
#156: Added support for file references for adding body content (plaintext, HTML, including prepend and append)
1 parent fb542e5 commit 7474b5b

File tree

7 files changed

+701
-0
lines changed

7 files changed

+701
-0
lines changed

src/main/java/org/simplejavamail/email/EmailException.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class EmailException extends MailException {
1010

1111
static final String NAME_MISSING_FOR_EMBEDDED_IMAGE = "No name given for embedded image nor passed inside the data source";
12+
static final String ERROR_READING_FROM_FILE = "Error reading from file: %s";
1213

1314
EmailException(@SuppressWarnings("SameParameterValue") final String message) {
1415
super(message);

src/main/java/org/simplejavamail/email/EmailPopulatingBuilder.java

+98
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.mail.util.ByteArrayDataSource;
1515
import java.io.ByteArrayInputStream;
1616
import java.io.File;
17+
import java.io.IOException;
1718
import java.io.InputStream;
1819
import java.util.ArrayList;
1920
import java.util.Collection;
@@ -23,6 +24,7 @@
2324
import java.util.Map;
2425
import java.util.Set;
2526

27+
import static java.lang.String.format;
2628
import static java.nio.charset.StandardCharsets.UTF_8;
2729
import static java.util.Arrays.asList;
2830
import static java.util.Collections.singletonList;
@@ -415,6 +417,21 @@ EmailPopulatingBuilder withForward(@Nullable final MimeMessage emailMessageToFor
415417
return this;
416418
}
417419

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+
418435
/**
419436
* Sets the optional email message body in plain text.
420437
* <p>
@@ -424,6 +441,7 @@ EmailPopulatingBuilder withForward(@Nullable final MimeMessage emailMessageToFor
424441
* @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
425442
* would be used instead by the email client.
426443
*
444+
* @see #withPlainText(File)
427445
* @see #prependText(String)
428446
* @see #appendText(String)
429447
*/
@@ -432,30 +450,77 @@ public EmailPopulatingBuilder withPlainText(@Nullable final String text) {
432450
return this;
433451
}
434452

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+
435467
/**
436468
* Prepends text to the current plain text body (or starts it if plain text body is missing).
437469
*
438470
* @param text The plain text to prepend to whatever plain text is already there.
439471
*
472+
* @see #prependText(File)
473+
* @see #appendText(String)
440474
* @see #withPlainText(String)
441475
*/
442476
public EmailPopulatingBuilder prependText(@Nonnull final String text) {
443477
this.text = text + defaultTo(this.text, "");
444478
return this;
445479
}
446480

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+
447495
/**
448496
* Appends text to the current plain text body (or starts it if plain text body is missing).
449497
*
450498
* @param text The plain text to append to whatever plain text is already there.
451499
*
500+
* @see #appendText(File)
501+
* @see #prependText(String)
452502
* @see #withPlainText(String)
453503
*/
454504
public EmailPopulatingBuilder appendText(@Nonnull final String text) {
455505
this.text = defaultTo(this.text, "") + text;
456506
return this;
457507
}
458508

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+
459524
/**
460525
* Sets the optional email message body in HTML text.
461526
* <p>
@@ -465,6 +530,7 @@ public EmailPopulatingBuilder appendText(@Nonnull final String text) {
465530
* @param textHTML HTML text to set as email body (overwrites any previous HTML text body). If no HTML body is included, plain text
466531
* would be used instead by the email client if provided.
467532
*
533+
* @see #withHTMLText(File)
468534
* @see #prependTextHTML(String)
469535
* @see #appendTextHTML(String)
470536
*/
@@ -473,23 +539,55 @@ public EmailPopulatingBuilder withHTMLText(@Nullable final String textHTML) {
473539
return this;
474540
}
475541

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+
476556
/**
477557
* Prepends HTML text to the current HTML text body (or starts it if HTML text body is missing).
478558
*
479559
* @param textHTML The HTML text to prepend to whatever is already there in the body.
480560
*
561+
* @see #prependTextHTML(File)
562+
* @see #appendTextHTML(String)
481563
* @see #withHTMLText(String)
482564
*/
483565
public EmailPopulatingBuilder prependTextHTML(@Nonnull final String textHTML) {
484566
this.textHTML = textHTML + defaultTo(this.textHTML, "");
485567
return this;
486568
}
487569

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+
488584
/**
489585
* Appends HTML text to the current HTML text body (or starts it if HTML text body is missing).
490586
*
491587
* @param textHTML The HTML text to append to whatever is already there in the body.
492588
*
589+
* @see #appendTextHTML(File)
590+
* @see #prependTextHTML(String)
493591
* @see #withHTMLText(String)
494592
*/
495593
public EmailPopulatingBuilder appendTextHTML(@Nonnull final String textHTML) {

src/main/java/org/simplejavamail/internal/clisupport/BuilderApiToPicocliCommandsMapper.java

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.simplejavamail.internal.clisupport.therapijavadoc.TherapiJavadocHelper;
1515
import org.simplejavamail.internal.clisupport.therapijavadoc.TherapiJavadocHelper.DocumentedMethodParam;
1616
import org.simplejavamail.internal.clisupport.valueinterpreters.EmlFilePathToMimeMessageFunction;
17+
import org.simplejavamail.internal.clisupport.valueinterpreters.FilePathIdentityFunction;
1718
import org.simplejavamail.internal.clisupport.valueinterpreters.MsgFilePathToMimeMessageFunction;
1819
import org.simplejavamail.internal.clisupport.valueinterpreters.StringToTransportStrategyFunction;
1920
import org.simplejavamail.internal.util.StringUtil;
@@ -25,6 +26,7 @@
2526
import javax.annotation.Nonnull;
2627
import javax.annotation.Nullable;
2728
import javax.mail.internet.MimeMessage;
29+
import java.io.File;
2830
import java.io.InputStream;
2931
import java.lang.annotation.Annotation;
3032
import java.lang.reflect.Method;
@@ -71,9 +73,11 @@ public final class BuilderApiToPicocliCommandsMapper {
7173
put(DataSource.class, "FILE");
7274
put(byte[].class, "FILE");
7375
put(InputStream.class, "FILE");
76+
put(File.class, "FILE");
7477
}};
7578

7679
static {
80+
ValueConversionHelper.registerValueConverter(new FilePathIdentityFunction());
7781
ValueConversionHelper.registerValueConverter(new EmlFilePathToMimeMessageFunction());
7882
ValueConversionHelper.registerValueConverter(new MsgFilePathToMimeMessageFunction());
7983
ValueConversionHelper.registerValueConverter(new StringToTransportStrategyFunction());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.simplejavamail.internal.clisupport.valueinterpreters;
2+
3+
import javax.annotation.Nonnull;
4+
import java.io.File;
5+
6+
public class FilePathIdentityFunction extends FileBasedFunction<File> {
7+
8+
@Override
9+
public Class<String> getFromType() {
10+
return String.class;
11+
}
12+
13+
@Override
14+
public Class<File> getTargetType() {
15+
return File.class;
16+
}
17+
18+
@Nonnull
19+
@Override
20+
protected File convertFile(File textFile) {
21+
return textFile;
22+
}
23+
}

src/main/java/org/simplejavamail/internal/util/MiscUtil.java

+11
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import javax.mail.internet.MimeUtility;
1111
import java.io.BufferedInputStream;
1212
import java.io.ByteArrayOutputStream;
13+
import java.io.File;
1314
import java.io.IOException;
1415
import java.io.InputStream;
1516
import java.io.UnsupportedEncodingException;
1617
import java.lang.annotation.Annotation;
1718
import java.lang.reflect.Method;
1819
import java.nio.charset.Charset;
20+
import java.nio.file.Files;
1921
import java.util.AbstractMap;
2022
import java.util.ArrayList;
2123
import java.util.Collection;
@@ -24,6 +26,8 @@
2426
import java.util.regex.Pattern;
2527

2628
import static java.lang.Integer.toHexString;
29+
import static java.lang.String.format;
30+
import static java.nio.charset.StandardCharsets.UTF_8;
2731
import static java.util.Arrays.asList;
2832
import static java.util.regex.Pattern.compile;
2933
import static org.bbottema.javareflection.TypeUtils.containsAnnotation;
@@ -185,4 +189,11 @@ public static int countMandatoryParameters(Method m) {
185189
}
186190
return mandatoryParameterCount;
187191
}
192+
193+
public static String readFileContent(@Nonnull final File file) throws IOException {
194+
if (!file.exists()) {
195+
throw new IllegalArgumentException(format("File not found: %s", file));
196+
}
197+
return new String(Files.readAllBytes(file.toPath()), UTF_8);
198+
}
188199
}

src/test/java/demo/CliDemoApp.java

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void main(String[] args) {
1919
demoTestConnection(serverConfig);
2020
demoSend(serverConfig);
2121
demoSendAsync(serverConfig);
22+
demoSendUsingFileAsHTMLBody(serverConfig);
2223
}
2324

2425
private static void demoTestConnection(ServerConfig serverConfig) {
@@ -51,4 +52,16 @@ private static void demoSendAsync(ServerConfig serverConfig) {
5152
"--mailer:withTransportStrategy", "SMTP_TLS"
5253
});
5354
}
55+
56+
private static void demoSendUsingFileAsHTMLBody(ServerConfig serverConfig) {
57+
ModuleLoader.loadCliModule().runCLI(new String[]{
58+
"send",
59+
"--email:startingBlank",
60+
"--email:withHTMLTextFromFile", SOURCE_FOLDER + "/test/resources/test-messages/html-body.html",
61+
"--email:from", "Test sender", YOUR_GMAIL_ADDRESS,
62+
"--email:to", "Test Receiver", YOUR_GMAIL_ADDRESS,
63+
"--mailer:withSMTPServer", "smtp.gmail.com", "587", serverConfig.getUsername(), serverConfig.getPassword(),
64+
"--mailer:withTransportStrategy", "SMTP_TLS"
65+
});
66+
}
5467
}

0 commit comments

Comments
 (0)