arList = new ArrayList<>();
+ arList.add(new AttachmentResource("important_document.pdf", new FileDataSource("path/to/important_document.pdf")));
+ arList.add(new AttachmentResource("company_logo.png", new FileDataSource("path/to/company_logo.png")));
+ Email email = EmailBuilder.startingBlank()
+ .from("sender@example.com")
+ .to("recipient@example.com")
+ .withSubject("Email with Plain Text and multiple Attachments!")
+ .withPlainText("This is a test email with attachment sent using SJM.")
+ .withAttachments(arList)
+ .buildEmail();
+ sendEmail(email);
+ }
+
+ public static void sendHTMLTextWithEmbeddedImageEmail() {
+ String htmlContent = "This is an email with HTML content
" + "This email body contains additional information and formatting.
" +
+ "
";
+
+ Email email = EmailBuilder.startingBlank()
+ .from("sender@example.com")
+ .to("recipient@example.com")
+ .withSubject("Email with HTML and Embedded Image!")
+ .withHTMLText(htmlContent)
+ .withEmbeddedImage("company_logo", new FileDataSource("path/to/company_logo.png"))
+ .buildEmail();
+ sendEmail(email);
+ }
+
+ public static void replyingToEmail(Email receivedEmail) {
+ EmailBuilder.replyingTo(receivedEmail)
+ .from("sender@example.com")
+ .prependText("This is a Reply Email. Original email included below:")
+ .buildEmail();
+ }
+
+ public static void forwardingEmail(Email receivedEmail) {
+ Email email = EmailBuilder.forwarding(receivedEmail)
+ .from("sender@example.com")
+ .prependText("This is an Forward Email. See below email:")
+ .buildEmail();
+ }
+
+ public static void handleExceptionWhenSendingEmail() {
+ try {
+ sendPlainTextEmail();
+ System.out.println("Email sent successfully!");
+ } catch (MailException e) {
+ System.err.println("Error: " + e.getMessage());
+ }
+ }
+
+ public static void setCustomHeaderWhenSendingEmail() {
+ Email email = EmailBuilder.startingBlank()
+ .from("sender@example.com")
+ .to("recipient@example.com")
+ .withSubject("Email with Custom Header")
+ .withPlainText("This is an important message.")
+ .withHeader("X-Priority", "1")
+ .buildEmail();
+ sendEmail(email);
+ }
+
+ private static void sendEmailWithDeliveryReadRecipient() {
+ Email email = EmailBuilder.startingBlank()
+ .from("sender@example.com")
+ .to("recipient@example.com")
+ .withSubject("Email with Delivery/Read Receipt Configured!")
+ .withPlainText("This is an email sending with delivery/read receipt.")
+ .withDispositionNotificationTo(new Recipient("name", "address@domain.com", Message.RecipientType.TO))
+ .withReturnReceiptTo(new Recipient("name", "address@domain.com", Message.RecipientType.TO))
+ .buildEmail();
+
+ sendEmail(email);
+ }
+
+ private static void sendEmail(Email email) {
+ Mailer mailer = MailerBuilder.withSMTPServer("smtp.example.com", 25, "username", "password")
+ .withMaximumEmailSize(1024 * 1024 * 5) // 5 Megabytes
+ .buildMailer();
+ boolean validate = mailer.validate(email);
+ if (validate) {
+ mailer.sendMail(email);
+ } else {
+ System.out.println("Invalid email address.");
+ }
+ }
+}