KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > smtp > SimpleSMTPHeader


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.net.smtp;
17
18 /***
19  * This class is used to construct a bare minimum
20  * acceptable header for an email message. To construct more
21  * complicated headers you should refer to RFC 822. When the
22  * Java Mail API is finalized, you will be
23  * able to use it to compose fully compliant Internet text messages.
24  * <p>
25  * The main purpose of the class is to faciliatate the mail sending
26  * process, by relieving the programmer from having to explicitly format
27  * a simple message header. For example:
28  * <pre>
29  * writer = client.sendMessageData();
30  * if(writer == null) // failure
31  * return false;
32  * header =
33  * new SimpleSMTPHeader("foobar@foo.com", "foo@bar.com" "Just testing");
34  * header.addCC("bar@foo.com");
35  * header.addHeaderField("Organization", "Foobar, Inc.");
36  * writer.write(header.toString());
37  * writer.write("This is just a test");
38  * writer.close();
39  * if(!client.completePendingCommand()) // failure
40  * return false;
41  * </pre>
42  * <p>
43  * <p>
44  * @author Daniel F. Savarese
45  * @see SMTPClient
46  ***/

47
48 public class SimpleSMTPHeader
49 {
50     private String JavaDoc __subject, __from, __to;
51     private StringBuffer JavaDoc __headerFields, __cc;
52
53     /***
54      * Creates a new SimpleSMTPHeader instance initialized with the given
55      * from, to, and subject header field values.
56      * <p>
57      * @param from The value of the <code>From:</code> header field. This
58      * should be the sender's email address.
59      * @param to The value of the <code>To:</code> header field. This
60      * should be the recipient's email address.
61      * @param subject The value of the <code>Subject:</code> header field.
62      * This should be the subject of the message.
63      ***/

64     public SimpleSMTPHeader(String JavaDoc from, String JavaDoc to, String JavaDoc subject)
65     {
66         __to = to;
67         __from = from;
68         __subject = subject;
69         __headerFields = new StringBuffer JavaDoc();
70         __cc = null;
71     }
72
73     /***
74      * Adds an arbitrary header field with the given value to the article
75      * header. These headers will be written before the From, To, Subject, and
76      * Cc fields when the SimpleSMTPHeader is convertered to a string.
77      * An example use would be:
78      * <pre>
79      * header.addHeaderField("Organization", "Foobar, Inc.");
80      * </pre>
81      * <p>
82      * @param headerField The header field to add, not including the colon.
83      * @param value The value of the added header field.
84      ***/

85     public void addHeaderField(String JavaDoc headerField, String JavaDoc value)
86     {
87         __headerFields.append(headerField);
88         __headerFields.append(": ");
89         __headerFields.append(value);
90         __headerFields.append('\n');
91     }
92
93
94     /***
95      * Add an email address to the CC (carbon copy or courtesy copy) list.
96      * <p>
97      * @param address The email address to add to the CC list.
98      ***/

99     public void addCC(String JavaDoc address)
100     {
101         if (__cc == null)
102             __cc = new StringBuffer JavaDoc();
103         else
104             __cc.append(", ");
105
106         __cc.append(address);
107     }
108
109
110     /***
111      * Converts the SimpleSMTPHeader to a properly formatted header in
112      * the form of a String, including the blank line used to separate
113      * the header from the article body. The header fields CC and Subject
114      * are only included when they are non-null.
115      * <p>
116      * @return The message header in the form of a String.
117      ***/

118     public String JavaDoc toString()
119     {
120         StringBuffer JavaDoc header = new StringBuffer JavaDoc();
121
122         if (__headerFields.length() > 0)
123             header.append(__headerFields.toString());
124
125         header.append("From: ");
126         header.append(__from);
127         header.append("\nTo: ");
128         header.append(__to);
129
130         if (__cc != null)
131         {
132             header.append("\nCc: ");
133             header.append(__cc.toString());
134         }
135
136         if (__subject != null)
137         {
138             header.append("\nSubject: ");
139             header.append(__subject);
140         }
141
142         header.append('\n');
143         header.append('\n');
144
145         return header.toString();
146     }
147 }
148
149
150
151
Popular Tags