|
| 1 | +package com.baeldung.java.io.simplemail; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.simplejavamail.api.email.AttachmentResource; |
| 7 | +import org.simplejavamail.api.email.Email; |
| 8 | +import org.simplejavamail.api.email.Recipient; |
| 9 | +import org.simplejavamail.api.mailer.Mailer; |
| 10 | +import org.simplejavamail.email.EmailBuilder; |
| 11 | +import org.simplejavamail.mailer.MailerBuilder; |
| 12 | +import org.simplejavamail.MailException; |
| 13 | + |
| 14 | +import jakarta.activation.FileDataSource; |
| 15 | +import jakarta.mail.Message; |
| 16 | + |
| 17 | +public class SimpleMailExample { |
| 18 | + |
| 19 | + public static void sendPlainTextEmail() { |
| 20 | + Email email = EmailBuilder.startingBlank() |
| 21 | + .from("sender@example.com") |
| 22 | + .to("recipient@example.com") |
| 23 | + .withSubject("Email with Plain Text!") |
| 24 | + .withPlainText("This is a test email sent using SJM.") |
| 25 | + .buildEmail(); |
| 26 | + sendEmail(email); |
| 27 | + } |
| 28 | + |
| 29 | + public static void sendPlainTextEmailToMultipleRecipient() { |
| 30 | + Email email = EmailBuilder.startingBlank() |
| 31 | + .from("sender@example.com") |
| 32 | + .to("recipient1@example.com, recipient2@example.com, recipient3@example.com") |
| 33 | + .withSubject("Email with Plain Text!") |
| 34 | + .withPlainText("This is a test email sent using SJM to multiple recipients.") |
| 35 | + .buildEmail(); |
| 36 | + sendEmail(email); |
| 37 | + } |
| 38 | + |
| 39 | + public static void sendEmailWithAttachment() { |
| 40 | + Email email = EmailBuilder.startingBlank() |
| 41 | + .from("sender@example.com") |
| 42 | + .to("recipient@example.com") |
| 43 | + .withSubject("Email with Plain Text and Attachment!") |
| 44 | + .withPlainText("This is a test email with attachment sent using SJM.") |
| 45 | + .withAttachment("important_document.pdf", new FileDataSource("path/to/important_document.pdf")) |
| 46 | + .buildEmail(); |
| 47 | + sendEmail(email); |
| 48 | + } |
| 49 | + |
| 50 | + public static void sendEmailWithMultipleAttachment() { |
| 51 | + List<AttachmentResource> arList = new ArrayList<>(); |
| 52 | + arList.add(new AttachmentResource("important_document.pdf", new FileDataSource("path/to/important_document.pdf"))); |
| 53 | + arList.add(new AttachmentResource("company_logo.png", new FileDataSource("path/to/company_logo.png"))); |
| 54 | + Email email = EmailBuilder.startingBlank() |
| 55 | + .from("sender@example.com") |
| 56 | + .to("recipient@example.com") |
| 57 | + .withSubject("Email with Plain Text and multiple Attachments!") |
| 58 | + .withPlainText("This is a test email with attachment sent using SJM.") |
| 59 | + .withAttachments(arList) |
| 60 | + .buildEmail(); |
| 61 | + sendEmail(email); |
| 62 | + } |
| 63 | + |
| 64 | + public static void sendHTMLTextWithEmbeddedImageEmail() { |
| 65 | + String htmlContent = "<h1>This is an email with HTML content</h1>" + "<p>This email body contains additional information and formatting.</p>" + |
| 66 | + "<img src=\"cid:company_logo\" alt=\"Company Logo\">"; |
| 67 | + |
| 68 | + Email email = EmailBuilder.startingBlank() |
| 69 | + .from("sender@example.com") |
| 70 | + .to("recipient@example.com") |
| 71 | + .withSubject("Email with HTML and Embedded Image!") |
| 72 | + .withHTMLText(htmlContent) |
| 73 | + .withEmbeddedImage("company_logo", new FileDataSource("path/to/company_logo.png")) |
| 74 | + .buildEmail(); |
| 75 | + sendEmail(email); |
| 76 | + } |
| 77 | + |
| 78 | + public static void replyingToEmail(Email receivedEmail) { |
| 79 | + EmailBuilder.replyingTo(receivedEmail) |
| 80 | + .from("sender@example.com") |
| 81 | + .prependText("This is a Reply Email. Original email included below:") |
| 82 | + .buildEmail(); |
| 83 | + } |
| 84 | + |
| 85 | + public static void forwardingEmail(Email receivedEmail) { |
| 86 | + Email email = EmailBuilder.forwarding(receivedEmail) |
| 87 | + .from("sender@example.com") |
| 88 | + .prependText("This is an Forward Email. See below email:") |
| 89 | + .buildEmail(); |
| 90 | + } |
| 91 | + |
| 92 | + public static void handleExceptionWhenSendingEmail() { |
| 93 | + try { |
| 94 | + sendPlainTextEmail(); |
| 95 | + System.out.println("Email sent successfully!"); |
| 96 | + } catch (MailException e) { |
| 97 | + System.err.println("Error: " + e.getMessage()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static void setCustomHeaderWhenSendingEmail() { |
| 102 | + Email email = EmailBuilder.startingBlank() |
| 103 | + .from("sender@example.com") |
| 104 | + .to("recipient@example.com") |
| 105 | + .withSubject("Email with Custom Header") |
| 106 | + .withPlainText("This is an important message.") |
| 107 | + .withHeader("X-Priority", "1") |
| 108 | + .buildEmail(); |
| 109 | + sendEmail(email); |
| 110 | + } |
| 111 | + |
| 112 | + private static void sendEmailWithDeliveryReadRecipient() { |
| 113 | + Email email = EmailBuilder.startingBlank() |
| 114 | + .from("sender@example.com") |
| 115 | + .to("recipient@example.com") |
| 116 | + .withSubject("Email with Delivery/Read Receipt Configured!") |
| 117 | + .withPlainText("This is an email sending with delivery/read receipt.") |
| 118 | + .withDispositionNotificationTo(new Recipient("name", "address@domain.com", Message.RecipientType.TO)) |
| 119 | + .withReturnReceiptTo(new Recipient("name", "address@domain.com", Message.RecipientType.TO)) |
| 120 | + .buildEmail(); |
| 121 | + |
| 122 | + sendEmail(email); |
| 123 | + } |
| 124 | + |
| 125 | + private static void sendEmail(Email email) { |
| 126 | + Mailer mailer = MailerBuilder.withSMTPServer("smtp.example.com", 25, "username", "password") |
| 127 | + .withMaximumEmailSize(1024 * 1024 * 5) // 5 Megabytes |
| 128 | + .buildMailer(); |
| 129 | + boolean validate = mailer.validate(email); |
| 130 | + if (validate) { |
| 131 | + mailer.sendMail(email); |
| 132 | + } else { |
| 133 | + System.out.println("Invalid email address."); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments