KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > smtp > SMTPMessage


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)SMTPMessage.java 1.7 05/08/29
24  *
25  * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.smtp;
29
30 import java.io.*;
31 import javax.mail.*;
32 import javax.mail.internet.*;
33
34 /**
35  * This class is a specialization of the MimeMessage class that allows
36  * you to specify various SMTP options and parameters that will be
37  * used when this message is sent over SMTP. Simply use this class
38  * instead of MimeMessage and set SMTP options using the methods on
39  * this class. <p>
40  *
41  * See the <a HREF="package-summary.html">com.sun.mail.smtp</a> package
42  * documentation for further information on the SMTP protocol provider. <p>
43  *
44  * @author Bill Shannon
45  * @see javax.mail.internet.MimeMessage
46  */

47
48 public class SMTPMessage extends MimeMessage {
49
50     /** Never notify of delivery status */
51     public static final int NOTIFY_NEVER = -1;
52     /** Notify of delivery success */
53     public static final int NOTIFY_SUCCESS = 1;
54     /** Notify of delivery failure */
55     public static final int NOTIFY_FAILURE = 2;
56     /** Notify of delivery delay */
57     public static final int NOTIFY_DELAY = 4;
58
59     /** Return full message with delivery status notification */
60     public static final int RETURN_FULL = 1;
61     /** Return only message headers with delivery status notification */
62     public static final int RETURN_HDRS = 2;
63
64     private static final String JavaDoc[] returnOptionString = { null, "FULL", "HDRS" };
65
66     private String JavaDoc envelopeFrom; // the string to use in the MAIL FROM: command
67
private int notifyOptions = 0;
68     private int returnOption = 0;
69     private boolean sendPartial = false;
70     private boolean allow8bitMIME = false;
71     private String JavaDoc submitter = null; // RFC 2554 AUTH=submitter
72
private String JavaDoc extension = null; // extensions to use with MAIL command
73

74     /**
75      * Default constructor. An empty message object is created.
76      * The <code>headers</code> field is set to an empty InternetHeaders
77      * object. The <code>flags</code> field is set to an empty Flags
78      * object. The <code>modified</code> flag is set to true.
79      */

80     public SMTPMessage(Session session) {
81     super(session);
82     }
83
84     /**
85      * Constructs an SMTPMessage by reading and parsing the data from the
86      * specified MIME InputStream. The InputStream will be left positioned
87      * at the end of the data for the message. Note that the input stream
88      * parse is done within this constructor itself.
89      *
90      * @param session Session object for this message
91      * @param is the message input stream
92      * @exception MessagingException
93      */

94     public SMTPMessage(Session session, InputStream is)
95             throws MessagingException {
96     super(session, is);
97     }
98
99     /**
100      * Constructs a new SMTPMessage with content initialized from the
101      * <code>source</code> MimeMessage. The new message is independent
102      * of the original. <p>
103      *
104      * Note: The current implementation is rather inefficient, copying
105      * the data more times than strictly necessary.
106      *
107      * @param source the message to copy content from
108      * @exception MessagingException
109      */

110     public SMTPMessage(MimeMessage source) throws MessagingException {
111     super(source);
112     }
113
114     /**
115      * Set the From address to appear in the SMTP envelope. Note that this
116      * is different than the From address that appears in the message itself.
117      * The envelope From address is typically used when reporting errors.
118      * See <A HREF="http://www.ietf.org/rfc/rfc821.txt">RFC 821</A> for
119      * details. <p>
120      *
121      * If set, overrides the <code>mail.smtp.from</code> property.
122      *
123      * @param from the envelope From address
124      */

125     public void setEnvelopeFrom(String JavaDoc from) {
126     envelopeFrom = from;
127     }
128
129     /**
130      * Return the envelope From address.
131      *
132      * @return the envelope From address, or null if not set
133      */

134     public String JavaDoc getEnvelopeFrom() {
135     return envelopeFrom;
136     }
137
138     /**
139      * Set notification options to be used if the server supports
140      * Delivery Status Notification
141      * (<A HREF="http://www.ietf.org/rfc/rfc1891.txt">RFC 1891</A>).
142      * Either <code>NOTIFY_NEVER</code> or some combination of
143      * <code>NOTIFY_SUCCESS</code>, <code>NOTIFY_FAILURE</code>, and
144      * <code>NOTIFY_DELAY</code>. <p>
145      *
146      * If set, overrides the <code>mail.smtp.dsn.notify</code> property.
147      *
148      * @param options notification options
149      */

150     public void setNotifyOptions(int options) {
151     if (options < -1 || options >= 8)
152         throw new IllegalArgumentException JavaDoc("Bad return option");
153     notifyOptions = options;
154     }
155
156     /**
157      * Get notification options. Returns zero if no options set.
158      *
159      * @return notification options
160      */

161     public int getNotifyOptions() {
162     return notifyOptions;
163     }
164
165     /**
166      * Return notification options as an RFC 1891 string.
167      * Returns null if no options set.
168      */

169     String JavaDoc getDSNNotify() {
170     if (notifyOptions == 0)
171         return null;
172     if (notifyOptions == NOTIFY_NEVER)
173         return "NEVER";
174     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
175     if ((notifyOptions & NOTIFY_SUCCESS) != 0)
176         sb.append("SUCCESS");
177     if ((notifyOptions & NOTIFY_FAILURE) != 0) {
178         if (sb.length() != 0)
179         sb.append(',');
180         sb.append("FAILURE");
181     }
182     if ((notifyOptions & NOTIFY_DELAY) != 0) {
183         if (sb.length() != 0)
184         sb.append(',');
185         sb.append("DELAY");
186     }
187     return sb.toString();
188     }
189
190     /**
191      * Set return option to be used if server supports
192      * Delivery Status Notification
193      * (<A HREF="http://www.ietf.org/rfc/rfc1891.txt">RFC 1891</A>).
194      * Either <code>RETURN_FULL</code> or <code>RETURN_HDRS</code>. <p>
195      *
196      * If set, overrides the <code>mail.smtp.dsn.ret</code> property.
197      *
198      * @param option return option
199      */

200     public void setReturnOption(int option) {
201     if (option < 0 || option > RETURN_HDRS)
202         throw new IllegalArgumentException JavaDoc("Bad return option");
203     returnOption = option;
204     }
205
206     /**
207      * Return return option. Returns zero if no option set.
208      *
209      * @return return option
210      */

211     public int getReturnOption() {
212     return returnOption;
213     }
214
215     /**
216      * Return return option as an RFC 1891 string.
217      * Returns null if no option set.
218      */

219     String JavaDoc getDSNRet() {
220     return returnOptionString[returnOption];
221     }
222
223     /**
224      * If set to true, and the server supports the 8BITMIME extension, text
225      * parts of this message that use the "quoted-printable" or "base64"
226      * encodings are converted to use "8bit" encoding if they follow the
227      * RFC 2045 rules for 8bit text. <p>
228      *
229      * If true, overrides the <code>mail.smtp.allow8bitmime</code> property.
230      *
231      * @param allow allow 8-bit flag
232      */

233     public void setAllow8bitMIME(boolean allow) {
234     allow8bitMIME = allow;
235     }
236
237     /**
238      * Is use of the 8BITMIME extension is allowed?
239      *
240      * @return allow 8-bit flag
241      */

242     public boolean getAllow8bitMIME() {
243     return allow8bitMIME;
244     }
245
246     /**
247      * If set to true, and this message has some valid and some invalid
248      * addresses, send the message anyway, reporting the partial failure with
249      * a SendFailedException. If set to false (the default), the message is
250      * not sent to any of the recipients if there is an invalid recipient
251      * address. <p>
252      *
253      * If true, overrides the <code>mail.smtp.sendpartial</code> property.
254      *
255      * @param partial send partial flag
256      */

257     public void setSendPartial(boolean partial) {
258     sendPartial = partial;
259     }
260
261     /**
262      * Send message if some addresses are invalid?
263      *
264      * @return send partial flag
265      */

266     public boolean getSendPartial() {
267     return sendPartial;
268     }
269
270     /**
271      * Gets the submitter to be used for the RFC 2554 AUTH= value
272      * in the MAIL FROM command.
273      *
274      * @return the name of the submitter.
275      */

276     public String JavaDoc getSubmitter() {
277     return submitter;
278     }
279
280     /**
281      * Sets the submitter to be used for the RFC 2554 AUTH= value
282      * in the MAIL FROM command. Normally only used by a server
283      * that's relaying a message. Clients will typically not
284      * set a submitter. See
285      * <A HREF="http://www.ietf.org/rfc/rfc2554.txt">RFC 2554</A>
286      * for details.
287      *
288      * @param submitter the name of the submitter
289      */

290     public void setSubmitter(String JavaDoc submitter) {
291     this.submitter = submitter;
292     }
293
294     /**
295      * Gets the extension string to use with the MAIL command.
296      *
297      * @return the extension string
298      *
299      * @since JavaMail 1.3.2
300      */

301     public String JavaDoc getMailExtension() {
302     return extension;
303     }
304
305     /**
306      * Set the extension string to use with the MAIL command.
307      * The extension string can be used to specify standard SMTP
308      * service extensions as well as vendor-specific extensions.
309      * Typically the application should use the
310      * {@link com.sun.mail.smtp.SMTPTransport SMTPTransport}
311      * method {@link com.sun.mail.smtp.SMTPTransport#supportsExtension
312      * supportsExtension}
313      * to verify that the server supports the desired service extension.
314      * See <A HREF="http://www.ietf.org/rfc/rfc1869.txt">RFC 1869</A>
315      * and other RFCs that define specific extensions. <p>
316      *
317      * For example: <p>
318      *
319      * <blockquote><pre>
320      * if (smtpTransport.supportsExtension("DELIVERBY"))
321      * smtpMsg.setMailExtension("BY=60;R");
322      * </pre></blockquote>
323      *
324      * @since JavaMail 1.3.2
325      */

326     public void setMailExtension(String JavaDoc extension) {
327     this.extension = extension;
328     }
329 }
330
Popular Tags