View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * JUnit test case for SimpleEmailTest
36   */
37  public class SimpleEmailTest extends AbstractEmailTest {
38  
39      private MockSimpleEmail email;
40  
41      @BeforeEach
42      public void setUpSimpleEmailTest() {
43          // reusable objects to be used across multiple tests
44          email = new MockSimpleEmail();
45      }
46  
47      @Test
48      @Disabled
49      public void testDefaultMimeCharset() throws EmailException, IOException {
50          /*
51           * disabling this test as it is dependent on execution order. MimeUtility.getDefaultMIMECharset does some internal caching and if mail.mime.charset is
52           * not defined, reverts to the default Java charset which is basically the system default file encoding.
53           */
54          System.setProperty(EmailConstants.MAIL_MIME_CHARSET, StandardCharsets.UTF_8.name());
55          // Test Success
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 ä"; // add non-ascii character, otherwise us-ascii will be used
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), // only check the start, the ä will be encoded
79                  email.getFromAddress(), email.getToAddresses(), email.getCcAddresses(), email.getBccAddresses(), true);
80  
81          final String message = getMessageAsString(0);
82          // check that the charset has been correctly set
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          // Test Success
91          for (final String validChar : testCharsValid) {
92              email.setMsg(validChar);
93              assertEquals(validChar, email.getContentAsString());
94          }
95          // Test Exception
96          for (final String invalidChar : testCharsNotValid) {
97              assertThrows(EmailException.class, () -> email.setMsg(invalidChar));
98          }
99  
100     }
101 
102     /**
103      * @throws EmailException when a bad address is set.
104      * @throws IOException    when sending fails TODO Add code to test the popBeforeSmtp() settings
105      */
106     @Test
107     public void testSend() throws EmailException, IOException {
108         // Test Success
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 }