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