commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/util/MimeMessageUtils.java [39:132]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public final class MimeMessageUtils {

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     * @throws IOException        creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final byte[] source) throws MessagingException, IOException {
        try (InputStream inputStream = new SharedByteArrayInputStream(source)) {
            return new MimeMessage(session, inputStream);
        }
    }

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     * @throws IOException        creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final File source) throws MessagingException, IOException {
        return createMimeMessage(session, source.toPath());
    }

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final InputStream source) throws MessagingException {
        return new MimeMessage(session, source);
    }

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @param options options specifying how the file is opened.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     * @throws IOException        creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final Path source, final OpenOption... options) throws MessagingException, IOException {
        try (InputStream inputStream = Files.newInputStream(source, options)) {
            return createMimeMessage(session, inputStream);
        }
    }

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

    /**
     * Writes a MimeMessage into a file.
     *
     * @param mimeMessage the MimeMessage to write.
     * @param resultFile  the file containing the MimeMessage.
     * @throws MessagingException accessing MimeMessage failed.
     * @throws IOException        writing the MimeMessage failed.
     */
    public static void writeMimeMessage(final MimeMessage mimeMessage, final File resultFile) throws MessagingException, IOException {
        if (!resultFile.getParentFile().exists() && !resultFile.getParentFile().mkdirs()) {
            throw new IOException("Failed to create the following parent directories: " + resultFile.getParentFile());
        }
        try (OutputStream outputStream = new FileOutputStream(resultFile)) {
            mimeMessage.writeTo(outputStream);
            outputStream.flush();
        }
    }

    /**
     * Instances should NOT be constructed in standard programming.
     */
    private MimeMessageUtils() {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/util/MimeMessageUtils.java [39:132]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public final class MimeMessageUtils {

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     * @throws IOException        creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final byte[] source) throws MessagingException, IOException {
        try (InputStream inputStream = new SharedByteArrayInputStream(source)) {
            return new MimeMessage(session, inputStream);
        }
    }

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     * @throws IOException        creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final File source) throws MessagingException, IOException {
        return createMimeMessage(session, source.toPath());
    }

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final InputStream source) throws MessagingException {
        return new MimeMessage(session, source);
    }

    /**
     * Creates a MimeMessage.
     *
     * @param session the mail session.
     * @param source  the input data.
     * @param options options specifying how the file is opened.
     * @return the MimeMessage.
     * @throws MessagingException creating the MimeMessage failed.
     * @throws IOException        creating the MimeMessage failed.
     */
    public static MimeMessage createMimeMessage(final Session session, final Path source, final OpenOption... options) throws MessagingException, IOException {
        try (InputStream inputStream = Files.newInputStream(source, options)) {
            return createMimeMessage(session, inputStream);
        }
    }

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

    /**
     * Writes a MimeMessage into a file.
     *
     * @param mimeMessage the MimeMessage to write.
     * @param resultFile  the file containing the MimeMessage.
     * @throws MessagingException accessing MimeMessage failed.
     * @throws IOException        writing the MimeMessage failed.
     */
    public static void writeMimeMessage(final MimeMessage mimeMessage, final File resultFile) throws MessagingException, IOException {
        if (!resultFile.getParentFile().exists() && !resultFile.getParentFile().mkdirs()) {
            throw new IOException("Failed to create the following parent directories: " + resultFile.getParentFile());
        }
        try (OutputStream outputStream = new FileOutputStream(resultFile)) {
            mimeMessage.writeTo(outputStream);
            outputStream.flush();
        }
    }

    /**
     * Instances should NOT be constructed in standard programming.
     */
    private MimeMessageUtils() {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



