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