001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.mail2.javax.util;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.FileOutputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.OutputStream;
025import java.nio.charset.StandardCharsets;
026import java.nio.file.Files;
027import java.nio.file.OpenOption;
028import java.nio.file.Path;
029
030import javax.mail.MessagingException;
031import javax.mail.Session;
032import javax.mail.internet.MimeMessage;
033import javax.mail.util.SharedByteArrayInputStream;
034
035/**
036 * Creates {@link MimeMessage} instances and other helper methods.
037 *
038 * @since 1.3
039 */
040public final class MimeMessageUtils {
041
042    /**
043     * Creates a MimeMessage.
044     *
045     * @param session the mail session.
046     * @param source  the input data.
047     * @return the MimeMessage.
048     * @throws MessagingException creating the MimeMessage failed.
049     * @throws IOException        creating the MimeMessage failed.
050     */
051    public static MimeMessage createMimeMessage(final Session session, final byte[] source) throws MessagingException, IOException {
052        try (InputStream inputStream = new SharedByteArrayInputStream(source)) {
053            return new MimeMessage(session, inputStream);
054        }
055    }
056
057    /**
058     * Creates a MimeMessage.
059     *
060     * @param session the mail session.
061     * @param source  the input data.
062     * @return the MimeMessage.
063     * @throws MessagingException creating the MimeMessage failed.
064     * @throws IOException        creating the MimeMessage failed.
065     */
066    public static MimeMessage createMimeMessage(final Session session, final File source) throws MessagingException, IOException {
067        try (InputStream inputStream = new FileInputStream(source)) {
068            return createMimeMessage(session, inputStream);
069        }
070    }
071
072    /**
073     * Creates a MimeMessage.
074     *
075     * @param session the mail session.
076     * @param source  the input data.
077     * @return the MimeMessage.
078     * @throws MessagingException creating the MimeMessage failed.
079     */
080    public static MimeMessage createMimeMessage(final Session session, final InputStream source) throws MessagingException {
081        return new MimeMessage(session, source);
082    }
083
084    /**
085     * Creates a MimeMessage.
086     *
087     * @param session the mail session.
088     * @param source  the input data.
089     * @param options options specifying how the file is opened.
090     * @return the MimeMessage.
091     * @throws MessagingException creating the MimeMessage failed.
092     * @throws IOException        creating the MimeMessage failed.
093     */
094    public static MimeMessage createMimeMessage(final Session session, final Path source, final OpenOption... options) throws MessagingException, IOException {
095        try (InputStream inputStream = Files.newInputStream(source, options)) {
096            return createMimeMessage(session, inputStream);
097        }
098    }
099
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}