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   * @version $Id: SendWithAttachmentsTest.java 1606709 2014-06-30 12:26:06Z ggregory $
35   */
36  public class SendWithAttachmentsTest extends AbstractEmailTest
37  {
38      private MockHtmlEmailConcrete email;
39  
40      @Before
41      public void setUpSendWithAttachmentsTest()
42      {
43          // reusable objects to be used across multiple tests
44          this.email = new MockHtmlEmailConcrete();
45      }
46  
47      /**
48       * @throws EmailException on an email error
49       * @throws IOException when sending fails, or a bad URL is used
50       */
51      @Test
52      public void testSendNoAttachments() throws EmailException, IOException
53      {
54          this.getMailServer();
55  
56          final String strSubject = "Test HTML Send #1 Subject (w charset)";
57  
58          this.email = new MockHtmlEmailConcrete();
59          this.email.setHostName(this.strTestMailServer);
60          this.email.setSmtpPort(this.getMailServerPort());
61          this.email.setFrom(this.strTestMailFrom);
62          this.email.addTo(this.strTestMailTo);
63  
64          this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
65  
66          this.email.setCharset(EmailConstants.ISO_8859_1);
67          this.email.setSubject(strSubject);
68  
69          final URL url = new URL(EmailConfiguration.TEST_URL);
70          final String cid = this.email.embed(url, "Apache Logo");
71  
72          final String strHtmlMsg =
73              "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
74  
75          this.email.setHtmlMsg(strHtmlMsg);
76          this.email.setTextMsg(
77              "Your email client does not support HTML emails");
78  
79          this.email.send();
80          this.fakeMailServer.stop();
81  
82          // validate txt message
83          validateSend(
84              this.fakeMailServer,
85              strSubject,
86              this.email.getTextMsg(),
87              this.email.getFromAddress(),
88              this.email.getToAddresses(),
89              this.email.getCcAddresses(),
90              this.email.getBccAddresses(),
91              true);
92  
93          // validate html message
94          validateSend(
95              this.fakeMailServer,
96              strSubject,
97              this.email.getHtmlMsg(),
98              this.email.getFromAddress(),
99              this.email.getToAddresses(),
100             this.email.getCcAddresses(),
101             this.email.getBccAddresses(),
102             false);
103     }
104 
105     /**
106      * @throws EmailException on an email error
107      * @throws IOException when sending fails, or a bad URL is used
108      */
109     @Test
110     public void testSendWAttachments() throws EmailException, IOException
111     {
112         final EmailAttachment attachment = new EmailAttachment();
113         File testFile = null;
114 
115         /** File to used to test file attachments (Must be valid) */
116         testFile = File.createTempFile("commons-email-testfile", ".txt");
117 
118         // ====================================================================
119         // Test Success
120         // ====================================================================
121         this.getMailServer();
122 
123         final String strSubject = "Test HTML Send #1 Subject (w charset)";
124 
125         this.email = new MockHtmlEmailConcrete();
126         this.email.setHostName(this.strTestMailServer);
127         this.email.setSmtpPort(this.getMailServerPort());
128         this.email.setFrom(this.strTestMailFrom);
129         this.email.addTo(this.strTestMailTo);
130 
131         /** File to be used to test file attachments (Must be valid) */
132         /** Use umlaut characters to test if the filename is properly encoded */
133         attachment.setName("Test Attachment - 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 }