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.mail2.javax.util;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.nio.charset.StandardCharsets;
26  import java.nio.file.Files;
27  import java.nio.file.OpenOption;
28  import java.nio.file.Path;
29  
30  import javax.mail.MessagingException;
31  import javax.mail.Session;
32  import javax.mail.internet.MimeMessage;
33  import javax.mail.util.SharedByteArrayInputStream;
34  
35  /**
36   * Creates {@link MimeMessage} instances and other helper methods.
37   *
38   * @since 1.3
39   */
40  public final class MimeMessageUtils {
41  
42      /**
43       * Creates a MimeMessage.
44       *
45       * @param session the mail session.
46       * @param source  the input data.
47       * @return the MimeMessage.
48       * @throws MessagingException creating the MimeMessage failed.
49       * @throws IOException        creating the MimeMessage failed.
50       */
51      public static MimeMessage createMimeMessage(final Session session, final byte[] source) throws MessagingException, IOException {
52          try (InputStream inputStream = new SharedByteArrayInputStream(source)) {
53              return new MimeMessage(session, inputStream);
54          }
55      }
56  
57      /**
58       * Creates a MimeMessage.
59       *
60       * @param session the mail session.
61       * @param source  the input data.
62       * @return the MimeMessage.
63       * @throws MessagingException creating the MimeMessage failed.
64       * @throws IOException        creating the MimeMessage failed.
65       */
66      public static MimeMessage createMimeMessage(final Session session, final File source) throws MessagingException, IOException {
67          try (InputStream inputStream = new FileInputStream(source)) {
68              return createMimeMessage(session, inputStream);
69          }
70      }
71  
72      /**
73       * Creates 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       */
80      public static MimeMessage createMimeMessage(final Session session, final InputStream source) throws MessagingException {
81          return new MimeMessage(session, source);
82      }
83  
84      /**
85       * Creates a MimeMessage.
86       *
87       * @param session the mail session.
88       * @param source  the input data.
89       * @param options options specifying how the file is opened.
90       * @return the MimeMessage.
91       * @throws MessagingException creating the MimeMessage failed.
92       * @throws IOException        creating the MimeMessage failed.
93       */
94      public static MimeMessage createMimeMessage(final Session session, final Path source, final OpenOption... options) throws MessagingException, IOException {
95          try (InputStream inputStream = Files.newInputStream(source, options)) {
96              return createMimeMessage(session, inputStream);
97          }
98      }
99  
100     /**
101      * Creates a MimeMessage using the platform's default character encoding.
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      * @throws IOException        creating the MimeMessage failed.
108      */
109     public static MimeMessage createMimeMessage(final Session session, final String source) throws MessagingException, IOException {
110         // RFC1341: https://www.w3.org/Protocols/rfc1341/7_1_Text.html
111         return createMimeMessage(session, source.getBytes(StandardCharsets.US_ASCII));
112     }
113 
114     /**
115      * Writes a MimeMessage into a file.
116      *
117      * @param mimeMessage the MimeMessage to write.
118      * @param resultFile  the file containing the MimeMessage.
119      * @throws MessagingException accessing MimeMessage failed.
120      * @throws IOException        writing the MimeMessage failed.
121      */
122     public static void writeMimeMessage(final MimeMessage mimeMessage, final File resultFile) throws MessagingException, IOException {
123         if (!resultFile.getParentFile().exists() && !resultFile.getParentFile().mkdirs()) {
124             throw new IOException("Failed to create the following parent directories: " + resultFile.getParentFile());
125         }
126         try (OutputStream outputStream = new FileOutputStream(resultFile)) {
127             mimeMessage.writeTo(outputStream);
128             outputStream.flush();
129         }
130     }
131 
132     /**
133      * Instances should NOT be constructed in standard programming.
134      */
135     private MimeMessageUtils() {
136     }
137 }