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 javax.mail.internet.MimeUtility;
24
25 import org.apache.commons.mail.mocks.MockHtmlEmailConcrete;
26 import org.apache.commons.mail.settings.EmailConfiguration;
27 import org.junit.Before;
28 import org.junit.Test;
29
30
31
32
33
34
35 public class SendWithAttachmentsTest extends AbstractEmailTest
36 {
37 private MockHtmlEmailConcrete email;
38
39 @Before
40 public void setUpSendWithAttachmentsTest()
41 {
42
43 this.email = new MockHtmlEmailConcrete();
44 }
45
46
47
48
49
50 @Test
51 public void testSendNoAttachments() throws EmailException, IOException
52 {
53 this.getMailServer();
54
55 final String strSubject = "Test HTML Send #1 Subject (w charset)";
56
57 this.email = new MockHtmlEmailConcrete();
58 this.email.setHostName(this.strTestMailServer);
59 this.email.setSmtpPort(this.getMailServerPort());
60 this.email.setFrom(this.strTestMailFrom);
61 this.email.addTo(this.strTestMailTo);
62
63 this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
64
65 this.email.setCharset(EmailConstants.ISO_8859_1);
66 this.email.setSubject(strSubject);
67
68 final URL url = new URL(EmailConfiguration.TEST_URL);
69 final String cid = this.email.embed(url, "Apache Logo");
70
71 final String strHtmlMsg =
72 "<html>The Apache logo - <img src=\"cid:" + cid + "\"><html>";
73
74 this.email.setHtmlMsg(strHtmlMsg);
75 this.email.setTextMsg(
76 "Your email client does not support HTML emails");
77
78 this.email.send();
79 this.fakeMailServer.stop();
80
81
82 validateSend(
83 this.fakeMailServer,
84 strSubject,
85 this.email.getTextMsg(),
86 this.email.getFromAddress(),
87 this.email.getToAddresses(),
88 this.email.getCcAddresses(),
89 this.email.getBccAddresses(),
90 true);
91
92
93 validateSend(
94 this.fakeMailServer,
95 strSubject,
96 this.email.getHtmlMsg(),
97 this.email.getFromAddress(),
98 this.email.getToAddresses(),
99 this.email.getCcAddresses(),
100 this.email.getBccAddresses(),
101 false);
102 }
103
104
105
106
107
108 @Test
109 public void testSendWAttachments() throws EmailException, IOException
110 {
111 final EmailAttachment attachment = new EmailAttachment();
112 File testFile = null;
113
114
115 testFile = File.createTempFile("commons-email-testfile", ".txt");
116
117
118
119
120 this.getMailServer();
121
122 final String strSubject = "Test HTML Send #1 Subject (w charset)";
123
124 this.email = new MockHtmlEmailConcrete();
125 this.email.setHostName(this.strTestMailServer);
126 this.email.setSmtpPort(this.getMailServerPort());
127 this.email.setFrom(this.strTestMailFrom);
128 this.email.addTo(this.strTestMailTo);
129
130
131
132
133 attachment.setName("a>ä, o>ö, u>ü, au>äu");
134 attachment.setDescription("Test Attachment Desc");
135 attachment.setPath(testFile.getAbsolutePath());
136 this.email.attach(attachment);
137
138 this.email.setAuthentication(this.strTestUser, this.strTestPasswd);
139
140 this.email.setCharset(EmailConstants.ISO_8859_1);
141 this.email.setSubject(strSubject);
142
143 final String strHtmlMsg = "<html>Test Message<html>";
144
145 this.email.setHtmlMsg(strHtmlMsg);
146 this.email.setTextMsg(
147 "Your email client does not support HTML emails");
148
149 this.email.send();
150 this.fakeMailServer.stop();
151
152 validateSend(
153 this.fakeMailServer,
154 strSubject,
155 this.email.getTextMsg(),
156 this.email.getFromAddress(),
157 this.email.getToAddresses(),
158 this.email.getCcAddresses(),
159 this.email.getBccAddresses(),
160 true);
161
162
163 validateSend(
164 this.fakeMailServer,
165 strSubject,
166 this.email.getHtmlMsg(),
167 this.email.getFromAddress(),
168 this.email.getToAddresses(),
169 this.email.getCcAddresses(),
170 this.email.getBccAddresses(),
171 false);
172
173
174 validateSend(
175 this.fakeMailServer,
176 strSubject,
177 MimeUtility.encodeText(attachment.getName()),
178 this.email.getFromAddress(),
179 this.email.getToAddresses(),
180 this.email.getCcAddresses(),
181 this.email.getBccAddresses(),
182 false);
183 }
184
185 }