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 java.io.IOException;
20
21 import org.apache.commons.mail.mocks.MockSimpleEmail;
22
23
24
25
26
27
28
29 public class SimpleEmailTest extends BaseEmailTestCase
30 {
31
32 private MockSimpleEmail email;
33
34
35
36
37 public SimpleEmailTest(String name)
38 {
39 super(name);
40 }
41
42
43
44 protected void setUp() throws Exception
45 {
46 super.setUp();
47
48 this.email = new MockSimpleEmail();
49 }
50
51
52
53 public void testGetSetMsg() throws EmailException
54 {
55
56
57
58 for (int i = 0; i < testCharsValid.length; i++)
59 {
60 this.email.setMsg(testCharsValid[i]);
61 assertEquals(testCharsValid[i], this.email.getMsg());
62 }
63
64
65
66
67 for (int i = 0; i < this.testCharsNotValid.length; i++)
68 {
69 try
70 {
71 this.email.setMsg(this.testCharsNotValid[i]);
72 fail("Should have thrown an exception");
73 }
74 catch (EmailException e)
75 {
76 assertTrue(true);
77 }
78 }
79
80 }
81
82
83
84
85
86
87 public void testSend() throws EmailException, IOException
88 {
89
90
91
92 this.getMailServer();
93
94 this.email = new MockSimpleEmail();
95 this.email.setHostName(this.strTestMailServer);
96 this.email.setSmtpPort(this.getMailServerPort());
97 this.email.setFrom(this.strTestMailFrom);
98 this.email.addTo(this.strTestMailTo);
99
100 if (this.strTestUser != null && this.strTestPasswd != null)
101 {
102 this.email.setAuthentication(
103 this.strTestUser,
104 this.strTestPasswd);
105 }
106
107 String strSubject = "Test Msg Subject";
108 String strMessage = "Test Msg Body";
109
110 this.email.setCharset(Email.ISO_8859_1);
111 this.email.setSubject(strSubject);
112
113 this.email.setMsg(strMessage);
114
115 this.email.send();
116
117 this.fakeMailServer.stop();
118 validateSend(
119 this.fakeMailServer,
120 strSubject,
121 this.email.getMsg(),
122 this.email.getFromAddress(),
123 this.email.getToList(),
124 this.email.getCcList(),
125 this.email.getBccList(),
126 true);
127 }
128 }