| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.apache.james.mime4j.samples.dom; |
| 21 |
|
| 22 |
import java.io.ByteArrayInputStream; |
| 23 |
import java.nio.charset.StandardCharsets; |
| 24 |
|
| 25 |
import org.apache.commons.io.IOUtils; |
| 26 |
import org.apache.james.mime4j.dom.Message; |
| 27 |
import org.apache.james.mime4j.dom.TextBody; |
| 28 |
import org.apache.james.mime4j.message.DefaultMessageBuilder; |
| 29 |
import org.apache.james.mime4j.stream.MimeConfig; |
| 30 |
|
| 31 |
public class ParsingMessage { |
| 32 |
private static String MESSAGE = "MIME-Version: 1.0\r\n" + |
| 33 |
"From: John Doe <[email protected]>\r\n" + |
| 34 |
"To: Mary Smith <[email protected]>\r\n" + |
| 35 |
"Subject: Saying Hello\r\n" + |
| 36 |
"Date: Thu, 24 Jun 2021 14:04:28 +0700\r\n" + |
| 37 |
"Message-ID:\r\n" + |
| 38 |
" <Mime4j.0.8520adcfe13bd088.17a3cd59575@interview1-HP-ProBook-440-G6>\r\n" + |
| 39 |
"Content-Type: text/plain; charset=ISO-8859-1\r\n" + |
| 40 |
"\r\n" + |
| 41 |
"This is a message just to say hello.\r\n" + |
| 42 |
"So, \"Hello\".\r\n"; |
| 43 |
|
| 44 |
public static void main(String[] args) throws Exception { |
| 45 |
DefaultMessageBuilder defaultMessageBuilder = new DefaultMessageBuilder(); |
| 46 |
defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE); |
| 47 |
|
| 48 |
Message message = defaultMessageBuilder.parseMessage(new ByteArrayInputStream(MESSAGE.getBytes(StandardCharsets.UTF_8))); |
| 49 |
|
| 50 |
System.out.println("The subject is " + message.getSubject()); |
| 51 |
|
| 52 |
TextBody textBody = (TextBody) message.getBody(); |
| 53 |
System.out.println("------------------------------"); |
| 54 |
System.out.println("Content: " + IOUtils.toString(textBody.getInputStream(), textBody.getMimeCharset())); |
| 55 |
System.out.println("------------------------------"); |
| 56 |
} |
| 57 |
} |