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 javax.mail.MessagingException;
20 import javax.mail.Session;
21 import javax.mail.internet.MimeMessage;
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28
29 /**
30 * Static helper methods.
31 *
32 * @since 1.3
33 */
34 public final class MimeMessageUtils
35 {
36 /**
37 * Instances should NOT be constructed in standard programming.
38 */
39 private MimeMessageUtils()
40 {
41 super();
42 }
43
44 /**
45 * Create a MimeMessage.
46 *
47 * @param session the mail session
48 * @param source the input data
49 * @return the MimeMessage
50 * @throws MessagingException creating the MimeMessage failed
51 * @throws IOException creating the MimeMessage failed
52 */
53 public static MimeMessage createMimeMessage(final Session session, final byte[] source)
54 throws MessagingException, IOException
55 {
56 ByteArrayInputStream is = null;
57
58 try
59 {
60 is = new ByteArrayInputStream(source);
61 return new MimeMessage(session, is);
62 }
63 finally
64 {
65 if (is != null)
66 {
67 is.close();
68 }
69 }
70 }
71
72 /**
73 * Create a MimeMessage.
74 *
75 * @param session the mail session
76 * @param source the input data
77 * @return the MimeMessage
78 * @throws MessagingException creating the MimeMessage failed
79 * @throws IOException creating the MimeMessage failed
80 */
81 public static MimeMessage createMimeMessage(final Session session, final File source)
82 throws MessagingException, IOException
83 {
84 FileInputStream is = null;
85
86 try
87 {
88 is = new FileInputStream(source);
89 return createMimeMessage(session, is);
90 }
91 finally
92 {
93 if (is != null)
94 {
95 is.close();
96 }
97 }
98 }
99
100 /**
101 * Create a MimeMessage.
102 *
103 * @param session the mail session
104 * @param source the input data
105 * @return the MimeMessage
106 * @throws MessagingException creating the MimeMessage failed
107 */
108 public static MimeMessage createMimeMessage(final Session session, final InputStream source)
109 throws MessagingException
110 {
111 return new MimeMessage(session, source);
112 }
113
114 /**
115 * Create a MimeMessage using the platform's default character encoding.
116 *
117 * @param session the mail session
118 * @param source the input data
119 * @return the MimeMessage
120 * @throws MessagingException creating the MimeMessage failed
121 * @throws IOException creating the MimeMessage failed
122 */
123 public static MimeMessage createMimeMessage(final Session session, final String source)
124 throws MessagingException, IOException
125 {
126 ByteArrayInputStream is = null;
127
128 try
129 {
130 final byte[] byteSource = source.getBytes();
131 is = new ByteArrayInputStream(byteSource);
132 return createMimeMessage(session, is);
133 }
134 finally
135 {
136 if (is != null)
137 {
138 is.close();
139 }
140 }
141 }
142
143 /**
144 * Convenience method to write a MimeMessage into a file.
145 *
146 * @param mimeMessage the MimeMessage to write
147 * @param resultFile the file containing the MimeMessgae
148 * @throws MessagingException accessing MimeMessage failed
149 * @throws IOException writing the MimeMessage failed
150 */
151 public static void writeMimeMessage(final MimeMessage mimeMessage, final File resultFile)
152 throws MessagingException, IOException
153 {
154
155 FileOutputStream fos = null;
156
157 try
158 {
159 if (!resultFile.getParentFile().exists() && !resultFile.getParentFile().mkdirs())
160 {
161 throw new IOException(
162 "Failed to create the following parent directories: "
163 + resultFile.getParentFile());
164 }
165
166 fos = new FileOutputStream(resultFile);
167 mimeMessage.writeTo(fos);
168 fos.flush();
169 fos.close();
170 fos = null;
171 }
172 finally
173 {
174 if (fos != null)
175 {
176 fos.close();
177 }
178 }
179 }
180 }