SimpleSMTPHeader.java

  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.commons.net.smtp;

  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.Locale;

  21. /**
  22.  * 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.
  23.  * When the Java Mail API is finalized, you will be able to use it to compose fully compliant Internet text messages.
  24.  * <p>
  25.  * 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
  26.  * header. For example:
  27.  * </p>
  28.  *
  29.  * <pre>
  30.  * writer = client.sendMessageData();
  31.  * if (writer == null) // failure
  32.  *   return false;
  33.  * header =
  34.  *    new SimpleSMTPHeader("foobar@foo.com", "foo@bar.com" "Just testing");
  35.  * header.addCC("bar@foo.com");
  36.  * header.addHeaderField("Organization", "Foobar, Inc.");
  37.  * writer.write(header.toString());
  38.  * writer.write("This is just a test");
  39.  * writer.close();
  40.  * if (!client.completePendingCommand()) // failure
  41.  *   return false;
  42.  * </pre>
  43.  *
  44.  * @see SMTPClient
  45.  */

  46. public class SimpleSMTPHeader {
  47.     private final String subject;
  48.     private final String from;
  49.     private final String to;
  50.     private final StringBuffer headerFields;
  51.     private boolean hasHeaderDate;
  52.     private StringBuffer cc;

  53.     /**
  54.      * Creates a new SimpleSMTPHeader instance initialized with the given from, to, and subject header field values.
  55.      *
  56.      * @param from    The value of the <code>From:</code> header field. This should be the sender's email address. Must not be null.
  57.      * @param to      The value of the <code>To:</code> header field. This should be the recipient's email address. May be null
  58.      * @param subject The value of the <code>Subject:</code> header field. This should be the subject of the message. May be null
  59.      */
  60.     public SimpleSMTPHeader(final String from, final String to, final String subject) {
  61.         if (from == null) {
  62.             throw new IllegalArgumentException("From cannot be null");
  63.         }
  64.         this.to = to;
  65.         this.from = from;
  66.         this.subject = subject;
  67.         this.headerFields = new StringBuffer();
  68.         this.cc = null;
  69.     }

  70.     /**
  71.      * Add an email address to the CC (carbon copy or courtesy copy) list.
  72.      *
  73.      * @param address The email address to add to the CC list.
  74.      */
  75.     public void addCC(final String address) {
  76.         if (cc == null) {
  77.             cc = new StringBuffer();
  78.         } else {
  79.             cc.append(", ");
  80.         }

  81.         cc.append(address);
  82.     }

  83.     /**
  84.      * Adds an arbitrary header field with the given value to the article header. These headers will be written before the
  85.      * {@code From}, {@code To}, {@code Subject}, and {@code Cc} fields when the SimpleSMTPHeader is converted to a string.
  86.      * An example use would be:
  87.      *
  88.      * <pre>
  89.      * header.addHeaderField("Organization", "Foobar, Inc.");
  90.      * </pre>
  91.      *
  92.      * @param headerField The header field to add, not including the colon.
  93.      * @param value       The value of the added header field.
  94.      */
  95.     public void addHeaderField(final String headerField, final String value) {
  96.         if (!hasHeaderDate && "Date".equals(headerField)) {
  97.             hasHeaderDate = true;
  98.         }
  99.         headerFields.append(headerField);
  100.         headerFields.append(": ");
  101.         headerFields.append(value);
  102.         headerFields.append('\n');
  103.     }

  104.     /**
  105.      * 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
  106.      * article body. The header fields CC and Subject are only included when they are non-null.
  107.      *
  108.      * @return The message header in the form of a String.
  109.      */
  110.     @Override
  111.     public String toString() {
  112.         final StringBuilder header = new StringBuilder();

  113.         final String pattern = "EEE, dd MMM yyyy HH:mm:ss Z"; // Fri, 21 Nov 1997 09:55:06 -0600
  114.         final SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);

  115.         if (!hasHeaderDate) {
  116.             addHeaderField("Date", format.format(new Date()));
  117.         }
  118.         if (headerFields.length() > 0) {
  119.             header.append(headerFields.toString());
  120.         }

  121.         header.append("From: ").append(from).append("\n");

  122.         if (to != null) {
  123.             header.append("To: ").append(to).append("\n");
  124.         }

  125.         if (cc != null) {
  126.             header.append("Cc: ").append(cc.toString()).append("\n");
  127.         }

  128.         if (subject != null) {
  129.             header.append("Subject: ").append(subject).append("\n");
  130.         }

  131.         header.append('\n'); // end of headers; body follows

  132.         return header.toString();
  133.     }
  134. }