Skip to content

File tree

4 files changed

+182
-17
lines changed

4 files changed

+182
-17
lines changed

core-java-modules/core-java-networking-6/pom.xml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414
</parent>
1515

1616
<dependencies>
17-
<dependency>
18-
<groupId>org.springframework</groupId>
19-
<artifactId>spring-web</artifactId>
20-
<version>${springframework.spring-web.version}</version>
21-
</dependency>
22-
<dependency>
23-
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-starter-web</artifactId>
25-
<version>${webflux.version}</version>
26-
</dependency>
2717
<dependency>
2818
<groupId>org.springframework.boot</groupId>
2919
<artifactId>spring-boot-starter-webflux</artifactId>
@@ -44,11 +34,6 @@
4434
<artifactId>async-http-client</artifactId>
4535
<version>${async-http-client.version}</version>
4636
</dependency>
47-
<dependency>
48-
<groupId>org.apache.commons</groupId>
49-
<artifactId>commons-lang3</artifactId>
50-
<version>${commons-lang3.version}</version>
51-
</dependency>
5237
<dependency>
5338
<groupId>com.squareup.okhttp3</groupId>
5439
<artifactId>okhttp</artifactId>
@@ -65,6 +50,16 @@
6550
<artifactId>commons-net</artifactId>
6651
<version>${commons-net.version}</version>
6752
</dependency>
53+
<dependency>
54+
<groupId>org.eclipse.angus</groupId>
55+
<artifactId>angus-mail</artifactId>
56+
<version>${angus-mail.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.icegreen</groupId>
60+
<artifactId>greenmail</artifactId>
61+
<version>${greenmail.version}</version>
62+
</dependency>
6863
<dependency>
6964
<groupId>org.wiremock</groupId>
7065
<artifactId>wiremock</artifactId>
@@ -75,19 +70,29 @@
7570

7671
<build>
7772
<finalName>core-java-networking-6</finalName>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-compiler-plugin</artifactId>
77+
<configuration>
78+
<source>21</source>
79+
<target>21</target>
80+
</configuration>
81+
</plugin>
82+
</plugins>
7883
</build>
7984

8085
<properties>
81-
<springframework.spring-web.version>4.3.4.RELEASE</springframework.spring-web.version>
8286
<apache.httpclient.version>4.5.14</apache.httpclient.version>
83-
<greenmail.version>2.0.0-alpha-3</greenmail.version>
8487
<apache.httpclient5.version>5.3.1</apache.httpclient5.version>
8588
<async-http-client.version>2.4.5</async-http-client.version>
8689
<jakarta.bind.version>2.3.3</jakarta.bind.version>
8790
<apache.httpclient5.version>5.4.2</apache.httpclient5.version>
8891
<okhttp.version>4.12.0</okhttp.version>
8992
<webflux.version>3.4.3</webflux.version>
9093
<commons-net.version>3.8.0</commons-net.version>
94+
<angus-mail.version>2.0.3</angus-mail.version>
95+
<greenmail.version>2.1.3</greenmail.version>
9196
<wiremock.version>3.13.0</wiremock.version>
9297
</properties>
9398

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.baeldung.inlineimagesinemail;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.Properties;
6+
7+
import jakarta.mail.Authenticator;
8+
import jakarta.mail.Message;
9+
import jakarta.mail.MessagingException;
10+
import jakarta.mail.PasswordAuthentication;
11+
import jakarta.mail.Session;
12+
import jakarta.mail.Transport;
13+
import jakarta.mail.internet.InternetAddress;
14+
import jakarta.mail.internet.MimeBodyPart;
15+
import jakarta.mail.internet.MimeMessage;
16+
import jakarta.mail.internet.MimeMultipart;
17+
18+
import com.icegreen.greenmail.util.GreenMail;
19+
20+
public class InlineImage {
21+
22+
private final String USERNAME = "YOUR_SMTP_USERNAME";
23+
private final String PASSWORD = "YOUR_SMTP_PASSWORD";
24+
private final String HOST = "SMTP HOST";
25+
private final String PORT = "SMTP PORT";
26+
27+
public Properties smtpProperties() {
28+
Properties prop = new Properties();
29+
prop.put("mail.smtp.auth", "true");
30+
prop.put("mail.smtp.starttls.enable", "true");
31+
prop.put("mail.smtp.host", HOST);
32+
prop.put("mail.smtp.port", PORT);
33+
prop.put("mail.smtp.user", USERNAME);
34+
prop.put("mail.smtp.password", PASSWORD);
35+
36+
return prop;
37+
}
38+
39+
public Session smtpsession(Properties props) {
40+
final String username = props.getProperty("mail.smtp.user");
41+
final String password = props.getProperty("mail.smtp.password");
42+
43+
Session session = Session.getInstance(props, new Authenticator() {
44+
@Override
45+
protected PasswordAuthentication getPasswordAuthentication() {
46+
return new PasswordAuthentication(username, password);
47+
}
48+
});
49+
50+
return session;
51+
}
52+
53+
public Session smtpsession(GreenMail greenMail) {
54+
55+
Session session = greenMail.getSmtp()
56+
.createSession();
57+
58+
return session;
59+
}
60+
61+
public void sendEmail(Session session, String to, String subject, String body, String filePath) throws MessagingException, IOException {
62+
63+
MimeMessage message = new MimeMessage(session);
64+
message.setFrom(new InternetAddress("[email protected]"));
65+
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
66+
message.setSubject(subject);
67+
68+
MimeBodyPart htmlPart = new MimeBodyPart();
69+
htmlPart.setContent(body, "text/html");
70+
71+
MimeBodyPart imagePart = new MimeBodyPart();
72+
imagePart.attachFile(new File(filePath));
73+
imagePart.setContentID("<image1>");
74+
imagePart.setDisposition(MimeBodyPart.INLINE);
75+
76+
MimeMultipart mimeMultipart = new MimeMultipart("related");
77+
mimeMultipart.addBodyPart(htmlPart);
78+
mimeMultipart.addBodyPart(imagePart);
79+
80+
message.setContent(mimeMultipart);
81+
82+
Transport.send(message);
83+
}
84+
85+
public static void main(String[] args) throws MessagingException, IOException {
86+
InlineImage inlineImage = new InlineImage();
87+
Properties properties = inlineImage.smtpProperties();
88+
Session session = inlineImage.smtpsession(properties);
89+
90+
String to = "[email protected]";
91+
String subject = "Baeldung";
92+
String body = """
93+
<p>Welcome to Baeldung, home of Java and its frameworks.</p>
94+
<img src='cid:image1'></img>
95+
<p> Explore and learn. </p>
96+
""";
97+
String imagePath = "src/main/resources/image/java.png";
98+
99+
inlineImage.sendEmail(session, to, subject, body, imagePath);
100+
}
101+
}
9.62 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.inlineimagesinemail;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import jakarta.mail.BodyPart;
7+
import jakarta.mail.Multipart;
8+
import jakarta.mail.Part;
9+
import jakarta.mail.Session;
10+
import jakarta.mail.internet.MimeMessage;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
import com.icegreen.greenmail.util.GreenMail;
15+
import com.icegreen.greenmail.util.ServerSetupTest;
16+
17+
class InlineImageUnitTest {
18+
19+
@Test
20+
void givenHtmlEmailWithInlineImage_whenSentViaGreenMailSmtp_thenReceivesEmailWithInlineImage() throws Exception {
21+
GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP);
22+
greenMail.start();
23+
InlineImage inlineImage = new InlineImage();
24+
Session session = inlineImage.smtpsession(greenMail);
25+
26+
String to = "receiver@localhost";
27+
String subject = "Test Subject";
28+
String body = """
29+
<p>Welcome to Baeldung, home of Java and its frameworks.</p>
30+
<img src='cid:image1'></img>
31+
<p> Explore and learn. </p>
32+
""";
33+
String imagePath = "src/main/resources/image/java.png";
34+
35+
inlineImage.sendEmail(session, to, subject, body, imagePath);
36+
37+
MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
38+
assertEquals(1, receivedMessages.length);
39+
40+
MimeMessage message = receivedMessages[0];
41+
42+
Multipart multipart = (Multipart) message.getContent();
43+
assertEquals(2, multipart.getCount());
44+
45+
BodyPart htmlPart = multipart.getBodyPart(0);
46+
assertTrue(htmlPart.getContentType()
47+
.contains("text/html"));
48+
String htmlContent = (String) htmlPart.getContent();
49+
assertTrue(htmlContent.contains("cid:image1"));
50+
51+
BodyPart imagePart = multipart.getBodyPart(1);
52+
String contentId = imagePart.getHeader("Content-ID")[0];
53+
assertTrue(contentId.contains("image1") || contentId.contains("<image1>"));
54+
55+
assertEquals(Part.INLINE, imagePart.getDisposition());
56+
greenMail.stop();
57+
}
58+
59+
}

0 commit comments

Comments
 (0)