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 java.nio.charset.StandardCharsets;
20  
21  import javax.mail.Message;
22  import javax.mail.MessagingException;
23  import javax.mail.Session;
24  import javax.mail.internet.AddressException;
25  import javax.mail.internet.InternetAddress;
26  import javax.mail.internet.MimeMessage;
27  
28  import org.apache.logging.log4j.core.util.Builder;
29  
30  /**
31   * Builder for {@link MimeMessage} instances.
32   */
33  public class MimeMessageBuilder implements Builder<MimeMessage> {
34      private final MimeMessage message;
35  
36      public MimeMessageBuilder(final Session session) {
37          message = new MimeMessage(session);
38      }
39  
40      public MimeMessageBuilder setFrom(final String from) throws MessagingException {
41          final InternetAddress address = parseAddress(from);
42  
43          if (null != address) {
44              message.setFrom(address);
45          } else {
46              try {
47                  message.setFrom();
48              } catch (final Exception ex) {
49                  message.setFrom((InternetAddress) null);
50              }
51          }
52          return this;
53      }
54  
55      public MimeMessageBuilder setReplyTo(final String replyTo) throws MessagingException {
56          final InternetAddress[] addresses = parseAddresses(replyTo);
57  
58          if (null != addresses) {
59              message.setReplyTo(addresses);
60          }
61          return this;
62      }
63  
64      public MimeMessageBuilder setRecipients(final Message.RecipientType recipientType, final String recipients)
65          throws MessagingException {
66          final InternetAddress[] addresses = parseAddresses(recipients);
67  
68          if (null != addresses) {
69              message.setRecipients(recipientType, addresses);
70          }
71          return this;
72      }
73  
74      public MimeMessageBuilder setSubject(final String subject) throws MessagingException {
75          if (subject != null) {
76              message.setSubject(subject, StandardCharsets.UTF_8.name());
77          }
78          return this;
79      }
80  
81      /**
82       * @deprecated Use {@link #build()}.
83       */
84      @Deprecated
85      public MimeMessage getMimeMessage() {
86          return build();
87      }
88  
89      @Override
90      public MimeMessage build() {
91          return message;
92      }
93  
94      private static InternetAddress parseAddress(final String address) throws AddressException {
95          return address == null ? null : new InternetAddress(address);
96      }
97  
98      private static InternetAddress[] parseAddresses(final String addresses) throws AddressException {
99          return addresses == null ? null : InternetAddress.parse(addresses, true);
100     }
101 }