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.File;
20 import java.io.IOException;
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;
38
39
40
41
42 public SendWithAttachmentsTest(String name)
43 {
44 super(name);
45 }
46
47
48
49 protected void setUp() throws Exception
50 {
51 super.setUp();
52
53 this.email = new MockHtmlEmailConcrete();
54 }
55
56
57
58
59
60 public void testSendNoAttachments() throws EmailException, IOException
61 {
62 this.getMailServer();
63
64 String strSubject = "Test HTML Send #1 Subject (w charset)";
65
66 this.email = new MockHtmlEmailConcrete();
67 this.email.setHostName(this.strTestMailServer);
68 this.email.setSmtpPort(this.getMailServerPort());
69 this.email.setFrom(this.strTestMailFrom);
70 this.email.addTo(this.strTestMailTo);
71
72 this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
73
74 this.email.setCharset(Email.ISO_8859_1);
75 this.email.setSubject(strSubject);
76
77 URL url = new URL(EmailConfiguration.TEST_URL);
78 String cid = this.email.embed(url, "Apache Logo");
79
80 String strHtmlMsg =
81 "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
82
83 this.email.setHtmlMsg(strHtmlMsg);
84 this.email.setTextMsg(
85 "Your email client does not support HTML emails");
86
87 this.email.send();
88 this.fakeMailServer.stop();
89
90
91 validateSend(
92 this.fakeMailServer,
93 strSubject,
94 this.email.getTextMsg(),
95 this.email.getFromAddress(),
96 this.email.getToList(),
97 this.email.getCcList(),
98 this.email.getBccList(),
99 true);
100
101
102 validateSend(
103 this.fakeMailServer,
104 strSubject,
105 this.email.getHtmlMsg(),
106 this.email.getFromAddress(),
107 this.email.getToList(),
108 this.email.getCcList(),
109 this.email.getBccList(),
110 false);
111 }
112
113
114
115
116
117 public void testSendWAttachments() throws EmailException, IOException
118 {
119 EmailAttachment attachment = new EmailAttachment();
120 File testFile = null;
121
122
123 testFile = File.createTempFile("commons-email-testfile", ".txt");
124
125
126
127
128 this.getMailServer();
129
130 String strSubject = "Test HTML Send #1 Subject (w charset)";
131
132 this.email = new MockHtmlEmailConcrete();
133 this.email.setHostName(this.strTestMailServer);
134 this.email.setSmtpPort(this.getMailServerPort());
135 this.email.setFrom(this.strTestMailFrom);
136 this.email.addTo(this.strTestMailTo);
137
138
139 attachment.setName("Test Attachment");
140 attachment.setDescription("Test Attachment Desc");
141 attachment.setPath(testFile.getAbsolutePath());
142 this.email.attach(attachment);
143
144 this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
145
146 this.email.setCharset(Email.ISO_8859_1);
147 this.email.setSubject(strSubject);
148
149 URL url = new URL(EmailConfiguration.TEST_URL);
150 String cid = this.email.embed(url, "Apache Logo");
151
152 String strHtmlMsg =
153 "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
154
155 this.email.setHtmlMsg(strHtmlMsg);
156 this.email.setTextMsg(
157 "Your email client does not support HTML emails");
158
159 this.email.send();
160 this.fakeMailServer.stop();
161
162 validateSend(
163 this.fakeMailServer,
164 strSubject,
165 this.email.getTextMsg(),
166 this.email.getFromAddress(),
167 this.email.getToList(),
168 this.email.getCcList(),
169 this.email.getBccList(),
170 true);
171
172
173 validateSend(
174 this.fakeMailServer,
175 strSubject,
176 this.email.getHtmlMsg(),
177 this.email.getFromAddress(),
178 this.email.getToList(),
179 this.email.getCcList(),
180 this.email.getBccList(),
181 false);
182
183
184 validateSend(
185 this.fakeMailServer,
186 strSubject,
187 attachment.getName(),
188 this.email.getFromAddress(),
189 this.email.getToList(),
190 this.email.getCcList(),
191 this.email.getBccList(),
192 false);
193 }
194
195 }