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.awt.BasicStroke; |
---|
23 | import java.awt.Color; |
---|
24 | import java.awt.Graphics2D; |
---|
25 | import java.awt.RenderingHints; |
---|
26 | import java.awt.image.BufferedImage; |
---|
27 | import java.io.IOException; |
---|
28 | import java.util.Date; |
---|
29 | |
---|
30 | import javax.imageio.ImageIO; |
---|
31 | |
---|
32 | import org.apache.james.mime4j.dom.BinaryBody; |
---|
33 | import org.apache.james.mime4j.dom.Multipart; |
---|
34 | import org.apache.james.mime4j.dom.TextBody; |
---|
35 | import org.apache.james.mime4j.field.address.AddressBuilder; |
---|
36 | import org.apache.james.mime4j.message.BodyPart; |
---|
37 | import org.apache.james.mime4j.message.MessageImpl; |
---|
38 | import org.apache.james.mime4j.message.MimeWriter; |
---|
39 | import org.apache.james.mime4j.message.MultipartImpl; |
---|
40 | import org.apache.james.mime4j.storage.Storage; |
---|
41 | import org.apache.james.mime4j.storage.StorageBodyFactory; |
---|
42 | import org.apache.james.mime4j.storage.StorageOutputStream; |
---|
43 | import org.apache.james.mime4j.storage.StorageProvider; |
---|
44 | |
---|
45 | /** |
---|
46 | * Creates a multipart/mixed message that consists of a text/plain and an |
---|
47 | * image/png part. The image is created on the fly; a similar technique can be |
---|
48 | * used to create PDF or XML attachments, for example. |
---|
49 | */ |
---|
50 | public class MultipartMessage { |
---|
51 | |
---|
52 | public static void main(String[] args) throws Exception { |
---|
53 | // 1) start with an empty message |
---|
54 | |
---|
55 | MessageImpl message = new MessageImpl(); |
---|
56 | |
---|
57 | // 2) set header fields |
---|
58 | |
---|
59 | // Date and From are required fields |
---|
60 | message.setDate(new Date()); |
---|
61 | message.setFrom(AddressBuilder.DEFAULT.parseMailbox("John Doe <jdoe@machine.example>")); |
---|
62 | |
---|
63 | // Message-ID should be present |
---|
64 | message.createMessageId("machine.example"); |
---|
65 | |
---|
66 | // set some optional fields |
---|
67 | message.setTo(AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>")); |
---|
68 | message.setSubject("An image for you"); |
---|
69 | |
---|
70 | // 3) set a multipart body |
---|
71 | |
---|
72 | Multipart multipart = new MultipartImpl("mixed"); |
---|
73 | |
---|
74 | // a multipart may have a preamble |
---|
75 | multipart.setPreamble("This is a multi-part message in MIME format."); |
---|
76 | |
---|
77 | // first part is text/plain |
---|
78 | StorageBodyFactory bodyFactory = new StorageBodyFactory(); |
---|
79 | BodyPart textPart = createTextPart(bodyFactory, "Why so serious?"); |
---|
80 | multipart.addBodyPart(textPart); |
---|
81 | |
---|
82 | // second part is image/png (image is created on the fly) |
---|
83 | BufferedImage image = renderSampleImage(); |
---|
84 | BodyPart imagePart = createImagePart(bodyFactory, image); |
---|
85 | multipart.addBodyPart(imagePart); |
---|
86 | |
---|
87 | // setMultipart also sets the Content-Type header field |
---|
88 | message.setMultipart(multipart); |
---|
89 | |
---|
90 | // 4) print message to standard output |
---|
91 | |
---|
92 | MimeWriter.DEFAULT.writeMessage(message, System.out); |
---|
93 | |
---|
94 | // 5) message is no longer needed and should be disposed of |
---|
95 | |
---|
96 | message.dispose(); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Creates a text part from the specified string. |
---|
101 | */ |
---|
102 | private static BodyPart createTextPart(StorageBodyFactory bodyFactory, String text) { |
---|
103 | // Use UTF-8 to encode the specified text |
---|
104 | TextBody body = bodyFactory.textBody(text, "UTF-8"); |
---|
105 | |
---|
106 | // Create a text/plain body part |
---|
107 | BodyPart bodyPart = new BodyPart(); |
---|
108 | bodyPart.setText(body); |
---|
109 | bodyPart.setContentTransferEncoding("quoted-printable"); |
---|
110 | |
---|
111 | return bodyPart; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Creates a binary part from the specified image. |
---|
116 | */ |
---|
117 | private static BodyPart createImagePart(StorageBodyFactory bodyFactory, |
---|
118 | BufferedImage image) throws IOException { |
---|
119 | // Create a binary message body from the image |
---|
120 | StorageProvider storageProvider = bodyFactory.getStorageProvider(); |
---|
121 | Storage storage = storeImage(storageProvider, image, "png"); |
---|
122 | BinaryBody body = bodyFactory.binaryBody(storage); |
---|
123 | |
---|
124 | // Create a body part with the correct MIME-type and transfer encoding |
---|
125 | BodyPart bodyPart = new BodyPart(); |
---|
126 | bodyPart.setBody(body, "image/png"); |
---|
127 | bodyPart.setContentTransferEncoding("base64"); |
---|
128 | |
---|
129 | // Specify a filename in the Content-Disposition header (implicitly sets |
---|
130 | // the disposition type to "attachment") |
---|
131 | bodyPart.setFilename("smiley.png"); |
---|
132 | |
---|
133 | return bodyPart; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Stores the specified image in a Storage object. |
---|
138 | */ |
---|
139 | private static Storage storeImage(StorageProvider storageProvider, |
---|
140 | BufferedImage image, String formatName) throws IOException { |
---|
141 | // An output stream that is capable of building a Storage object. |
---|
142 | StorageOutputStream out = storageProvider.createStorageOutputStream(); |
---|
143 | |
---|
144 | // Write the image to our output stream. A StorageOutputStream can be |
---|
145 | // used to create attachments using any API that supports writing a |
---|
146 | // document to an output stream, e.g. iText's PdfWriter. |
---|
147 | ImageIO.write(image, formatName, out); |
---|
148 | |
---|
149 | // Implicitly closes the output stream and returns the data that has |
---|
150 | // been written to it. |
---|
151 | return out.toStorage(); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Draws an image; unrelated to Mime4j. |
---|
156 | */ |
---|
157 | private static BufferedImage renderSampleImage() { |
---|
158 | System.setProperty("java.awt.headless", "true"); |
---|
159 | |
---|
160 | final int size = 100; |
---|
161 | |
---|
162 | BufferedImage img = new BufferedImage(size, size, |
---|
163 | BufferedImage.TYPE_BYTE_GRAY); |
---|
164 | |
---|
165 | Graphics2D gfx = img.createGraphics(); |
---|
166 | gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
---|
167 | RenderingHints.VALUE_ANTIALIAS_ON); |
---|
168 | gfx.setStroke(new BasicStroke(size / 40f, BasicStroke.CAP_ROUND, |
---|
169 | BasicStroke.JOIN_ROUND)); |
---|
170 | |
---|
171 | gfx.setColor(Color.BLACK); |
---|
172 | gfx.setBackground(Color.WHITE); |
---|
173 | gfx.clearRect(0, 0, size, size); |
---|
174 | |
---|
175 | int b = size / 30; |
---|
176 | gfx.drawOval(b, b, size - 1 - 2 * b, size - 1 - 2 * b); |
---|
177 | |
---|
178 | int esz = size / 7; |
---|
179 | int ex = (int) (0.27f * size); |
---|
180 | gfx.drawOval(ex, ex, esz, esz); |
---|
181 | gfx.drawOval(size - 1 - esz - ex, ex, esz, esz); |
---|
182 | |
---|
183 | b = size / 5; |
---|
184 | gfx.drawArc(b, b, size - 1 - 2 * b, size - 1 - 2 * b, 200, 140); |
---|
185 | |
---|
186 | return img; |
---|
187 | } |
---|
188 | |
---|
189 | } |
---|