MimeMessageUtils.java

  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. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.nio.charset.StandardCharsets;
  25. import java.nio.file.Files;
  26. import java.nio.file.OpenOption;
  27. import java.nio.file.Path;

  28. import javax.mail.MessagingException;
  29. import javax.mail.Session;
  30. import javax.mail.internet.MimeMessage;
  31. import javax.mail.util.SharedByteArrayInputStream;

  32. /**
  33.  * Creates {@link MimeMessage} instances and other helper methods.
  34.  *
  35.  * @since 1.3
  36.  */
  37. public final class MimeMessageUtils {

  38.     /**
  39.      * Creates a MimeMessage.
  40.      *
  41.      * @param session the mail session.
  42.      * @param source  the input data.
  43.      * @return the MimeMessage.
  44.      * @throws MessagingException creating the MimeMessage failed.
  45.      * @throws IOException        creating the MimeMessage failed.
  46.      */
  47.     public static MimeMessage createMimeMessage(final Session session, final byte[] source) throws MessagingException, IOException {
  48.         try (InputStream inputStream = new SharedByteArrayInputStream(source)) {
  49.             return new MimeMessage(session, inputStream);
  50.         }
  51.     }

  52.     /**
  53.      * Creates a MimeMessage.
  54.      *
  55.      * @param session the mail session.
  56.      * @param source  the input data.
  57.      * @return the MimeMessage.
  58.      * @throws MessagingException creating the MimeMessage failed.
  59.      * @throws IOException        creating the MimeMessage failed.
  60.      */
  61.     public static MimeMessage createMimeMessage(final Session session, final File source) throws MessagingException, IOException {
  62.         try (InputStream inputStream = new FileInputStream(source)) {
  63.             return createMimeMessage(session, inputStream);
  64.         }
  65.     }

  66.     /**
  67.      * Creates a MimeMessage.
  68.      *
  69.      * @param session the mail session.
  70.      * @param source  the input data.
  71.      * @return the MimeMessage.
  72.      * @throws MessagingException creating the MimeMessage failed.
  73.      */
  74.     public static MimeMessage createMimeMessage(final Session session, final InputStream source) throws MessagingException {
  75.         return new MimeMessage(session, source);
  76.     }

  77.     /**
  78.      * Creates a MimeMessage.
  79.      *
  80.      * @param session the mail session.
  81.      * @param source  the input data.
  82.      * @param options options specifying how the file is opened.
  83.      * @return the MimeMessage.
  84.      * @throws MessagingException creating the MimeMessage failed.
  85.      * @throws IOException        creating the MimeMessage failed.
  86.      */
  87.     public static MimeMessage createMimeMessage(final Session session, final Path source, final OpenOption... options) throws MessagingException, IOException {
  88.         try (InputStream inputStream = Files.newInputStream(source, options)) {
  89.             return createMimeMessage(session, inputStream);
  90.         }
  91.     }

  92.     /**
  93.      * Creates a MimeMessage using the platform's default character encoding.
  94.      *
  95.      * @param session the mail session.
  96.      * @param source  the input data.
  97.      * @return the MimeMessage.
  98.      * @throws MessagingException creating the MimeMessage failed.
  99.      * @throws IOException        creating the MimeMessage failed.
  100.      */
  101.     public static MimeMessage createMimeMessage(final Session session, final String source) throws MessagingException, IOException {
  102.         // RFC1341: https://www.w3.org/Protocols/rfc1341/7_1_Text.html
  103.         return createMimeMessage(session, source.getBytes(StandardCharsets.US_ASCII));
  104.     }

  105.     /**
  106.      * Writes a MimeMessage into a file.
  107.      *
  108.      * @param mimeMessage the MimeMessage to write.
  109.      * @param resultFile  the file containing the MimeMessage.
  110.      * @throws MessagingException accessing MimeMessage failed.
  111.      * @throws IOException        writing the MimeMessage failed.
  112.      */
  113.     public static void writeMimeMessage(final MimeMessage mimeMessage, final File resultFile) throws MessagingException, IOException {
  114.         if (!resultFile.getParentFile().exists() && !resultFile.getParentFile().mkdirs()) {
  115.             throw new IOException("Failed to create the following parent directories: " + resultFile.getParentFile());
  116.         }
  117.         try (OutputStream outputStream = new FileOutputStream(resultFile)) {
  118.             mimeMessage.writeTo(outputStream);
  119.             outputStream.flush();
  120.         }
  121.     }

  122.     /**
  123.      * Instances should NOT be constructed in standard programming.
  124.      */
  125.     private MimeMessageUtils() {
  126.     }
  127. }