1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.mail;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.URL;
21
22 import org.apache.commons.mail.mocks.MockHtmlEmailConcrete;
23 import org.apache.commons.mail.settings.EmailConfiguration;
24
25
26
27
28
29
30
31
32
33 public class HtmlEmailTest extends BaseEmailTestCase
34 {
35
36 private MockHtmlEmailConcrete email = null;
37
38
39
40
41 public HtmlEmailTest(String name)
42 {
43 super(name);
44 }
45
46
47 protected void setUp()
48 {
49 super.setUp();
50
51 this.email = new MockHtmlEmailConcrete();
52 }
53
54
55 public void testGetSetTextMsg()
56 {
57
58
59
60 try
61 {
62 for (int i = 0; i < testCharsValid.length; i++)
63 {
64 this.email.setTextMsg(testCharsValid[i]);
65 assertEquals(testCharsValid[i], this.email.getTextMsg());
66 }
67 }
68 catch (EmailException e)
69 {
70 fail("Shoudn't have thrown exception");
71 }
72
73
74
75
76 for (int i = 0; i < this.testCharsNotValid.length; i++)
77 {
78 try
79 {
80 this.email.setTextMsg(this.testCharsNotValid[i]);
81 fail("Should have thrown an exception");
82 }
83 catch (EmailException e)
84 {
85 assertTrue(true);
86 }
87 catch (Exception e)
88 {
89 fail("Unexpected exception thrown");
90 }
91 }
92
93 }
94
95
96 public void testGetSetHtmlMsg()
97 {
98
99
100
101 try
102 {
103 for (int i = 0; i < testCharsValid.length; i++)
104 {
105 this.email.setHtmlMsg(testCharsValid[i]);
106 assertEquals(testCharsValid[i], this.email.getHtmlMsg());
107 }
108 }
109 catch (EmailException e)
110 {
111 fail("Shoudn't have thrown exception");
112 }
113
114
115
116
117 for (int i = 0; i < this.testCharsNotValid.length; i++)
118 {
119 try
120 {
121 this.email.setHtmlMsg(this.testCharsNotValid[i]);
122 fail("Should have thrown an exception");
123 }
124 catch (EmailException e)
125 {
126 assertTrue(true);
127 }
128 catch (Exception e)
129 {
130 fail("Unexpected exception thrown");
131 }
132 }
133
134 }
135
136
137 public void testGetSetMsg()
138 {
139
140
141
142 try
143 {
144 for (int i = 0; i < testCharsValid.length; i++)
145 {
146 this.email.setMsg(testCharsValid[i]);
147 assertEquals(testCharsValid[i], this.email.getTextMsg());
148
149 assertTrue(
150 this.email.getHtmlMsg().indexOf(testCharsValid[i]) != -1);
151 }
152 }
153 catch (EmailException e)
154 {
155 fail("Shoudn't have thrown exception");
156 }
157
158
159
160
161 for (int i = 0; i < this.testCharsNotValid.length; i++)
162 {
163 try
164 {
165 this.email.setMsg(this.testCharsNotValid[i]);
166 fail("Should have thrown an exception");
167 }
168 catch (EmailException e)
169 {
170 assertTrue(true);
171 }
172 catch (Exception e)
173 {
174 fail("Unexpected exception thrown");
175 }
176 }
177
178 }
179
180
181
182
183
184 public void testEmbed() throws Exception
185 {
186
187
188
189
190 String strEmbed =
191 this.email.embed(new URL(this.strTestURL), "Test name");
192 assertNotNull(strEmbed);
193 assertEquals(HtmlEmail.CID_LENGTH, strEmbed.length());
194
195
196
197
198
199 try
200 {
201 this.email.embed(new URL("http://bad.url"), "Test name");
202 fail("Should have thrown an exception");
203 }
204 catch (EmailException e)
205 {
206 assertTrue(true);
207 }
208 catch (Exception e)
209 {
210 fail("Unexpected exception thrown");
211 }
212 }
213
214
215 public void testSend()
216 {
217 EmailAttachment attachment = new EmailAttachment();
218 File testFile = null;
219
220 try
221 {
222
223 testFile = File.createTempFile("commons-email-testfile", ".txt");
224 }
225 catch (IOException e)
226 {
227 fail("Test file cannot be found");
228 }
229
230
231
232
233 try
234 {
235 this.getMailServer();
236
237 String strSubject = "Test HTML Send #1 Subject (w charset)";
238
239 this.email = new MockHtmlEmailConcrete();
240 this.email.setHostName(this.strTestMailServer);
241 this.email.setSmtpPort(this.getMailServerPort());
242 this.email.setFrom(this.strTestMailFrom);
243 this.email.addTo(this.strTestMailTo);
244
245
246 attachment.setName("Test Attachment");
247 attachment.setDescription("Test Attachment Desc");
248 attachment.setPath(testFile.getAbsolutePath());
249 this.email.attach(attachment);
250
251 this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
252
253 this.email.setCharset(Email.ISO_8859_1);
254 this.email.setSubject(strSubject);
255
256 URL url = new URL(EmailConfiguration.TEST_URL);
257 String cid = this.email.embed(url, "Apache Logo");
258
259 String strHtmlMsg =
260 "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
261
262 this.email.setHtmlMsg(strHtmlMsg);
263 this.email.setTextMsg(
264 "Your email client does not support HTML emails");
265
266 this.email.send();
267 this.fakeMailServer.stop();
268
269 validateSend(
270 this.fakeMailServer,
271 strSubject,
272 this.email.getTextMsg(),
273 this.email.getFromAddress(),
274 this.email.getToList(),
275 this.email.getCcList(),
276 this.email.getBccList(),
277 true);
278
279
280 validateSend(
281 this.fakeMailServer,
282 strSubject,
283 this.email.getHtmlMsg(),
284 this.email.getFromAddress(),
285 this.email.getToList(),
286 this.email.getCcList(),
287 this.email.getBccList(),
288 false);
289
290
291 validateSend(
292 this.fakeMailServer,
293 strSubject,
294 attachment.getName(),
295 this.email.getFromAddress(),
296 this.email.getToList(),
297 this.email.getCcList(),
298 this.email.getBccList(),
299 false);
300 }
301 catch (Exception e)
302 {
303 fail("Unexpected exception thrown");
304 }
305
306 try
307 {
308 this.getMailServer();
309
310 this.email = new MockHtmlEmailConcrete();
311 this.email.setHostName(this.strTestMailServer);
312 this.email.setSmtpPort(this.getMailServerPort());
313 this.email.setFrom(this.strTestMailFrom);
314 this.email.addTo(this.strTestMailTo);
315
316 if (this.strTestUser != null && this.strTestPasswd != null)
317 {
318 this.email.setAuthentication(
319 this.strTestUser,
320 this.strTestPasswd);
321 }
322
323 String strSubject = "Test HTML Send #1 Subject (wo charset)";
324 this.email.setSubject(strSubject);
325 this.email.setTextMsg("Test message");
326
327 this.email.send();
328 this.fakeMailServer.stop();
329
330 validateSend(
331 this.fakeMailServer,
332 strSubject,
333 this.email.getTextMsg(),
334 this.email.getFromAddress(),
335 this.email.getToList(),
336 this.email.getCcList(),
337 this.email.getBccList(),
338 true);
339 }
340
341 catch (IOException e)
342 {
343 fail("Failed to save email to output file");
344 }
345 catch (Exception e)
346 {
347 e.printStackTrace();
348 fail("Unexpected exception thrown");
349 }
350 }
351
352
353
354
355
356 public void testSend2() throws Exception
357 {
358
359
360
361
362 this.getMailServer();
363
364 this.email = new MockHtmlEmailConcrete();
365 this.email.setHostName(this.strTestMailServer);
366 this.email.setSmtpPort(this.getMailServerPort());
367 this.email.setFrom(this.strTestMailFrom);
368 this.email.addTo(this.strTestMailTo);
369
370 if (this.strTestUser != null && this.strTestPasswd != null)
371 {
372 this.email.setAuthentication(
373 this.strTestUser,
374 this.strTestPasswd);
375 }
376
377 String strSubject = "Test HTML Send #2 Subject (wo charset)";
378 this.email.setSubject(strSubject);
379 this.email.setMsg("Test txt msg");
380
381 this.email.send();
382 this.fakeMailServer.stop();
383
384 validateSend(
385 this.fakeMailServer,
386 strSubject,
387 this.email.getTextMsg(),
388 this.email.getFromAddress(),
389 this.email.getToList(),
390 this.email.getCcList(),
391 this.email.getBccList(),
392 true);
393
394
395 validateSend(
396 this.fakeMailServer,
397 strSubject,
398 this.email.getHtmlMsg(),
399 this.email.getFromAddress(),
400 this.email.getToList(),
401 this.email.getCcList(),
402 this.email.getBccList(),
403 false);
404
405 this.getMailServer();
406
407 this.email = new MockHtmlEmailConcrete();
408 this.email.setHostName(this.strTestMailServer);
409 this.email.setFrom(this.strTestMailFrom);
410 this.email.setSmtpPort(this.getMailServerPort());
411 this.email.addTo(this.strTestMailTo);
412
413 if (this.strTestUser != null && this.strTestPasswd != null)
414 {
415 this.email.setAuthentication(
416 this.strTestUser,
417 this.strTestPasswd);
418 }
419
420 strSubject = "Test HTML Send #2 Subject (w charset)";
421 this.email.setCharset(Email.ISO_8859_1);
422 this.email.setSubject(strSubject);
423 this.email.setMsg("Test txt msg");
424
425 this.email.send();
426 this.fakeMailServer.stop();
427
428 validateSend(
429 this.fakeMailServer,
430 strSubject,
431 this.email.getTextMsg(),
432 this.email.getFromAddress(),
433 this.email.getToList(),
434 this.email.getCcList(),
435 this.email.getBccList(),
436 true);
437
438
439 validateSend(
440 this.fakeMailServer,
441 strSubject,
442 this.email.getHtmlMsg(),
443 this.email.getFromAddress(),
444 this.email.getToList(),
445 this.email.getCcList(),
446 this.email.getBccList(),
447 false);
448
449 }
450
451 }