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.mail.util;
018
019import javax.mail.MessagingException;
020import javax.mail.Session;
021import javax.mail.internet.MimeMessage;
022import java.io.ByteArrayInputStream;
023import java.io.File;
024import java.io.FileInputStream;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.InputStream;
028
029/**
030 * Static helper methods.
031 *
032 * @since 1.3
033 */
034public final class MimeMessageUtils
035{
036    /**
037     * Instances should NOT be constructed in standard programming.
038     */
039    private MimeMessageUtils()
040    {
041        super();
042    }
043
044    /**
045     * Create a MimeMessage.
046     *
047     * @param session the mail session
048     * @param source the input data
049     * @return the MimeMessage
050     * @throws MessagingException creating the MimeMessage failed
051     * @throws IOException creating the MimeMessage failed
052     */
053    public static MimeMessage createMimeMessage(final Session session, final byte[] source)
054        throws MessagingException, IOException
055    {
056        ByteArrayInputStream is = null;
057
058        try
059        {
060            is = new ByteArrayInputStream(source);
061            return new MimeMessage(session, is);
062        }
063        finally
064        {
065            if (is != null)
066            {
067                is.close();
068            }
069        }
070    }
071
072    /**
073     * Create 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     * @throws IOException creating the MimeMessage failed
080     */
081    public static MimeMessage createMimeMessage(final Session session, final File source)
082        throws MessagingException, IOException
083    {
084        FileInputStream is = null;
085
086        try
087        {
088            is = new FileInputStream(source);
089            return createMimeMessage(session, is);
090        }
091        finally
092        {
093            if (is != null)
094            {
095                is.close();
096            }
097        }
098    }
099
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}