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  /**
27   *  Helper class for SMTPManager.
28   */
29  public class MimeMessageBuilder {
30      private final MimeMessage message;
31  
32      public MimeMessageBuilder(final Session session) {
33          message = new MimeMessage(session);
34      }
35  
36      public MimeMessageBuilder setFrom(final String from) throws MessagingException {
37          final InternetAddress address = parseAddress(from);
38  
39          if (null != address) {
40              message.setFrom(address);
41          } else {
42              try {
43                  message.setFrom();
44              } catch (final Exception ex) {
45                  message.setFrom(null);
46              }
47          }
48          return this;
49      }
50  
51      public MimeMessageBuilder setReplyTo(final String replyTo) throws MessagingException {
52          final InternetAddress[] addresses = parseAddresses(replyTo);
53  
54          if (null != addresses) {
55              message.setReplyTo(addresses);
56          }
57          return this;
58      }
59  
60      public MimeMessageBuilder setRecipients(final Message.RecipientType recipientType, final String recipients)
61          throws MessagingException {
62          final InternetAddress[] addresses = parseAddresses(recipients);
63  
64          if (null != addresses) {
65              message.setRecipients(recipientType, addresses);
66          }
67          return this;
68      }
69  
70      public MimeMessageBuilder setSubject(final String subject) throws MessagingException {
71          if (subject != null) {
72              message.setSubject(subject, "UTF-8");
73          }
74          return this;
75      }
76  
77      public MimeMessage getMimeMessage() {
78          return message;
79      }
80  
81      private static InternetAddress parseAddress(final String address) throws AddressException {
82          return address == null ? null : new InternetAddress(address);
83      }
84  
85      private static InternetAddress[] parseAddresses(final String addresses) throws AddressException {
86          return addresses == null ? null : InternetAddress.parse(addresses, true);
87      }
88  }