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.logging.log4j.core.net;
018
019import javax.mail.Message;
020import javax.mail.MessagingException;
021import javax.mail.Session;
022import javax.mail.internet.AddressException;
023import javax.mail.internet.InternetAddress;
024import javax.mail.internet.MimeMessage;
025
026import org.apache.logging.log4j.core.util.Constants;
027
028/**
029 *  Helper class for SmtpManager.
030 */
031public class MimeMessageBuilder {
032    private final MimeMessage message;
033
034    public MimeMessageBuilder(final Session session) {
035        message = new MimeMessage(session);
036    }
037
038    public MimeMessageBuilder setFrom(final String from) throws MessagingException {
039        final InternetAddress address = parseAddress(from);
040
041        if (null != address) {
042            message.setFrom(address);
043        } else {
044            try {
045                message.setFrom();
046            } catch (final Exception ex) {
047                message.setFrom((InternetAddress) null);
048            }
049        }
050        return this;
051    }
052
053    public MimeMessageBuilder setReplyTo(final String replyTo) throws MessagingException {
054        final InternetAddress[] addresses = parseAddresses(replyTo);
055
056        if (null != addresses) {
057            message.setReplyTo(addresses);
058        }
059        return this;
060    }
061
062    public MimeMessageBuilder setRecipients(final Message.RecipientType recipientType, final String recipients)
063        throws MessagingException {
064        final InternetAddress[] addresses = parseAddresses(recipients);
065
066        if (null != addresses) {
067            message.setRecipients(recipientType, addresses);
068        }
069        return this;
070    }
071
072    public MimeMessageBuilder setSubject(final String subject) throws MessagingException {
073        if (subject != null) {
074            message.setSubject(subject, Constants.UTF_8.name());
075        }
076        return this;
077    }
078
079    public MimeMessage getMimeMessage() {
080        return message;
081    }
082
083    private static InternetAddress parseAddress(final String address) throws AddressException {
084        return address == null ? null : new InternetAddress(address);
085    }
086
087    private static InternetAddress[] parseAddresses(final String addresses) throws AddressException {
088        return addresses == null ? null : InternetAddress.parse(addresses, true);
089    }
090}