1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail2.jakarta;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25
26 import org.apache.commons.mail2.core.EmailConstants;
27 import org.apache.commons.mail2.core.EmailException;
28 import org.apache.commons.mail2.core.EmailUtils;
29 import org.apache.commons.mail2.jakarta.mocks.MockSimpleEmail;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Disabled;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class SimpleEmailTest extends AbstractEmailTest {
38
39 private MockSimpleEmail email;
40
41 @BeforeEach
42 public void setUpSimpleEmailTest() {
43
44 email = new MockSimpleEmail();
45 }
46
47 @Test
48 @Disabled
49 public void testDefaultMimeCharset() throws EmailException, IOException {
50
51
52
53
54 System.setProperty(EmailConstants.MAIL_MIME_CHARSET, StandardCharsets.UTF_8.name());
55
56 getMailServer();
57
58 email = new MockSimpleEmail();
59 email.setHostName(strTestMailServer);
60 email.setSmtpPort(getMailServerPort());
61 email.setFrom(strTestMailFrom);
62 email.addTo(strTestMailTo);
63
64 if (strTestUser != null && strTestPasswd != null) {
65 email.setAuthentication(strTestUser, strTestPasswd);
66 }
67
68 final String strSubject = "Test Msg Subject";
69 final String strMessage = "Test Msg Body ä";
70
71 email.setSubject(strSubject);
72 email.setMsg(strMessage);
73
74 email.send();
75
76 fakeMailServer.stop();
77
78 validateSend(fakeMailServer, strSubject, email.getContentAsString().substring(0, 13),
79 email.getFromAddress(), email.getToAddresses(), email.getCcAddresses(), email.getBccAddresses(), true);
80
81 final String message = getMessageAsString(0);
82
83 assertTrue(EmailUtils.toLower(message).contains("content-type: text/plain; charset=utf-8"));
84
85 System.clearProperty(EmailConstants.MAIL_MIME_CHARSET);
86 }
87
88 @Test
89 public void testGetSetMsg() throws EmailException {
90
91 for (final String validChar : testCharsValid) {
92 email.setMsg(validChar);
93 assertEquals(validChar, email.getContentAsString());
94 }
95
96 for (final String invalidChar : testCharsNotValid) {
97 assertThrows(EmailException.class, () -> email.setMsg(invalidChar));
98 }
99
100 }
101
102
103
104
105
106 @Test
107 public void testSend() throws EmailException, IOException {
108
109 getMailServer();
110
111 email = new MockSimpleEmail();
112 email.setHostName(strTestMailServer);
113 email.setSmtpPort(getMailServerPort());
114 email.setFrom(strTestMailFrom);
115 email.addTo(strTestMailTo);
116
117 if (strTestUser != null && strTestPasswd != null) {
118 email.setAuthentication(strTestUser, strTestPasswd);
119 }
120
121 final String strSubject = "Test Msg Subject";
122 final String strMessage = "Test Msg Body";
123
124 email.setCharset(EmailConstants.ISO_8859_1);
125 email.setSubject(strSubject);
126
127 email.setMsg(strMessage);
128
129 email.send();
130
131 fakeMailServer.stop();
132 validateSend(fakeMailServer, strSubject, email.getContentAsString(), email.getFromAddress(), email.getToAddresses(), email.getCcAddresses(),
133 email.getBccAddresses(), true);
134 }
135 }