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