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  
18  package org.apache.commons.net.smtp;
19  
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  import java.util.Locale;
23  
24  /**
25   * This class is used to construct a bare minimum acceptable header for an email message. To construct more complicated headers you should refer to RFC 5322.
26   * When the Java Mail API is finalized, you will be able to use it to compose fully compliant Internet text messages.
27   * <p>
28   * The main purpose of the class is to facilitate the mail sending process, by relieving the programmer from having to explicitly format a simple message
29   * header. For example:
30   * </p>
31   *
32   * <pre>
33   * writer = client.sendMessageData();
34   * if (writer == null) // failure
35   *   return false;
36   * header =
37   *    new SimpleSMTPHeader("foobar@foo.com", "foo@bar.com" "Just testing");
38   * header.addCC("bar@foo.com");
39   * header.addHeaderField("Organization", "Foobar, Inc.");
40   * writer.write(header.toString());
41   * writer.write("This is just a test");
42   * writer.close();
43   * if (!client.completePendingCommand()) // failure
44   *   return false;
45   * </pre>
46   *
47   * @see SMTPClient
48   */
49  
50  public class SimpleSMTPHeader {
51      private final String subject;
52      private final String from;
53      private final String to;
54      private final StringBuffer headerFields;
55      private boolean hasHeaderDate;
56      private StringBuffer cc;
57  
58      /**
59       * Creates a new SimpleSMTPHeader instance initialized with the given from, to, and subject header field values.
60       *
61       * @param from    The value of the <code>From:</code> header field. This should be the sender's email address. Must not be null.
62       * @param to      The value of the <code>To:</code> header field. This should be the recipient's email address. May be null
63       * @param subject The value of the <code>Subject:</code> header field. This should be the subject of the message. May be null
64       */
65      public SimpleSMTPHeader(final String from, final String to, final String subject) {
66          if (from == null) {
67              throw new IllegalArgumentException("From cannot be null");
68          }
69          this.to = to;
70          this.from = from;
71          this.subject = subject;
72          this.headerFields = new StringBuffer();
73          this.cc = null;
74      }
75  
76      /**
77       * Add an email address to the CC (carbon copy or courtesy copy) list.
78       *
79       * @param address The email address to add to the CC list.
80       */
81      public void addCC(final String address) {
82          if (cc == null) {
83              cc = new StringBuffer();
84          } else {
85              cc.append(", ");
86          }
87  
88          cc.append(address);
89      }
90  
91      /**
92       * Adds an arbitrary header field with the given value to the article header. These headers will be written before the
93       * {@code From}, {@code To}, {@code Subject}, and {@code Cc} fields when the SimpleSMTPHeader is converted to a string.
94       * An example use would be:
95       *
96       * <pre>
97       * header.addHeaderField("Organization", "Foobar, Inc.");
98       * </pre>
99       *
100      * @param headerField The header field to add, not including the colon.
101      * @param value       The value of the added header field.
102      */
103     public void addHeaderField(final String headerField, final String value) {
104         if (!hasHeaderDate && "Date".equals(headerField)) {
105             hasHeaderDate = true;
106         }
107         headerFields.append(headerField);
108         headerFields.append(": ");
109         headerFields.append(value);
110         headerFields.append('\n');
111     }
112 
113     /**
114      * Converts the SimpleSMTPHeader to a properly formatted header in the form of a String, including the blank line used to separate the header from the
115      * article body. The header fields CC and Subject are only included when they are non-null.
116      *
117      * @return The message header in the form of a String.
118      */
119     @Override
120     public String toString() {
121         final StringBuilder header = new StringBuilder();
122 
123         final String pattern = "EEE, dd MMM yyyy HH:mm:ss Z"; // Fri, 21 Nov 1997 09:55:06 -0600
124         final SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
125 
126         if (!hasHeaderDate) {
127             addHeaderField("Date", format.format(new Date()));
128         }
129         if (headerFields.length() > 0) {
130             header.append(headerFields.toString());
131         }
132 
133         header.append("From: ").append(from).append("\n");
134 
135         if (to != null) {
136             header.append("To: ").append(to).append("\n");
137         }
138 
139         if (cc != null) {
140             header.append("Cc: ").append(cc.toString()).append("\n");
141         }
142 
143         if (subject != null) {
144             header.append("Subject: ").append(subject).append("\n");
145         }
146 
147         header.append('\n'); // end of headers; body follows
148 
149         return header.toString();
150     }
151 }