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