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.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
28
29
30
31
32
33
34 public class SendWithAttachmentsTest extends BaseEmailTestCase
35 {
36
37 private MockHtmlEmailConcrete email = null;
38
39
40
41
42 public SendWithAttachmentsTest(String name)
43 {
44 super(name);
45 }
46
47
48 protected void setUp()
49 {
50 super.setUp();
51
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
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
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
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
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
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
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
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
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 }