1 package org.apache.commons.net.smtp;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 /****
58 * This class is used to construct a bare minimum
59 * acceptable header for an email message. To construct more
60 * complicated headers you should refer to RFC 822. When the
61 * Java Mail API is finalized, you will be
62 * able to use it to compose fully compliant Internet text messages.
63 * <p>
64 * The main purpose of the class is to faciliatate the mail sending
65 * process, by relieving the programmer from having to explicitly format
66 * a simple message header. For example:
67 * <pre>
68 * writer = client.sendMessageData();
69 * if(writer == null) // failure
70 * return false;
71 * header =
72 * new SimpleSMTPHeader("foobar@foo.com", "foo@bar.com" "Just testing");
73 * header.addCC("bar@foo.com");
74 * header.addHeaderField("Organization", "Foobar, Inc.");
75 * writer.write(header.toString());
76 * writer.write("This is just a test");
77 * writer.close();
78 * if(!client.completePendingCommand()) // failure
79 * return false;
80 * </pre>
81 * <p>
82 * <p>
83 * @author Daniel F. Savarese
84 * @see SMTPClient
85 ***/
86
87 public class SimpleSMTPHeader
88 {
89 private String __subject, __from, __to;
90 private StringBuffer __headerFields, __cc;
91
92 /****
93 * Creates a new SimpleSMTPHeader instance initialized with the given
94 * from, to, and subject header field values.
95 * <p>
96 * @param from The value of the <code>From:</code> header field. This
97 * should be the sender's email address.
98 * @param from The value of the <code>To:</code> header field. This
99 * should be the recipient's email address.
100 * @param subject The value of the <code>Subject:</code> header field.
101 * This should be the subject of the message.
102 ***/
103 public SimpleSMTPHeader(String from, String to, String subject)
104 {
105 __to = to;
106 __from = from;
107 __subject = subject;
108 __headerFields = new StringBuffer();
109 __cc = null;
110 }
111
112 /****
113 * Adds an arbitrary header field with the given value to the article
114 * header. These headers will be written before the From, To, Subject, and
115 * Cc fields when the SimpleSMTPHeader is convertered to a string.
116 * An example use would be:
117 * <pre>
118 * header.addHeaderField("Organization", "Foobar, Inc.");
119 * </pre>
120 * <p>
121 * @param headerField The header field to add, not including the colon.
122 * @param value The value of the added header field.
123 ***/
124 public void addHeaderField(String headerField, String value)
125 {
126 __headerFields.append(headerField);
127 __headerFields.append(": ");
128 __headerFields.append(value);
129 __headerFields.append('\n');
130 }
131
132
133 /****
134 * Add an email address to the CC (carbon copy or courtesy copy) list.
135 * <p>
136 * @param address The email address to add to the CC list.
137 ***/
138 public void addCC(String address)
139 {
140 if (__cc == null)
141 __cc = new StringBuffer();
142 else
143 __cc.append(", ");
144
145 __cc.append(address);
146 }
147
148
149 /****
150 * Converts the SimpleSMTPHeader to a properly formatted header in
151 * the form of a String, including the blank line used to separate
152 * the header from the article body.
153 * <p>
154 * @return The message header in the form of a String.
155 ***/
156 public String toString()
157 {
158 StringBuffer header = new StringBuffer();
159
160 if (__headerFields.length() > 0)
161 header.append(__headerFields.toString());
162
163 header.append("From: ");
164 header.append(__from);
165 header.append("\nTo: ");
166 header.append(__to);
167
168 if (__cc != null)
169 {
170 header.append("\nCc: ");
171 header.append(__cc);
172 }
173
174 header.append("\nSubject: ");
175 header.append(__subject);
176 header.append('\n');
177
178 header.append('\n');
179
180 return header.toString();
181 }
182 }
183
184
185
This page was automatically generated by Maven