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.IOException; |
---|
23 | import java.util.Date; |
---|
24 | |
---|
25 | import org.apache.james.mime4j.dom.TextBody; |
---|
26 | import org.apache.james.mime4j.field.address.AddressBuilder; |
---|
27 | import org.apache.james.mime4j.field.address.ParseException; |
---|
28 | import org.apache.james.mime4j.message.MessageImpl; |
---|
29 | import org.apache.james.mime4j.message.MimeWriter; |
---|
30 | import org.apache.james.mime4j.storage.StorageBodyFactory; |
---|
31 | |
---|
32 | /** |
---|
33 | * This example generates a message very similar to the one from RFC 5322 |
---|
34 | * Appendix A.1.1. |
---|
35 | */ |
---|
36 | public class TextPlainMessage { |
---|
37 | public static void main(String[] args) throws IOException, ParseException { |
---|
38 | // 1) start with an empty message |
---|
39 | |
---|
40 | MessageImpl message = new MessageImpl(); |
---|
41 | |
---|
42 | // 2) set header fields |
---|
43 | |
---|
44 | // Date and From are required fields |
---|
45 | message.setDate(new Date()); |
---|
46 | message.setFrom(AddressBuilder.DEFAULT.parseMailbox("John Doe <jdoe@machine.example>")); |
---|
47 | |
---|
48 | // Message-ID should be present |
---|
49 | message.createMessageId("machine.example"); |
---|
50 | |
---|
51 | // set some optional fields |
---|
52 | message.setTo(AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>")); |
---|
53 | message.setSubject("Saying Hello"); |
---|
54 | |
---|
55 | // 3) set a text body |
---|
56 | |
---|
57 | StorageBodyFactory bodyFactory = new StorageBodyFactory(); |
---|
58 | TextBody body = bodyFactory.textBody("This is a message just to " |
---|
59 | + "say hello.\r\nSo, \"Hello\"."); |
---|
60 | |
---|
61 | // note that setText also sets the Content-Type header field |
---|
62 | message.setText(body); |
---|
63 | |
---|
64 | // 4) print message to standard output |
---|
65 | |
---|
66 | MimeWriter.DEFAULT.writeMessage(message, System.out); |
---|
67 | |
---|
68 | // 5) message is no longer needed and should be disposed of |
---|
69 | |
---|
70 | message.dispose(); |
---|
71 | } |
---|
72 | } |
---|