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.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.io.IOException;
24
25 import org.apache.commons.mail.mocks.MockSimpleEmail;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29
30
31
32
33
34 public class SimpleEmailTest extends AbstractEmailTest
35 {
36 private MockSimpleEmail email;
37
38 @Before
39 public void setUpSimpleEmailTest()
40 {
41
42 this.email = new MockSimpleEmail();
43 }
44
45 @Test
46 public void testGetSetMsg() throws EmailException
47 {
48
49
50
51 for (final String validChar : testCharsValid)
52 {
53 this.email.setMsg(validChar);
54 assertEquals(validChar, this.email.getMsg());
55 }
56
57
58
59
60 for (final String invalidChar : this.testCharsNotValid)
61 {
62 try
63 {
64 this.email.setMsg(invalidChar);
65 fail("Should have thrown an exception");
66 }
67 catch (final EmailException e)
68 {
69 assertTrue(true);
70 }
71 }
72
73 }
74
75
76
77
78
79
80 @Test
81 public void testSend() throws EmailException, IOException
82 {
83
84
85
86 this.getMailServer();
87
88 this.email = new MockSimpleEmail();
89 this.email.setHostName(this.strTestMailServer);
90 this.email.setSmtpPort(this.getMailServerPort());
91 this.email.setFrom(this.strTestMailFrom);
92 this.email.addTo(this.strTestMailTo);
93
94 if (this.strTestUser != null && this.strTestPasswd != null)
95 {
96 this.email.setAuthentication(
97 this.strTestUser,
98 this.strTestPasswd);
99 }
100
101 final String strSubject = "Test Msg Subject";
102 final String strMessage = "Test Msg Body";
103
104 this.email.setCharset(EmailConstants.ISO_8859_1);
105 this.email.setSubject(strSubject);
106
107 this.email.setMsg(strMessage);
108
109 this.email.send();
110
111 this.fakeMailServer.stop();
112 validateSend(
113 this.fakeMailServer,
114 strSubject,
115 this.email.getMsg(),
116 this.email.getFromAddress(),
117 this.email.getToAddresses(),
118 this.email.getCcAddresses(),
119 this.email.getBccAddresses(),
120 true);
121 }
122
123 @Test
124 @Ignore
125 public void testDefaultMimeCharset() throws EmailException, IOException
126 {
127
128
129
130
131
132
133 System.setProperty(EmailConstants.MAIL_MIME_CHARSET, "utf-8");
134
135
136
137
138 this.getMailServer();
139
140 this.email = new MockSimpleEmail();
141 this.email.setHostName(this.strTestMailServer);
142 this.email.setSmtpPort(this.getMailServerPort());
143 this.email.setFrom(this.strTestMailFrom);
144 this.email.addTo(this.strTestMailTo);
145
146 if (this.strTestUser != null && this.strTestPasswd != null)
147 {
148 this.email.setAuthentication(
149 this.strTestUser,
150 this.strTestPasswd);
151 }
152
153 final String strSubject = "Test Msg Subject";
154 final String strMessage = "Test Msg Body ä";
155
156 this.email.setSubject(strSubject);
157 this.email.setMsg(strMessage);
158
159 this.email.send();
160
161 this.fakeMailServer.stop();
162
163 validateSend(
164 this.fakeMailServer,
165 strSubject,
166 this.email.getMsg().substring(0, 13),
167 this.email.getFromAddress(),
168 this.email.getToAddresses(),
169 this.email.getCcAddresses(),
170 this.email.getBccAddresses(),
171 true);
172
173 final String message = getMessageAsString(0);
174
175 assertTrue(message.toLowerCase().contains("content-type: text/plain; charset=utf-8"));
176
177 System.clearProperty(EmailConstants.MAIL_MIME_CHARSET);
178 }
179 }