1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail;
18
19 import static org.junit.Assert.*;
20
21 import java.io.File;
22 import java.nio.charset.Charset;
23 import java.nio.charset.IllegalCharsetNameException;
24 import java.util.ArrayList;
25 import java.util.Calendar;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.Hashtable;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Properties;
33
34 import javax.mail.Message;
35 import javax.mail.Session;
36 import javax.mail.internet.InternetAddress;
37 import javax.mail.internet.MimeMessage;
38 import javax.mail.internet.MimeMultipart;
39 import javax.mail.internet.ParseException;
40
41 import org.apache.commons.mail.mocks.MockEmailConcrete;
42 import org.junit.Before;
43 import org.junit.Test;
44
45
46
47
48
49
50 public class EmailTest extends AbstractEmailTest
51 {
52 private static final String[] VALID_EMAILS =
53 {
54 "me@home.com",
55 "joe.doe@apache.org",
56 "someone_here@work-address.com.au"
57 };
58
59
60 private MockEmailConcrete email;
61
62 @Before
63 public void setUpEmailTest()
64 {
65
66 email = new MockEmailConcrete();
67 }
68
69 @Test
70 public void testGetSetDebug()
71 {
72 email.setDebug(true);
73 assertTrue(email.isDebug());
74 email.setDebug(false);
75 assertFalse(email.isDebug());
76 }
77
78 @Test
79 public void testGetSetSession() throws Exception
80 {
81
82 final Properties properties = new Properties(System.getProperties());
83 properties.setProperty(EmailConstants.MAIL_TRANSPORT_PROTOCOL, EmailConstants.SMTP);
84
85 properties.setProperty(
86 EmailConstants.MAIL_PORT,
87 String.valueOf(getMailServerPort()));
88 properties.setProperty(EmailConstants.MAIL_HOST, strTestMailServer);
89 properties.setProperty(EmailConstants.MAIL_DEBUG, String.valueOf(false));
90
91 final Session mySession = Session.getInstance(properties, null);
92
93 email.setMailSession(mySession);
94 assertEquals(mySession, email.getMailSession());
95
96 }
97
98 @Test
99 public void testGetSetAuthentication()
100 {
101
102 final String strUsername = "user.name";
103 final String strPassword = "user.pwd";
104 email.setAuthentication(strUsername, strPassword);
105
106
107
108 final DefaultAuthenticator retrievedAuth =
109 (DefaultAuthenticator) email.getAuthenticator();
110
111
112 assertEquals(
113 strUsername,
114 retrievedAuth.getPasswordAuthentication().getUserName());
115 assertEquals(
116 strPassword,
117 retrievedAuth.getPasswordAuthentication().getPassword());
118 }
119
120 @Test
121 public void testGetSetAuthenticator()
122 {
123
124 final String strUsername = "user.name";
125 final String strPassword = "user.pwd";
126 final DefaultAuthenticator authenticator =
127 new DefaultAuthenticator(strUsername, strPassword);
128 email.setAuthenticator(authenticator);
129
130
131
132 final DefaultAuthenticator retrievedAuth =
133 (DefaultAuthenticator) email.getAuthenticator();
134
135
136 assertEquals(
137 strUsername,
138 retrievedAuth.getPasswordAuthentication().getUserName());
139 assertEquals(
140 strPassword,
141 retrievedAuth.getPasswordAuthentication().getPassword());
142 }
143
144 @Test
145 public void testGetSetCharset()
146 {
147
148
149 Charset set = Charset.forName("US-ASCII");
150 email.setCharset(set.name());
151 assertEquals(set.name(), email.getCharset());
152
153 set = Charset.forName("UTF-8");
154 email.setCharset(set.name());
155 assertEquals(set.name(), email.getCharset());
156 }
157
158 @Test
159 public void testSetContentEmptyMimeMultipart()
160 {
161 final MimeMultipart part = new MimeMultipart();
162 email.setContent(part);
163
164 assertEquals(part, email.getContentMimeMultipart());
165 }
166
167 @Test
168 public void testSetContentMimeMultipart()
169 {
170 final MimeMultipart part = new MimeMultipart("abc123");
171 email.setContent(part);
172
173 assertEquals(part, email.getContentMimeMultipart());
174 }
175
176 @Test
177 public void testSetContentNull() throws Exception
178 {
179 email.setContent(null);
180 assertNull(email.getContentMimeMultipart());
181 }
182
183 @Test
184 public void testSetContentObject()
185 {
186
187
188 String testObject = "test string object";
189 String testContentType = " ; charset=" + EmailConstants.US_ASCII;
190
191 email.setContent(testObject, testContentType);
192 assertEquals(testObject, email.getContentObject());
193 assertEquals(testContentType, email.getContentType());
194
195
196
197 testObject = null;
198 testContentType = " ; charset=" + EmailConstants.US_ASCII + " some more here";
199
200 email.setContent(testObject, testContentType);
201 assertEquals(testObject, email.getContentObject());
202 assertEquals(testContentType, email.getContentType());
203
204
205
206 testObject = "test string object";
207 testContentType = null;
208
209 email.setContent(testObject, testContentType);
210 assertEquals(testObject, email.getContentObject());
211 assertEquals(testContentType, email.getContentType());
212
213
214
215 testObject = "test string object";
216 testContentType = " something incorrect ";
217
218 email.setContent(testObject, testContentType);
219 assertEquals(testObject, email.getContentObject());
220 assertEquals(testContentType, email.getContentType());
221 }
222
223 @Test
224 public void testGetSetHostName()
225 {
226 for (final String validChar : testCharsValid) {
227 email.setHostName(validChar);
228 assertEquals(validChar, email.getHostName());
229 }
230 }
231
232 @Test
233 public void testGetSetSmtpPort()
234 {
235 email.setSmtpPort(1);
236 assertEquals(
237 1,
238 Integer.valueOf(email.getSmtpPort()).intValue());
239
240 email.setSmtpPort(Integer.MAX_VALUE);
241 assertEquals(
242 Integer.MAX_VALUE,
243 Integer.valueOf(email.getSmtpPort()).intValue());
244 }
245
246 @Test(expected = IllegalArgumentException.class)
247 public void testSetSmtpPortZero()
248 {
249 email.setSmtpPort(0);
250 }
251
252 @Test(expected = IllegalArgumentException.class)
253 public void testSetSmptPortNegative()
254 {
255 email.setSmtpPort(-1);
256 }
257
258 @Test(expected = IllegalArgumentException.class)
259 public void testSetSmtpPortMinValue()
260 {
261 email.setSmtpPort(Integer.MIN_VALUE);
262 }
263
264 @Test
265 public void testSetFrom() throws Exception
266 {
267
268
269
270
271 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
272 arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));
273 arrExpected.add(
274 new InternetAddress(
275 "joe.doe@apache.org",
276 "joe.doe@apache.org"));
277 arrExpected.add(
278 new InternetAddress(
279 "someone_here@work-address.com.au",
280 "someone_here@work-address.com.au"));
281
282 for (int i = 0; i < VALID_EMAILS.length; i++)
283 {
284
285
286 email.setFrom(VALID_EMAILS[i]);
287
288
289 assertEquals(arrExpected.get(i), email.getFromAddress());
290 }
291 }
292
293 @Test
294 public void testSetFromWithEncoding() throws Exception
295 {
296
297
298
299 final String testValidEmail = "me@home.com";
300
301 final InternetAddress inetExpected =
302 new InternetAddress("me@home.com", "me@home.com", EmailConstants.ISO_8859_1);
303
304
305 email.setFrom(testValidEmail, testValidEmail, EmailConstants.ISO_8859_1);
306
307
308 assertEquals(inetExpected, email.getFromAddress());
309
310 }
311
312 @Test
313 public void testSetFrom2() throws Exception
314 {
315
316
317
318 final String[] testEmailNames = {"Name1", "", null};
319 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
320 arrExpected.add(new InternetAddress("me@home.com", "Name1"));
321 arrExpected.add(
322 new InternetAddress(
323 "joe.doe@apache.org",
324 "joe.doe@apache.org"));
325 arrExpected.add(
326 new InternetAddress(
327 "someone_here@work-address.com.au",
328 "someone_here@work-address.com.au"));
329
330 for (int i = 0; i < VALID_EMAILS.length; i++)
331 {
332
333 email.setFrom(VALID_EMAILS[i], testEmailNames[i]);
334
335
336 assertEquals(arrExpected.get(i), email.getFromAddress());
337
338 }
339 }
340
341 @Test(expected = IllegalCharsetNameException.class)
342 public void testSetFromBadEncoding() throws Exception {
343 email.setFrom("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
344 }
345
346 @Test
347 public void testAddTo() throws Exception
348 {
349
350
351
352
353 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
354 arrExpected.add(new InternetAddress("me@home.com"));
355 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
356 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
357
358 for (final String address : VALID_EMAILS) {
359 email.addTo(address);
360 }
361
362
363 assertEquals(arrExpected.size(), email.getToAddresses().size());
364 assertEquals(arrExpected.toString(), email.getToAddresses().toString());
365 }
366
367 @Test
368 public void testAddToArray() throws Exception
369 {
370
371
372
373
374 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
375 arrExpected.add(new InternetAddress("me@home.com"));
376 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
377 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
378
379
380 email.addTo(VALID_EMAILS);
381
382
383 assertEquals(arrExpected.size(), email.getToAddresses().size());
384 assertEquals(arrExpected.toString(), email.getToAddresses().toString());
385 }
386
387 @Test
388 public void testAddToWithEncoding() throws Exception
389 {
390
391
392
393 final String testCharset = EmailConstants.ISO_8859_1;
394 final String[] testEmailNames = {"Name1", "", null};
395
396 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
397 arrExpected.add(
398 new InternetAddress(
399 "me@home.com",
400 "Name1",
401 testCharset));
402 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
403 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
404
405 for (int i = 0; i < VALID_EMAILS.length; i++)
406 {
407
408 email.addTo(VALID_EMAILS[i], testEmailNames[i], testCharset);
409 }
410
411
412 assertEquals(arrExpected.size(), email.getToAddresses().size());
413 assertEquals(arrExpected.toString(), email.getToAddresses().toString());
414 }
415
416 @Test
417 public void testAddTo2() throws Exception
418 {
419
420
421
422
423 final String[] testEmailNames = {"Name1", "", null};
424
425 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
426 arrExpected.add(new InternetAddress("me@home.com", "Name1"));
427 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
428 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
429
430 for (int i = 0; i < VALID_EMAILS.length; i++)
431 {
432
433 email.addTo(VALID_EMAILS[i], testEmailNames[i]);
434 }
435
436
437 assertEquals(arrExpected.size(), email.getToAddresses().size());
438 assertEquals(arrExpected.toString(), email.getToAddresses().toString());
439 }
440
441 @Test(expected = IllegalCharsetNameException.class)
442 public void testAddToBadEncoding() throws Exception
443 {
444 email.addTo("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
445 }
446
447 @Test
448 public void testSetTo() throws Exception
449 {
450
451
452
453 final List<InternetAddress> testEmailValid2 = new ArrayList<InternetAddress>();
454 testEmailValid2.add(new InternetAddress("me@home.com", "Name1"));
455 testEmailValid2.add(
456 new InternetAddress(
457 "joe.doe@apache.org",
458 "joe.doe@apache.org"));
459 testEmailValid2.add(
460 new InternetAddress(
461 "someone_here@work-address.com.au",
462 "someone_here@work-address.com.au"));
463
464 email.setTo(testEmailValid2);
465
466
467 assertEquals(testEmailValid2.size(), email.getToAddresses().size());
468 assertEquals(
469 testEmailValid2.toString(),
470 email.getToAddresses().toString());
471 }
472
473 @Test(expected = EmailException.class)
474 public void testSetToNull() throws Exception
475 {
476 email.setTo(null);
477 }
478
479 @Test(expected = EmailException.class)
480 public void testSetToEmpty() throws Exception
481 {
482 email.setTo(Collections.<InternetAddress>emptyList());
483 }
484
485 @Test
486 public void testAddCc() throws Exception
487 {
488
489
490
491
492 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
493 arrExpected.add(new InternetAddress("me@home.com"));
494 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
495 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
496
497 for (final String address : VALID_EMAILS) {
498 email.addCc(address);
499 }
500
501
502 assertEquals(arrExpected.size(), email.getCcAddresses().size());
503 assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
504 }
505
506 @Test
507 public void testAddCcArray() throws Exception
508 {
509
510
511
512
513 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
514 arrExpected.add(new InternetAddress("me@home.com"));
515 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
516 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
517
518
519 email.addCc(VALID_EMAILS);
520
521
522 assertEquals(arrExpected.size(), email.getCcAddresses().size());
523 assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
524 }
525
526 @Test
527 public void testAddCcWithEncoding() throws Exception
528 {
529
530
531
532 final String testCharset = EmailConstants.ISO_8859_1;
533 final String[] testEmailNames = {"Name1", "", null};
534
535 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
536 arrExpected.add(
537 new InternetAddress("me@home.com", "Name1", testCharset));
538 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
539 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
540
541
542 for (int i = 0; i < VALID_EMAILS.length; i++)
543 {
544 email.addCc(VALID_EMAILS[i], testEmailNames[i], testCharset);
545 }
546
547
548 assertEquals(arrExpected.size(), email.getCcAddresses().size());
549 assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
550 }
551
552 @Test
553 public void testAddCc2() throws Exception
554 {
555
556
557
558
559 final String[] testEmailNames = {"Name1", "", null};
560
561 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
562 arrExpected.add(new InternetAddress("me@home.com", "Name1"));
563 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
564 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
565
566 for (int i = 0; i < VALID_EMAILS.length; i++)
567 {
568
569 email.addCc(VALID_EMAILS[i], testEmailNames[i]);
570 }
571
572
573 assertEquals(arrExpected.size(), email.getCcAddresses().size());
574 assertEquals(arrExpected.toString(), email.getCcAddresses().toString());
575 }
576
577 @Test(expected = IllegalCharsetNameException.class)
578 public void testAddCcBadEncoding() throws Exception
579 {
580 email.addCc("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
581 }
582
583 @Test
584 public void testSetCc() throws Exception
585 {
586
587
588
589 final List<InternetAddress> testEmailValid2 = new ArrayList<InternetAddress>();
590 testEmailValid2.add(new InternetAddress("Name1 <me@home.com>"));
591 testEmailValid2.add(new InternetAddress("\"joe.doe@apache.org\" <joe.doe@apache.org>"));
592 testEmailValid2.add(
593 new InternetAddress("\"someone_here@work.com.au\" <someone_here@work.com.au>"));
594
595 email.setCc(testEmailValid2);
596 assertEquals(testEmailValid2, email.getCcAddresses());
597 }
598
599 @Test(expected = EmailException.class)
600 public void testSetCcNull() throws Exception
601 {
602 email.setCc(null);
603 }
604
605 @Test(expected = EmailException.class)
606 public void testSetCcEmpty() throws Exception
607 {
608 email.setCc(Collections.<InternetAddress>emptyList());
609 }
610
611 @Test
612 public void testAddBcc() throws Exception
613 {
614
615
616
617
618 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
619 arrExpected.add(new InternetAddress("me@home.com"));
620 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
621 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
622
623 for (final String address : VALID_EMAILS) {
624 email.addBcc(address);
625 }
626
627
628 assertEquals(arrExpected.size(), email.getBccAddresses().size());
629 assertEquals(
630 arrExpected.toString(),
631 email.getBccAddresses().toString());
632 }
633
634 @Test
635 public void testAddBccArray() throws Exception
636 {
637
638
639
640
641 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
642 arrExpected.add(new InternetAddress("me@home.com"));
643 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
644 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
645
646
647 email.addBcc(VALID_EMAILS);
648
649
650 assertEquals(arrExpected.size(), email.getBccAddresses().size());
651 assertEquals(
652 arrExpected.toString(),
653 email.getBccAddresses().toString());
654 }
655
656 @Test
657 public void testAddBccWithEncoding() throws Exception
658 {
659
660
661
662 final String testCharset = EmailConstants.ISO_8859_1;
663 final String[] testEmailNames = {"Name1", "", null};
664
665 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
666 arrExpected.add(new InternetAddress("me@home.com", "Name1", testCharset));
667 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
668 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
669
670 for (int i = 0; i < VALID_EMAILS.length; i++)
671 {
672
673 email.addBcc(VALID_EMAILS[i], testEmailNames[i], testCharset);
674 }
675
676
677 assertEquals(arrExpected.size(), email.getBccAddresses().size());
678 assertEquals(
679 arrExpected.toString(),
680 email.getBccAddresses().toString());
681 }
682
683 @Test
684 public void testAddBcc2() throws Exception
685 {
686
687
688
689
690 final String[] testEmailNames = {"Name1", "", null};
691
692
693 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
694 arrExpected.add(new InternetAddress("me@home.com", "Name1"));
695 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
696 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
697
698 for (int i = 0; i < VALID_EMAILS.length; i++)
699 {
700
701 email.addBcc(VALID_EMAILS[i], testEmailNames[i]);
702 }
703
704
705 assertEquals(arrExpected.size(), email.getBccAddresses().size());
706 assertEquals(
707 arrExpected.toString(),
708 email.getBccAddresses().toString());
709 }
710
711 @Test(expected = IllegalCharsetNameException.class)
712 public void testAddBccBadEncoding() throws Exception
713 {
714 email.addBcc("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
715 }
716
717 @Test
718 public void testSetBcc() throws Exception
719 {
720
721
722
723 final List<InternetAddress> testInetEmailValid = new ArrayList<InternetAddress>();
724 testInetEmailValid.add(new InternetAddress("me@home.com", "Name1"));
725 testInetEmailValid.add(
726 new InternetAddress(
727 "joe.doe@apache.org",
728 "joe.doe@apache.org"));
729 testInetEmailValid.add(
730 new InternetAddress(
731 "someone_here@work-address.com.au",
732 "someone_here@work-address.com.au"));
733
734 email.setBcc(testInetEmailValid);
735 assertEquals(testInetEmailValid, email.getBccAddresses());
736 }
737
738 @Test(expected = EmailException.class)
739 public void testSetBccNull() throws Exception
740 {
741 email.setBcc(null);
742 }
743
744 @Test(expected = EmailException.class)
745 public void testSetBccEmpty() throws Exception
746 {
747 email.setBcc(Collections.<InternetAddress>emptyList());
748 }
749
750 @Test
751 public void testAddReplyTo() throws Exception
752 {
753
754
755
756
757 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
758 arrExpected.add(new InternetAddress("me@home.com"));
759 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
760 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
761
762 for (final String address : VALID_EMAILS) {
763 email.addReplyTo(address);
764 }
765
766
767 assertEquals(arrExpected.size(), email.getReplyToAddresses().size());
768 assertEquals(
769 arrExpected.toString(),
770 email.getReplyToAddresses().toString());
771 }
772
773 @Test
774 public void testAddReplyToWithEncoding() throws Exception
775 {
776
777
778
779 final String testCharset = EmailConstants.ISO_8859_1;
780 final String[] testEmailNames = {"Name1", "", null};
781
782 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
783 arrExpected.add(new InternetAddress("me@home.com", "Name1", testCharset));
784 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
785 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
786
787 for (int i = 0; i < VALID_EMAILS.length; i++)
788 {
789
790 email.addReplyTo(VALID_EMAILS[i], testEmailNames[i], testCharset);
791 }
792
793
794 assertEquals(arrExpected.size(), email.getReplyToAddresses().size());
795 assertEquals(
796 arrExpected.toString(),
797 email.getReplyToAddresses().toString());
798 }
799
800 @Test
801 public void testAddReplyTo2() throws Exception
802 {
803
804
805
806
807 final String[] testEmailNames = {"Name1", "", null};
808
809 final List<InternetAddress> arrExpected = new ArrayList<InternetAddress>();
810 arrExpected.add(new InternetAddress("me@home.com", "Name1"));
811 arrExpected.add(new InternetAddress("joe.doe@apache.org"));
812 arrExpected.add(new InternetAddress("someone_here@work-address.com.au"));
813
814 for (int i = 0; i < VALID_EMAILS.length; i++)
815 {
816
817 email.addReplyTo(VALID_EMAILS[i], testEmailNames[i]);
818 }
819
820
821 assertEquals(arrExpected.size(), email.getReplyToAddresses().size());
822 assertEquals(
823 arrExpected.toString(),
824 email.getReplyToAddresses().toString());
825 }
826
827 @Test(expected = IllegalCharsetNameException.class)
828 public void testAddReplyToBadEncoding() throws Exception
829 {
830 email.addReplyTo("me@home.com", "me@home.com", "bad.encoding\uc5ec\n");
831 }
832
833 @Test
834 public void testAddHeader()
835 {
836
837
838
839 final Map<String, String> headers = new HashMap<String, String>();
840 headers.put("X-Priority", "1");
841 headers.put("Disposition-Notification-To", "me@home.com");
842 headers.put("X-Mailer", "Sendmail");
843
844 for (final Map.Entry<String, String> header : headers.entrySet()) {
845 final String name = header.getKey();
846 final String value = header.getValue();
847 email.addHeader(name, value);
848 }
849
850 assertEquals(headers.size(), email.getHeaders().size());
851 assertEquals(headers, email.getHeaders());
852 }
853
854 @Test(expected = IllegalArgumentException.class)
855 public void testAddHeaderEmptyName() throws Exception
856 {
857 email.addHeader("", "me@home.com");
858 }
859
860 @Test(expected = IllegalArgumentException.class)
861 public void testAddHeaderNullName() throws Exception
862 {
863 email.addHeader(null, "me@home.com");
864 }
865
866 @Test(expected = IllegalArgumentException.class)
867 public void testAddHeaderEmptyValue() throws Exception
868 {
869 email.addHeader("X-Mailer", "");
870 }
871
872 @Test(expected = IllegalArgumentException.class)
873 public void testAddHeaderNullValue() throws Exception
874 {
875 email.addHeader("X-Mailer", null);
876 }
877
878 @Test
879 public void testSetHeaders()
880 {
881 final Map<String, String> ht = new Hashtable<String, String>();
882 ht.put("X-Priority", "1");
883 ht.put("Disposition-Notification-To", "me@home.com");
884 ht.put("X-Mailer", "Sendmail");
885
886 email.setHeaders(ht);
887
888 assertEquals(ht.size(), email.getHeaders().size());
889 assertEquals(ht, email.getHeaders());
890 }
891
892 @Test
893 public void testGetHeader()
894 {
895 final Map<String, String> ht = new Hashtable<String, String>();
896 ht.put("X-Foo", "Bar");
897 ht.put("X-Int", "1");
898
899 email.setHeaders(ht);
900
901 assertEquals("Bar", email.getHeader("X-Foo"));
902 assertEquals("1", email.getHeader("X-Int"));
903 }
904
905 @Test
906 public void testGetHeaders()
907 {
908 final Map<String, String> ht = new Hashtable<String, String>();
909 ht.put("X-Foo", "Bar");
910 ht.put("X-Int", "1");
911
912 email.setHeaders(ht);
913
914 assertEquals(ht.size(), email.getHeaders().size());
915 }
916
917 @Test
918 public void testFoldingHeaders() throws Exception
919 {
920 email.setHostName(strTestMailServer);
921 email.setSmtpPort(getMailServerPort());
922 email.setFrom("a@b.com");
923 email.addTo("c@d.com");
924 email.setSubject("test mail");
925
926 final String headerValue = "1234567890 1234567890 123456789 01234567890 123456789 0123456789 01234567890 01234567890";
927 email.addHeader("X-LongHeader", headerValue);
928
929 assertTrue(email.getHeaders().size() == 1);
930
931 assertFalse(email.getHeaders().get("X-LongHeader").contains("\r\n"));
932
933 email.buildMimeMessage();
934
935 final MimeMessage msg = email.getMimeMessage();
936 msg.saveChanges();
937
938 final String[] values = msg.getHeader("X-LongHeader");
939 assertEquals(1, values.length);
940
941
942 final String[] lines = values[0].split("\\r\\n");
943 assertEquals(2, lines.length);
944
945
946 assertTrue(values[0].indexOf("\n") == values[0].lastIndexOf("\n"));
947 }
948
949 @Test(expected = IllegalArgumentException.class)
950 public void testSetHeaderEmptyValue() throws Exception
951 {
952 email.setHeaders(Collections.singletonMap("X-Mailer", ""));
953 }
954
955 @Test(expected = IllegalArgumentException.class)
956 public void testSetHeaderNullValue() throws Exception
957 {
958 email.setHeaders(Collections.singletonMap("X-Mailer", (String) null));
959 }
960
961 @Test(expected = IllegalArgumentException.class)
962 public void testSetHeaderEmptyName() throws Exception
963 {
964 email.setHeaders(Collections.singletonMap("", "me@home.com"));
965 }
966
967 @Test(expected = IllegalArgumentException.class)
968 public void testSetHeaderNullName() throws Exception
969 {
970 email.setHeaders(Collections.singletonMap((String) null, "me@home.com"));
971 }
972
973 @Test
974 public void testSetSubjectValid() {
975 for (final String validChar : testCharsValid) {
976 email.setSubject(validChar);
977 assertEquals(validChar, email.getSubject());
978 }
979 assertEquals(null, email.setSubject(null).getSubject());
980 assertEquals("", email.setSubject("").getSubject());
981 assertEquals(" ", email.setSubject(" ").getSubject());
982 assertEquals("\t", email.setSubject("\t").getSubject());
983 }
984
985 @Test
986 public void testEndOflineCharactersInSubjectAreReplacedWithSpaces() {
987 for (final String invalidChar : endOfLineCombinations) {
988 email.setSubject(invalidChar);
989 assertNotEquals(invalidChar, email.getSubject());
990 }
991 assertEquals("abcdefg", email.setSubject("abcdefg").getSubject());
992 assertEquals("abc defg", email.setSubject("abc\rdefg").getSubject());
993 assertEquals("abc defg", email.setSubject("abc\ndefg").getSubject());
994 assertEquals("abc defg", email.setSubject("abc\r\ndefg").getSubject());
995 assertEquals("abc defg", email.setSubject("abc\n\rdefg").getSubject());
996 }
997
998 @Test(expected = EmailException.class)
999 public void testSendNoHostName() throws Exception
1000 {
1001 getMailServer();
1002
1003 email = new MockEmailConcrete();
1004 email.send();
1005 }
1006
1007 @Test
1008 public void testSendBadHostName()
1009 {
1010 try
1011 {
1012 getMailServer();
1013
1014 email = new MockEmailConcrete();
1015 email.setSubject("Test Email #1 Subject");
1016 email.setHostName("bad.host.com");
1017 email.setFrom("me@home.com");
1018 email.addTo("me@home.com");
1019 email.addCc("me@home.com");
1020 email.addBcc("me@home.com");
1021 email.addReplyTo("me@home.com");
1022
1023 email.setContent(
1024 "test string object",
1025 " ; charset=" + EmailConstants.US_ASCII);
1026
1027 email.send();
1028 fail("Should have thrown an exception");
1029 }
1030 catch (final EmailException e)
1031 {
1032 assertTrue(e.getCause() instanceof ParseException);
1033 fakeMailServer.stop();
1034 }
1035 }
1036
1037 @Test(expected = EmailException.class)
1038 public void testSendFromNotSet() throws Exception
1039 {
1040 getMailServer();
1041
1042 email = new MockEmailConcrete();
1043 email.setHostName(strTestMailServer);
1044 email.setSmtpPort(getMailServerPort());
1045 email.addTo("me@home.com");
1046
1047 email.send();
1048 }
1049
1050 @Test
1051 public void testSendFromSetInSession() throws Exception
1052 {
1053 getMailServer();
1054
1055 email = new MockEmailConcrete();
1056 email.setHostName(strTestMailServer);
1057 email.setSmtpPort(getMailServerPort());
1058 email.addTo("me@home.com");
1059 email.getSession().getProperties().setProperty(EmailConstants.MAIL_FROM, "me@home.com");
1060
1061 email.send();
1062 }
1063
1064 @Test(expected = EmailException.class)
1065 public void testSendDestinationNotSet() throws Exception
1066 {
1067 getMailServer();
1068
1069 email = new MockEmailConcrete();
1070 email.setHostName(strTestMailServer);
1071 email.setSmtpPort(getMailServerPort());
1072 email.setFrom("me@home.com");
1073
1074 email.send();
1075 }
1076
1077 @Test(expected = EmailException.class)
1078 public void testSendBadAuthSet() throws Exception
1079 {
1080 getMailServer();
1081
1082 email = new MockEmailConcrete();
1083 email.setHostName(strTestMailServer);
1084 email.setSmtpPort(getMailServerPort());
1085 email.setFrom(strTestMailFrom);
1086 email.addTo(strTestMailTo);
1087 email.setAuthentication(null, null);
1088
1089 email.send();
1090 }
1091
1092 @Test
1093 public void testSendCorrectSmtpPortContainedInException()
1094 {
1095 try
1096 {
1097 getMailServer();
1098
1099 email = new MockEmailConcrete();
1100 email.setHostName("bad.host.com");
1101 email.setSSLOnConnect(true);
1102 email.setFrom(strTestMailFrom);
1103 email.addTo(strTestMailTo);
1104 email.setAuthentication(null, null);
1105 email.send();
1106 fail("Should have thrown an exception");
1107 }
1108 catch (final EmailException e)
1109 {
1110 assertTrue(e.getMessage().contains("bad.host.com:465"));
1111 fakeMailServer.stop();
1112 }
1113 }
1114
1115 @Test
1116 public void testGetSetSentDate()
1117 {
1118
1119
1120 final Date dtTest = Calendar.getInstance().getTime();
1121 email.setSentDate(dtTest);
1122 assertEquals(dtTest, email.getSentDate());
1123
1124
1125 email.setSentDate(null);
1126
1127 final Date sentDate = email.getSentDate();
1128
1129
1130
1131
1132
1133 assertTrue(Math.abs(sentDate.getTime() - dtTest.getTime()) < 1000);
1134 }
1135
1136 @Test
1137 public void testToInternetAddressArray() throws Exception
1138 {
1139 final List<InternetAddress> testInetEmailValid = new ArrayList<InternetAddress>();
1140
1141 testInetEmailValid.add(new InternetAddress("me@home.com", "Name1"));
1142 testInetEmailValid.add(
1143 new InternetAddress(
1144 "joe.doe@apache.org",
1145 "joe.doe@apache.org"));
1146 testInetEmailValid.add(
1147 new InternetAddress(
1148 "someone_here@work-address.com.au",
1149 "someone_here@work-address.com.au"));
1150
1151 email.setBcc(testInetEmailValid);
1152 assertEquals(
1153 testInetEmailValid.size(),
1154 email.getBccAddresses().size());
1155 }
1156
1157 @Test
1158 public void testSetPopBeforeSmtp()
1159 {
1160
1161 final boolean boolPopBeforeSmtp = true;
1162 final String strHost = "mail.home.com";
1163 final String strUsername = "user.name";
1164 final String strPassword = "user.passwd";
1165
1166 email.setPopBeforeSmtp(
1167 boolPopBeforeSmtp,
1168 strHost,
1169 strUsername,
1170 strPassword);
1171
1172
1173 assertEquals(boolPopBeforeSmtp, email.isPopBeforeSmtp());
1174 assertEquals(strHost, email.getPopHost());
1175 assertEquals(strUsername, email.getPopUsername());
1176 assertEquals(strPassword, email.getPopPassword());
1177 }
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191 @Test
1192 public void testDefaultCharsetAppliesToTextContent() throws Exception
1193 {
1194 email.setHostName(strTestMailServer);
1195 email.setSmtpPort(getMailServerPort());
1196 email.setFrom("a@b.com");
1197 email.addTo("c@d.com");
1198 email.setSubject("test mail");
1199
1200 email.setCharset("ISO-8859-1");
1201 email.setContent("test content", "text/plain");
1202 email.buildMimeMessage();
1203 final MimeMessage msg = email.getMimeMessage();
1204 msg.saveChanges();
1205 assertEquals("text/plain; charset=ISO-8859-1", msg.getContentType());
1206 }
1207
1208
1209
1210
1211
1212
1213
1214 @Test
1215 public void testDefaultCharsetCanBeOverriddenByContentType()
1216 throws Exception
1217 {
1218 email.setHostName(strTestMailServer);
1219 email.setSmtpPort(getMailServerPort());
1220 email.setFrom("a@b.com");
1221 email.addTo("c@d.com");
1222 email.setSubject("test mail");
1223
1224 email.setCharset("ISO-8859-1");
1225 email.setContent("test content", "text/plain; charset=US-ASCII");
1226 email.buildMimeMessage();
1227 final MimeMessage msg = email.getMimeMessage();
1228 msg.saveChanges();
1229 assertEquals("text/plain; charset=US-ASCII", msg.getContentType());
1230 }
1231
1232
1233
1234
1235
1236
1237 @Test
1238 public void testDefaultCharsetIgnoredByNonTextContent()
1239 throws Exception
1240 {
1241 email.setHostName(strTestMailServer);
1242 email.setSmtpPort(getMailServerPort());
1243 email.setFrom("a@b.com");
1244 email.addTo("c@d.com");
1245 email.setSubject("test mail");
1246
1247 email.setCharset("ISO-8859-1");
1248 email.setContent("test content", "application/octet-stream");
1249 email.buildMimeMessage();
1250 final MimeMessage msg = email.getMimeMessage();
1251 msg.saveChanges();
1252 assertEquals("application/octet-stream", msg.getContentType());
1253 }
1254
1255 @Test
1256 public void testCorrectContentTypeForPNG() throws Exception
1257 {
1258 email.setHostName(strTestMailServer);
1259 email.setSmtpPort(getMailServerPort());
1260 email.setFrom("a@b.com");
1261 email.addTo("c@d.com");
1262 email.setSubject("test mail");
1263
1264 email.setCharset("ISO-8859-1");
1265 final File png = new File("./target/test-classes/images/logos/maven-feather.png");
1266 email.setContent(png, "image/png");
1267 email.buildMimeMessage();
1268 final MimeMessage msg = email.getMimeMessage();
1269 msg.saveChanges();
1270 assertEquals("image/png", msg.getContentType());
1271 }
1272
1273 @Test
1274 public void testGetSetBounceAddress()
1275 {
1276 assertNull(email.getBounceAddress());
1277
1278 final String bounceAddress = "test_bounce@apache.org";
1279 email.setBounceAddress(bounceAddress);
1280
1281
1282 assertEquals(bounceAddress, email.getBounceAddress());
1283 }
1284
1285 @Test
1286 public void testSetNullAndEmptyBounceAddress()
1287 {
1288 assertNull(email.setBounceAddress(null).getBounceAddress());
1289 assertEquals("", email.setBounceAddress("").getBounceAddress());
1290 }
1291
1292 @Test(expected = RuntimeException.class)
1293 public void testGetSetInvalidBounceAddress()
1294 {
1295 email.setBounceAddress("invalid-bounce-address");
1296 }
1297
1298 @Test
1299 public void testSupportForInternationalDomainNames() throws Exception
1300 {
1301 email.setHostName(strTestMailServer);
1302 email.setSmtpPort(getMailServerPort());
1303 email.setFrom("from@d\u00F6m\u00E4in.example");
1304 email.addTo("to@d\u00F6m\u00E4in.example");
1305 email.addCc("cc@d\u00F6m\u00E4in.example");
1306 email.addBcc("bcc@d\u00F6m\u00E4in.example");
1307 email.setSubject("test mail");
1308 email.setSubject("testSupportForInternationalDomainNames");
1309 email.setMsg("This is a test mail ... :-)");
1310
1311 email.buildMimeMessage();
1312 final MimeMessage msg = email.getMimeMessage();
1313
1314 assertEquals("from@xn--dmin-moa0i.example", msg.getFrom()[0].toString());
1315 assertEquals("to@xn--dmin-moa0i.example", msg.getRecipients(Message.RecipientType.TO)[0].toString());
1316 assertEquals("cc@xn--dmin-moa0i.example", msg.getRecipients(Message.RecipientType.CC)[0].toString());
1317 assertEquals("bcc@xn--dmin-moa0i.example", msg.getRecipients(Message.RecipientType.BCC)[0].toString());
1318 }
1319 }