View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.mail.util;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.File;
26  import java.util.List;
27  import java.util.Properties;
28  
29  import javax.activation.DataSource;
30  import javax.mail.Session;
31  import javax.mail.internet.MimeMessage;
32  
33  import org.apache.commons.mail.HtmlEmail;
34  import org.junit.Test;
35  
36  /**
37   * Testing the MimeMessageParser.
38   */
39  public class MimeMessageParserTest
40  {
41      @Test
42      public void testParseSimpleEmail() throws Exception
43      {
44          final Session session = Session.getDefaultInstance(new Properties());
45          final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/simple.eml"));
46          final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
47  
48          mimeMessageParser.parse();
49  
50          assertEquals("Test HTML Send #1 Subject (wo charset)", mimeMessageParser.getSubject());
51          assertNotNull(mimeMessageParser.getMimeMessage());
52          assertTrue(mimeMessageParser.isMultipart());
53          assertFalse(mimeMessageParser.hasHtmlContent());
54          assertTrue(mimeMessageParser.hasPlainContent());
55          assertNotNull(mimeMessageParser.getPlainContent());
56          assertNull(mimeMessageParser.getHtmlContent());
57          assertTrue(mimeMessageParser.getTo().size() == 1);
58          assertTrue(mimeMessageParser.getCc().size() == 0);
59          assertTrue(mimeMessageParser.getBcc().size() == 0);
60          assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
61          assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
62          assertFalse(mimeMessageParser.hasAttachments());
63      }
64  
65      @Test
66      public void testParseSimpleReplyEmail() throws Exception
67      {
68          final Session session = Session.getDefaultInstance(new Properties());
69          final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/simple-reply.eml"));
70          final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
71  
72          mimeMessageParser.parse();
73  
74          assertEquals("Re: java.lang.NoClassDefFoundError: org/bouncycastle/asn1/pkcs/PrivateKeyInfo", mimeMessageParser.getSubject());
75          assertNotNull(mimeMessageParser.getMimeMessage());
76          assertFalse(mimeMessageParser.isMultipart());
77          assertFalse(mimeMessageParser.hasHtmlContent());
78          assertTrue(mimeMessageParser.hasPlainContent());
79          assertNotNull(mimeMessageParser.getPlainContent());
80          assertNull(mimeMessageParser.getHtmlContent());
81          assertTrue(mimeMessageParser.getTo().size() == 1);
82          assertTrue(mimeMessageParser.getCc().size() == 0);
83          assertTrue(mimeMessageParser.getBcc().size() == 0);
84          assertEquals("coheigea@apache.org", mimeMessageParser.getFrom());
85          assertEquals("dev@ws.apache.org", mimeMessageParser.getReplyTo());
86          assertFalse(mimeMessageParser.hasAttachments());
87      }
88  
89      @Test
90      public void testParseHtmlEmailWithAttachments() throws Exception
91      {
92          DataSource dataSource;
93          final Session session = Session.getDefaultInstance(new Properties());
94          final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/html-attachment.eml"));
95          final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
96  
97          mimeMessageParser.parse();
98  
99          assertEquals("Test", mimeMessageParser.getSubject());
100         assertNotNull(mimeMessageParser.getMimeMessage());
101         assertTrue(mimeMessageParser.isMultipart());
102         assertTrue(mimeMessageParser.hasHtmlContent());
103         assertTrue(mimeMessageParser.hasPlainContent());
104         assertNotNull(mimeMessageParser.getPlainContent());
105         assertNotNull(mimeMessageParser.getHtmlContent());
106         assertTrue(mimeMessageParser.getTo().size() == 1);
107         assertTrue(mimeMessageParser.getCc().size() == 0);
108         assertTrue(mimeMessageParser.getBcc().size() == 0);
109         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getFrom());
110         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getReplyTo());
111         assertTrue(mimeMessageParser.hasAttachments());
112         final List<?> attachmentList = mimeMessageParser.getAttachmentList();
113         assertTrue(attachmentList.size() == 2);
114 
115         dataSource = mimeMessageParser.findAttachmentByName("Wasserlilien.jpg");
116         assertNotNull(dataSource);
117         assertEquals("image/jpeg", dataSource.getContentType());
118 
119         dataSource = mimeMessageParser.findAttachmentByName("it20one.pdf");
120         assertNotNull(dataSource);
121         assertEquals("application/pdf", dataSource.getContentType());
122     }
123 
124     @Test
125     public void testParseHtmlEmailWithAttachmentAndEncodedFilename() throws Exception
126     {
127         DataSource dataSource;
128         final Session session = Session.getDefaultInstance(new Properties());
129         final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/html-attachment-encoded-filename.eml"));
130         final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
131 
132         mimeMessageParser.parse();
133 
134         assertEquals("Test HTML Send #1 Subject (w charset)", mimeMessageParser.getSubject());
135         assertNotNull(mimeMessageParser.getMimeMessage());
136         assertTrue(mimeMessageParser.isMultipart());
137         assertTrue(mimeMessageParser.hasHtmlContent());
138         assertTrue(mimeMessageParser.hasPlainContent());
139         assertNotNull(mimeMessageParser.getPlainContent());
140         assertNotNull(mimeMessageParser.getHtmlContent());
141         assertTrue(mimeMessageParser.getTo().size() == 1);
142         assertTrue(mimeMessageParser.getCc().size() == 0);
143         assertTrue(mimeMessageParser.getBcc().size() == 0);
144         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
145         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
146         assertTrue(mimeMessageParser.hasAttachments());
147         final List<?> attachmentList = mimeMessageParser.getAttachmentList();
148         assertTrue(attachmentList.size() == 1);
149 
150         dataSource = mimeMessageParser.getAttachmentList().get(0);
151         assertNotNull(dataSource);
152         assertEquals("text/plain", dataSource.getContentType());
153         assertEquals("Test Attachment - a>ä, o>ö, u>ü, au>äu", dataSource.getName());
154     }
155 
156     /**
157      * This test parses an "email read notification" where the resulting data source has no name. Originally
158      * the missing name caused a NPE in MimeUtility.decodeText().
159      *
160      * @throws Exception the test failed
161      */
162     @Test    
163     public void testParseMultipartReport() throws Exception
164     {
165         DataSource dataSource;
166         final Session session = Session.getDefaultInstance(new Properties());
167         final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/multipart-report.eml"));
168         final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
169 
170         mimeMessageParser.parse();
171 
172         assertEquals("Gelesen: ", mimeMessageParser.getSubject());
173         assertNotNull(mimeMessageParser.getMimeMessage());
174         assertTrue(mimeMessageParser.isMultipart());
175         assertTrue(mimeMessageParser.hasHtmlContent());
176         assertFalse(mimeMessageParser.hasPlainContent());
177         assertNull(mimeMessageParser.getPlainContent());
178         assertNotNull(mimeMessageParser.getHtmlContent());
179         assertTrue(mimeMessageParser.getTo().size() == 1);
180         assertTrue(mimeMessageParser.getCc().size() == 0);
181         assertTrue(mimeMessageParser.getBcc().size() == 0);
182         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getFrom());
183         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getReplyTo());
184         assertTrue(mimeMessageParser.hasAttachments());
185         final List<?> attachmentList = mimeMessageParser.getAttachmentList();
186         assertTrue(attachmentList.size() == 1);
187 
188         dataSource = (DataSource) attachmentList.get(0);
189         assertNotNull(dataSource);
190         assertNull(dataSource.getName());
191         assertEquals("message/disposition-notification", dataSource.getContentType());
192     }
193 
194     /**
195      * This test parses a SAP generated email which only contains a PDF but no email
196      * text.
197      *
198      * @throws Exception the test failed
199      */
200     @Test    
201     public void testAttachmentOnly() throws Exception
202     {
203         DataSource dataSource;
204         final Session session = Session.getDefaultInstance(new Properties());
205         final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/attachment-only.eml"));
206         final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
207 
208         mimeMessageParser.parse();
209 
210         assertEquals("Kunde 100029   Auftrag   3600", mimeMessageParser.getSubject());
211         assertNotNull(mimeMessageParser.getMimeMessage());
212         assertFalse(mimeMessageParser.isMultipart());
213         assertFalse(mimeMessageParser.hasHtmlContent());
214         assertFalse(mimeMessageParser.hasPlainContent());
215         assertNull(mimeMessageParser.getPlainContent());
216         assertNull(mimeMessageParser.getHtmlContent());
217         assertTrue(mimeMessageParser.getTo().size() == 1);
218         assertTrue(mimeMessageParser.getCc().size() == 0);
219         assertTrue(mimeMessageParser.getBcc().size() == 0);
220         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getFrom());
221         assertEquals("siegfried.goeschl@it20one.at", mimeMessageParser.getReplyTo());
222         assertTrue(mimeMessageParser.hasAttachments());
223         final List<?> attachmentList = mimeMessageParser.getAttachmentList();
224         assertTrue(attachmentList.size() == 1);
225 
226         dataSource = mimeMessageParser.findAttachmentByName("Kunde 100029   Auftrag   3600.pdf");
227         assertNotNull(dataSource);
228         assertEquals("application/pdf", dataSource.getContentType());
229     }
230     
231     /**
232      * This test parses an eml file published with issue EMAIL-110.
233      * This eml file has a corrupted attachment but should not create
234      * an OutOfMemoryException.
235      * 
236      * @throws Exception the test failed
237      */
238     @Test    
239     public void testParseNoHeaderSeperatorWithOutOfMemory() throws Exception
240     {
241         final Session session = Session.getDefaultInstance(new Properties());
242         final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/outofmemory-no-header-seperation.eml"));
243         final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
244 
245         mimeMessageParser.parse();
246 
247         assertEquals("A corrupt Attachment", mimeMessageParser.getSubject());
248         assertNotNull(mimeMessageParser.getMimeMessage());
249         assertTrue(mimeMessageParser.isMultipart());
250         assertFalse(mimeMessageParser.hasHtmlContent());
251         assertFalse(mimeMessageParser.hasPlainContent());
252         assertNull(mimeMessageParser.getPlainContent());
253         assertNull(mimeMessageParser.getHtmlContent());
254         assertEquals(mimeMessageParser.getTo().size(), 1);
255         assertEquals(mimeMessageParser.getCc().size(), 0);
256         assertEquals(mimeMessageParser.getBcc().size(), 0);
257     }
258 
259     /**
260      * This test parses an email which contains a text attachment together with email text.
261      *
262      * @throws Exception the test failed
263      */
264     @Test    
265     public void testMultipartTextAttachment() throws Exception
266     {
267         DataSource dataSource;
268         final Session session = Session.getDefaultInstance(new Properties());
269         final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/multipart-text-attachment.eml"));
270         final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
271 
272         mimeMessageParser.parse();
273 
274         assertEquals("test", mimeMessageParser.getSubject());
275         assertNotNull(mimeMessageParser.getMimeMessage());
276         assertTrue(mimeMessageParser.isMultipart());
277         assertFalse(mimeMessageParser.hasHtmlContent());
278         assertTrue(mimeMessageParser.hasPlainContent());
279         assertNotNull(mimeMessageParser.getPlainContent());
280         assertNull(mimeMessageParser.getHtmlContent());
281         assertTrue(mimeMessageParser.getTo().size() == 1);
282         assertTrue(mimeMessageParser.getCc().size() == 0);
283         assertTrue(mimeMessageParser.getBcc().size() == 0);
284         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
285         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
286         assertTrue(mimeMessageParser.hasAttachments());
287         final List<?> attachmentList = mimeMessageParser.getAttachmentList();
288         assertTrue(attachmentList.size() == 1);
289 
290         dataSource = mimeMessageParser.findAttachmentByName("test.txt");
291         assertNotNull(dataSource);
292         assertEquals("text/plain", dataSource.getContentType());
293     }
294 
295     /**
296      * This test parses an email which only contains a text attachment with content-disposition: attachment.
297      *
298      * @throws Exception the test failed
299      */
300     @Test    
301     public void testMultipartTextAttachmentOnly() throws Exception
302     {
303         DataSource dataSource;
304         final Session session = Session.getDefaultInstance(new Properties());
305         final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/multipart-text-attachment-only.eml"));
306         final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
307 
308         mimeMessageParser.parse();
309 
310         assertEquals("test", mimeMessageParser.getSubject());
311         assertNotNull(mimeMessageParser.getMimeMessage());
312         assertTrue(mimeMessageParser.isMultipart());
313         assertFalse(mimeMessageParser.hasHtmlContent());
314         assertFalse(mimeMessageParser.hasPlainContent());
315         assertNull(mimeMessageParser.getPlainContent());
316         assertNull(mimeMessageParser.getHtmlContent());
317         assertTrue(mimeMessageParser.getTo().size() == 1);
318         assertTrue(mimeMessageParser.getCc().size() == 0);
319         assertTrue(mimeMessageParser.getBcc().size() == 0);
320         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
321         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
322         assertTrue(mimeMessageParser.hasAttachments());
323         final List<?> attachmentList = mimeMessageParser.getAttachmentList();
324         assertTrue(attachmentList.size() == 1);
325 
326         dataSource = mimeMessageParser.findAttachmentByName("test.txt");
327         assertNotNull(dataSource);
328         assertEquals("text/plain", dataSource.getContentType());
329     }
330 
331     /**
332      * This test parses an email which contains an html attachment with content-disposition: attachment.
333      *
334      * @throws Exception the test failed
335      */
336     @Test
337     public void testParseHtmlEmailWithHtmlAttachment() throws Exception
338     {
339         DataSource dataSource;
340         final Session session = Session.getDefaultInstance(new Properties());
341         final MimeMessage message = MimeMessageUtils.createMimeMessage(session, new File("./src/test/resources/eml/html-attachment-content-disposition.eml"));
342         final MimeMessageParser mimeMessageParser = new MimeMessageParser(message);
343 
344         mimeMessageParser.parse();
345 
346         assertEquals("test", mimeMessageParser.getSubject());
347         assertNotNull(mimeMessageParser.getMimeMessage());
348         assertTrue(mimeMessageParser.isMultipart());
349         assertFalse(mimeMessageParser.hasHtmlContent());
350         assertTrue(mimeMessageParser.hasPlainContent());
351         assertNotNull(mimeMessageParser.getPlainContent());
352         assertNull(mimeMessageParser.getHtmlContent());
353         assertTrue(mimeMessageParser.getTo().size() == 1);
354         assertTrue(mimeMessageParser.getCc().size() == 0);
355         assertTrue(mimeMessageParser.getBcc().size() == 0);
356         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
357         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
358         assertTrue(mimeMessageParser.hasAttachments());
359         final List<?> attachmentList = mimeMessageParser.getAttachmentList();
360         assertTrue(attachmentList.size() == 1);
361 
362         dataSource = mimeMessageParser.findAttachmentByName("test.html");
363         assertNotNull(dataSource);
364         assertEquals("text/html", dataSource.getContentType());
365     }
366 
367     @Test
368     public void testParseCreatedHtmlEmailWithNoContent() throws Exception
369     {
370         final Session session = Session.getDefaultInstance(new Properties());
371 
372         final HtmlEmail email = new HtmlEmail();
373 
374         email.setMailSession(session);
375 
376         email.setFrom("test_from@apache.org");
377         email.setSubject("Test Subject");
378         email.addTo("test_to@apache.org");
379 
380         email.buildMimeMessage();
381         final MimeMessage msg = email.getMimeMessage();
382 
383         final MimeMessageParser mimeMessageParser = new MimeMessageParser(msg);
384         mimeMessageParser.parse();
385 
386         assertEquals("Test Subject", mimeMessageParser.getSubject());
387         assertNotNull(mimeMessageParser.getMimeMessage());
388         assertTrue(mimeMessageParser.isMultipart());
389         assertFalse(mimeMessageParser.hasHtmlContent());
390         assertFalse(mimeMessageParser.hasPlainContent());
391         assertNull(mimeMessageParser.getPlainContent());
392         assertNull(mimeMessageParser.getHtmlContent());
393         assertTrue(mimeMessageParser.getTo().size() == 1);
394         assertTrue(mimeMessageParser.getCc().size() == 0);
395         assertTrue(mimeMessageParser.getBcc().size() == 0);
396         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
397         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
398         assertFalse(mimeMessageParser.hasAttachments());
399     }
400 
401     @Test
402     public void testParseCreatedHtmlEmailWithTextContent() throws Exception
403     {
404         final Session session = Session.getDefaultInstance(new Properties());
405 
406         final HtmlEmail email = new HtmlEmail();
407 
408         email.setMailSession(session);
409 
410         email.setFrom("test_from@apache.org");
411         email.setSubject("Test Subject");
412         email.addTo("test_to@apache.org");
413         email.setTextMsg("My test message");
414 
415         email.buildMimeMessage();
416         final MimeMessage msg = email.getMimeMessage();
417 
418         final MimeMessageParser mimeMessageParser = new MimeMessageParser(msg);
419         mimeMessageParser.parse();
420 
421         assertEquals("Test Subject", mimeMessageParser.getSubject());
422         assertNotNull(mimeMessageParser.getMimeMessage());
423         assertTrue(mimeMessageParser.isMultipart());
424         assertFalse(mimeMessageParser.hasHtmlContent());
425         assertTrue(mimeMessageParser.hasPlainContent());
426         assertNotNull(mimeMessageParser.getPlainContent());
427         assertNull(mimeMessageParser.getHtmlContent());
428         assertTrue(mimeMessageParser.getTo().size() == 1);
429         assertTrue(mimeMessageParser.getCc().size() == 0);
430         assertTrue(mimeMessageParser.getBcc().size() == 0);
431         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
432         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
433         assertFalse(mimeMessageParser.hasAttachments());
434     }
435 
436     @Test
437     public void testParseCreatedHtmlEmailWithMixedContent() throws Exception
438     {
439         final Session session = Session.getDefaultInstance(new Properties());
440 
441         final HtmlEmail email = new HtmlEmail();
442 
443         email.setMailSession(session);
444 
445         email.setFrom("test_from@apache.org");
446         email.setSubject("Test Subject");
447         email.addTo("test_to@apache.org");
448         email.setTextMsg("My test message");
449         email.setHtmlMsg("<p>My HTML message</p>");
450 
451         email.buildMimeMessage();
452         final MimeMessage msg = email.getMimeMessage();
453 
454         final MimeMessageParser mimeMessageParser = new MimeMessageParser(msg);
455         mimeMessageParser.parse();
456 
457         assertEquals("Test Subject", mimeMessageParser.getSubject());
458         assertNotNull(mimeMessageParser.getMimeMessage());
459         assertTrue(mimeMessageParser.isMultipart());
460         assertTrue(mimeMessageParser.hasHtmlContent());
461         assertTrue(mimeMessageParser.hasPlainContent());
462         assertNotNull(mimeMessageParser.getPlainContent());
463         assertNotNull(mimeMessageParser.getHtmlContent());
464         assertTrue(mimeMessageParser.getTo().size() == 1);
465         assertTrue(mimeMessageParser.getCc().size() == 0);
466         assertTrue(mimeMessageParser.getBcc().size() == 0);
467         assertEquals("test_from@apache.org", mimeMessageParser.getFrom());
468         assertEquals("test_from@apache.org", mimeMessageParser.getReplyTo());
469         assertFalse(mimeMessageParser.hasAttachments());
470     }
471 }