1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.mail;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.net.MalformedURLException;
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 279300 2005-09-07 11:43:52Z henning $
32   */
33  
34  public class SendWithAttachmentsTest extends BaseEmailTestCase
35  {
36      /** */
37      private MockHtmlEmailConcrete email = null;
38  
39      /**
40       * @param name name
41       */
42      public SendWithAttachmentsTest(String name)
43      {
44          super(name);
45      }
46  
47      /** */
48      protected void setUp()
49      {
50          super.setUp();
51          // reusable objects to be used across multiple tests
52          this.email = new MockHtmlEmailConcrete();
53      }
54  
55      /** */
56      public void testSendNoAttachments()
57      {
58          try
59          {
60              this.getMailServer();
61  
62              String strSubject = "Test HTML Send #1 Subject (w charset)";
63  
64              this.email = new MockHtmlEmailConcrete();
65              this.email.setHostName(this.strTestMailServer);
66              this.email.setSmtpPort(this.getMailServerPort());
67              this.email.setFrom(this.strTestMailFrom);
68              this.email.addTo(this.strTestMailTo);
69  
70              this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
71  
72              this.email.setCharset(Email.ISO_8859_1);
73              this.email.setSubject(strSubject);
74  
75              URL url = new URL(EmailConfiguration.TEST_URL);
76              String cid = this.email.embed(url, "Apache Logo");
77  
78              String strHtmlMsg =
79                  "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
80  
81              this.email.setHtmlMsg(strHtmlMsg);
82              this.email.setTextMsg(
83                  "Your email client does not support HTML emails");
84  
85              this.email.send();
86              this.fakeMailServer.stop();
87  
88              // validate txt message
89              validateSend(
90                  this.fakeMailServer,
91                  strSubject,
92                  this.email.getTextMsg(),
93                  this.email.getFromAddress(),
94                  this.email.getToList(),
95                  this.email.getCcList(),
96                  this.email.getBccList(),
97                  true);
98  
99              // validate html message
100             validateSend(
101                 this.fakeMailServer,
102                 strSubject,
103                 this.email.getHtmlMsg(),
104                 this.email.getFromAddress(),
105                 this.email.getToList(),
106                 this.email.getCcList(),
107                 this.email.getBccList(),
108                 false);
109         }
110 
111         catch (MalformedURLException e)
112         {
113             e.printStackTrace();
114             fail("Unexpected exception thrown");
115         }
116         catch (IOException e)
117         {
118             e.printStackTrace();
119             fail("Failed to save email to output file");
120         }
121         catch (Exception e)
122         {
123             e.printStackTrace();
124             fail("Unexpected exception thrown");
125         }
126 
127     }
128 
129 	/** */
130 	public void testSendWAttachments()
131 	{
132 		EmailAttachment attachment = new EmailAttachment();
133 		File testFile = null;
134 
135 		try
136 		{
137 			/** File to used to test file attachmetns (Must be valid) */
138 			testFile = File.createTempFile("commons-email-testfile", ".txt");
139 		}
140 		catch (IOException e)
141 		{
142 			e.printStackTrace();
143 			fail("Test file cannot be found");
144 		}
145 
146 		// ====================================================================
147 		// Test Success
148 		// ====================================================================
149 		try
150 		{
151 			this.getMailServer();
152 
153 			String strSubject = "Test HTML Send #1 Subject (w charset)";
154 
155 			this.email = new MockHtmlEmailConcrete();
156 			this.email.setHostName(this.strTestMailServer);
157 			this.email.setSmtpPort(this.getMailServerPort());
158 			this.email.setFrom(this.strTestMailFrom);
159 			this.email.addTo(this.strTestMailTo);
160 
161 			/** File to used to test file attachmetns (Must be valid) */
162 			attachment.setName("Test Attachment");
163 			attachment.setDescription("Test Attachment Desc");
164 			attachment.setPath(testFile.getAbsolutePath());
165 			this.email.attach(attachment);
166 
167 			this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
168 
169 			this.email.setCharset(Email.ISO_8859_1);
170 			this.email.setSubject(strSubject);
171 
172 			URL url = new URL(EmailConfiguration.TEST_URL);
173 			String cid = this.email.embed(url, "Apache Logo");
174 
175 			String strHtmlMsg =
176 				"<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
177 
178 			this.email.setHtmlMsg(strHtmlMsg);
179 			this.email.setTextMsg(
180 				"Your email client does not support HTML emails");
181 
182 			this.email.send();
183 			this.fakeMailServer.stop();
184 			// validate txt message
185 			validateSend(
186 				this.fakeMailServer,
187 				strSubject,
188 				this.email.getTextMsg(),
189 				this.email.getFromAddress(),
190 				this.email.getToList(),
191 				this.email.getCcList(),
192 				this.email.getBccList(),
193 				true);
194 
195 			// validate html message
196 			validateSend(
197 				this.fakeMailServer,
198 				strSubject,
199 				this.email.getHtmlMsg(),
200 				this.email.getFromAddress(),
201 				this.email.getToList(),
202 				this.email.getCcList(),
203 				this.email.getBccList(),
204 				false);
205 
206 			// validate attachment
207 			validateSend(
208 				this.fakeMailServer,
209 				strSubject,
210 				attachment.getName(),
211 				this.email.getFromAddress(),
212 				this.email.getToList(),
213 				this.email.getCcList(),
214 				this.email.getBccList(),
215 				false);
216 		}
217 
218 		catch (MalformedURLException e)
219 		{
220 			e.printStackTrace();
221 			fail("Unexpected exception thrown");
222 		}
223 		catch (IOException e)
224 		{
225 			e.printStackTrace();
226 			fail("Failed to save email to output file");
227 		}
228 		catch (Exception e)
229 		{
230 			e.printStackTrace();
231 			fail("Unexpected exception thrown");
232 		}
233     }
234 
235 }