Debian Codegraph @

examples/src/main/java/org/apache/james/mime4j/samples/dom/ParsingMessage.java

raw symbols SCIP
1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
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 }