View Javadoc

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