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 * @version $Id: MimeMessageUtils.html 952467 2015-05-23 18:45:36Z tn $
034 */
035public final class MimeMessageUtils
036{
037    /**
038     * Instances should NOT be constructed in standard programming.
039     */
040    private MimeMessageUtils()
041    {
042        super();
043    }
044
045    /**
046     * Create a MimeMessage.
047     *
048     * @param session the mail session
049     * @param source the input data
050     * @return the MimeMessage
051     * @throws MessagingException creating the MimeMessage failed
052     * @throws IOException creating the MimeMessage failed
053     */
054    public static MimeMessage createMimeMessage(final Session session, final byte[] source)
055        throws MessagingException, IOException
056    {
057        ByteArrayInputStream is = null;
058
059        try
060        {
061            is = new ByteArrayInputStream(source);
062            return new MimeMessage(session, is);
063        }
064        finally
065        {
066            if (is != null)
067            {
068                is.close();
069            }
070        }
071    }
072
073    /**
074     * Create a MimeMessage.
075     *
076     * @param session the mail session
077     * @param source the input data
078     * @return the MimeMessage
079     * @throws MessagingException creating the MimeMessage failed
080     * @throws IOException creating the MimeMessage failed
081     */
082    public static MimeMessage createMimeMessage(final Session session, final File source)
083        throws MessagingException, IOException
084    {
085        FileInputStream is = null;
086
087        try
088        {
089            is = new FileInputStream(source);
090            return createMimeMessage(session, is);
091        }
092        finally
093        {
094            if (is != null)
095            {
096                is.close();
097            }
098        }
099    }
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(final Session session, final 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(final Session session, final String source)
125        throws MessagingException, IOException
126    {
127        ByteArrayInputStream is = null;
128
129        try
130        {
131            final 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(final MimeMessage mimeMessage, final 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 (final Exception e)
182                {
183                    e.printStackTrace();
184                }
185            }
186        }
187    }
188}