Skip to content

Commit 9c4a0b7

Browse files
authored
Bael 7807 (#16400)
* BAEL-7490 read write file in separate thread * Change the to try resources * Update the code to sync with article * Skeleton * Update validation * update send mail * reformat and structure
1 parent ed6d85a commit 9c4a0b7

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

libraries-io/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<artifactId>spring-web</artifactId>
4949
<version>${spring.version}</version>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.simplejavamail</groupId>
53+
<artifactId>simple-java-mail</artifactId>
54+
<version>${simplejavamail.version}</version>
55+
</dependency>
5156
</dependencies>
5257

5358
<build>
@@ -71,6 +76,7 @@
7176
<zip4j.version>2.11.5</zip4j.version>
7277
<opencsv.version>5.9</opencsv.version>
7378
<spring.version>6.1.4</spring.version>
79+
<simplejavamail.version>8.7.0</simplejavamail.version>
7480
</properties>
7581

7682
</project>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)