View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17  package org.apache.logging.log4j.core.net;
    18
    19  import javax.mail.Message;
    20  import javax.mail.MessagingException;
    21  import javax.mail.Session;
    22  import javax.mail.internet.AddressException;
    23  import javax.mail.internet.InternetAddress;
    24  import javax.mail.internet.MimeMessage;
    25
    26  import org.apache.logging.log4j.core.util.Constants;
    27
    28  /**
    29   *  Helper class for SmtpManager.
    30   */
    31  public class MimeMessageBuilder {
    32      private final MimeMessage message;
    33
    34      public MimeMessageBuilder(final Session session) {
    35          message = new MimeMessage(session);
    36      }
    37
    38      public MimeMessageBuilder setFrom(final String from) throws MessagingException {
    39          final InternetAddress address = parseAddress(from);
    40
    41          if (null != address) {
    42              message.setFrom(address);
    43          } else {
    44              try {
    45                  message.setFrom();
    46              } catch (final Exception ex) {
    47                  message.setFrom((InternetAddress) null);
    48              }
    49          }
    50          return this;
    51      }
    52
    53      public MimeMessageBuilder setReplyTo(final String replyTo) throws MessagingException {
    54          final InternetAddress[] addresses = parseAddresses(replyTo);
    55
    56          if (null != addresses) {
    57              message.setReplyTo(addresses);
    58          }
    59          return this;
    60      }
    61
    62      public MimeMessageBuilder setRecipients(final Message.RecipientType recipientType, final String recipients)
    63          throws MessagingException {
    64          final InternetAddress[] addresses = parseAddresses(recipients);
    65
    66          if (null != addresses) {
    67              message.setRecipients(recipientType, addresses);
    68          }
    69          return this;
    70      }
    71
    72      public MimeMessageBuilder setSubject(final String subject) throws MessagingException {
    73          if (subject != null) {
    74              message.setSubject(subject, Constants.UTF_8.name());
    75          }
    76          return this;
    77      }
    78
    79      public MimeMessage getMimeMessage() {
    80          return message;
    81      }
    82
    83      private static InternetAddress parseAddress(final String address) throws AddressException {
    84          return address == null ? null : new InternetAddress(address);
    85      }
    86
    87      private static InternetAddress[] parseAddresses(final String addresses) throws AddressException {
    88          return addresses == null ? null : InternetAddress.parse(addresses, true);
    89      }
    90  }