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 java.io.File;
20  import java.io.IOException;
21  import java.net.URL;
22  
23  import org.apache.commons.mail2.core.EmailConstants;
24  import org.apache.commons.mail2.core.EmailException;
25  import org.apache.commons.mail2.jakarta.mocks.MockHtmlEmailConcrete;
26  import org.apache.commons.mail2.jakarta.settings.EmailConfiguration;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  import jakarta.mail.internet.MimeUtility;
31  
32  /**
33   * JUnit test case verifying bugzilla issue 30973 is fixed.
34   */
35  public class SendWithAttachmentsTest extends AbstractEmailTest {
36      private MockHtmlEmailConcrete email;
37  
38      @BeforeEach
39      public void setUpSendWithAttachmentsTest() {
40          // reusable objects to be used across multiple tests
41          email = new MockHtmlEmailConcrete();
42      }
43  
44      /**
45       * @throws EmailException on an email error
46       * @throws IOException    when sending fails, or a bad URL is used
47       */
48      @Test
49      public void testSendNoAttachments() throws EmailException, IOException {
50          getMailServer();
51  
52          final String strSubject = "Test HTML Send #1 Subject (w charset)";
53  
54          email = new MockHtmlEmailConcrete();
55          email.setHostName(strTestMailServer);
56          email.setSmtpPort(getMailServerPort());
57          email.setFrom(strTestMailFrom);
58          email.addTo(strTestMailTo);
59  
60          email.setAuthentication(strTestUser, strTestPasswd);
61  
62          email.setCharset(EmailConstants.ISO_8859_1);
63          email.setSubject(strSubject);
64  
65          final URL url = new URL(EmailConfiguration.TEST_URL);
66          final String cid = email.embed(url, "Apache Logo");
67  
68          final String strHtmlMsg = "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
69  
70          email.setHtmlMsg(strHtmlMsg);
71          email.setTextMsg("Your email client does not support HTML emails");
72  
73          email.send();
74          fakeMailServer.stop();
75  
76          // validate txt message
77          validateSend(fakeMailServer, strSubject, email.getText(), email.getFromAddress(), email.getToAddresses(), email.getCcAddresses(),
78                  email.getBccAddresses(), true);
79  
80          // validate html message
81          validateSend(fakeMailServer, strSubject, email.getHtml(), email.getFromAddress(), email.getToAddresses(), email.getCcAddresses(),
82                  email.getBccAddresses(), false);
83      }
84  
85      /**
86       * @throws EmailException on an email error
87       * @throws IOException    when sending fails, or a bad URL is used
88       */
89      @Test
90      public void testSendWAttachments() throws EmailException, IOException {
91          final EmailAttachment attachment = new EmailAttachment();
92  
93          /** File to used to test file attachments (Must be valid) */
94          final File testFile = File.createTempFile("commons-email-testfile", ".txt");
95          // Test Success
96          getMailServer();
97  
98          final String strSubject = "Test HTML Send #1 Subject (w charset)";
99  
100         email = new MockHtmlEmailConcrete();
101         email.setHostName(strTestMailServer);
102         email.setSmtpPort(getMailServerPort());
103         email.setFrom(strTestMailFrom);
104         email.addTo(strTestMailTo);
105 
106         /** File to be used to test file attachments (Must be valid) */
107         /** Use umlaut characters to test if the file name is properly encoded */
108         // use short name to avoid folding. Otherwise need to unfold when checking result.
109         attachment.setName("a>ä, o>ö, u>ü, au>äu");
110         attachment.setDescription("Test Attachment Desc");
111         attachment.setPath(testFile.getAbsolutePath());
112         email.attach(attachment);
113 
114         email.setAuthentication(strTestUser, strTestPasswd);
115 
116         email.setCharset(EmailConstants.ISO_8859_1);
117         email.setSubject(strSubject);
118 
119         final String strHtmlMsg = "<html>Test Message<html>";
120 
121         email.setHtmlMsg(strHtmlMsg);
122         email.setTextMsg("Your email client does not support HTML emails");
123 
124         email.send();
125         fakeMailServer.stop();
126         // validate txt message
127         validateSend(fakeMailServer, strSubject, email.getText(), email.getFromAddress(), email.getToAddresses(), email.getCcAddresses(),
128                 email.getBccAddresses(), true);
129 
130         // validate html message
131         validateSend(fakeMailServer, strSubject, email.getHtml(), email.getFromAddress(), email.getToAddresses(), email.getCcAddresses(),
132                 email.getBccAddresses(), false);
133 
134         // validate attachment
135         validateSend(fakeMailServer, strSubject, MimeUtility.encodeText(attachment.getName()), email.getFromAddress(), email.getToAddresses(),
136                 email.getCcAddresses(), email.getBccAddresses(), false);
137     }
138 
139 }