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