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