    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017
    018    package org.apache.commons.net.smtp;
    019
    020    /***
    021     * This class is used to construct a bare minimum
    022     * acceptable header for an email message.  To construct more
    023     * complicated headers you should refer to RFC 822.  When the
    024     * Java Mail API is finalized, you will be
    025     * able to use it to compose fully compliant Internet text messages.
    026     * <p>
    027     * The main purpose of the class is to faciliatate the mail sending
    028     * process, by relieving the programmer from having to explicitly format
    029     * a simple message header.  For example:
    030     * <pre>
    031     * writer = client.sendMessageData();
    032     * if(writer == null) // failure
    033     *   return false;
    034     * header =
    035     *    new SimpleSMTPHeader("foobar@foo.com", "foo@bar.com" "Just testing");
    036     * header.addCC("bar@foo.com");
    037     * header.addHeaderField("Organization", "Foobar, Inc.");
    038     * writer.write(header.toString());
    039     * writer.write("This is just a test");
    040     * writer.close();
    041     * if(!client.completePendingCommand()) // failure
    042     *   return false;
    043     * </pre>
    044     * <p>
    045     * <p>
    046     * @see SMTPClient
    047     ***/
    048
    049    public class SimpleSMTPHeader
    050    {
    051        private String __subject, __from, __to;
    052        private StringBuffer __headerFields, __cc;
    053
    054        /***
    055         * Creates a new SimpleSMTPHeader instance initialized with the given
    056         * from, to, and subject header field values.
    057         * <p>
    058         * @param from  The value of the <code>From:</code> header field.  This
    059         *              should be the sender's email address.
    060         * @param to    The value of the <code>To:</code> header field.  This
    061         *              should be the recipient's email address.
    062         * @param subject  The value of the <code>Subject:</code> header field.
    063         *              This should be the subject of the message.
    064         ***/
    065        public SimpleSMTPHeader(String from, String to, String subject)
    066        {
    067            __to = to;
    068            __from = from;
    069            __subject = subject;
    070            __headerFields = new StringBuffer();
    071            __cc = null;
    072        }
    073
    074        /***
    075         * Adds an arbitrary header field with the given value to the article
    076         * header.  These headers will be written before the From, To, Subject, and
    077         * Cc fields when the SimpleSMTPHeader is convertered to a string.
    078         * An example use would be:
    079         * <pre>
    080         * header.addHeaderField("Organization", "Foobar, Inc.");
    081         * </pre>
    082         * <p>
    083         * @param headerField  The header field to add, not including the colon.
    084         * @param value  The value of the added header field.
    085         ***/
    086        public void addHeaderField(String headerField, String value)
    087        {
    088            __headerFields.append(headerField);
    089            __headerFields.append(": ");
    090            __headerFields.append(value);
    091            __headerFields.append('\n');
    092        }
    093
    094
    095        /***
    096         * Add an email address to the CC (carbon copy or courtesy copy) list.
    097         * <p>
    098         * @param address The email address to add to the CC list.
    099         ***/
    100        public void addCC(String address)
    101        {
    102            if (__cc == null)
    103                __cc = new StringBuffer();
    104            else
    105                __cc.append(", ");
    106
    107            __cc.append(address);
    108        }
    109
    110
    111        /***
    112         * Converts the SimpleSMTPHeader to a properly formatted header in
    113         * the form of a String, including the blank line used to separate
    114         * the header from the article body.  The header fields CC and Subject
    115         * are only included when they are non-null.
    116         * <p>
    117         * @return The message header in the form of a String.
    118         ***/
    119        @Override
    120        public String toString()
    121        {
    122            StringBuilder header = new StringBuilder();
    123
    124            if (__headerFields.length() > 0)
    125                header.append(__headerFields.toString());
    126
    127            header.append("From: ");
    128            header.append(__from);
    129            header.append("\nTo: ");
    130            header.append(__to);
    131
    132            if (__cc != null)
    133            {
    134                header.append("\nCc: ");
    135                header.append(__cc.toString());
    136            }
    137
    138            if (__subject != null)
    139            {
    140                header.append("\nSubject: ");
    141                header.append(__subject);
    142            }
    143
    144            header.append('\n');
    145            header.append('\n');
    146
    147            return header.toString();
    148        }
    149    }
    150
    151
    152