Package: SMTPMessage
SMTPMessage
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SMTPMessage(MimeMessage) |
|
|
|
|
|
||||||||||||||||||||
SMTPMessage(Session) |
|
|
|
|
|
||||||||||||||||||||
SMTPMessage(Session, InputStream) |
|
|
|
|
|
||||||||||||||||||||
getAllow8bitMIME() |
|
|
|
|
|
||||||||||||||||||||
getDSNNotify() |
|
|
|
|
|
||||||||||||||||||||
getDSNRet() |
|
|
|
|
|
||||||||||||||||||||
getEnvelopeFrom() |
|
|
|
|
|
||||||||||||||||||||
getMailExtension() |
|
|
|
|
|
||||||||||||||||||||
getNotifyOptions() |
|
|
|
|
|
||||||||||||||||||||
getReturnOption() |
|
|
|
|
|
||||||||||||||||||||
getSendPartial() |
|
|
|
|
|
||||||||||||||||||||
getSubmitter() |
|
|
|
|
|
||||||||||||||||||||
setAllow8bitMIME(boolean) |
|
|
|
|
|
||||||||||||||||||||
setEnvelopeFrom(String) |
|
|
|
|
|
||||||||||||||||||||
setMailExtension(String) |
|
|
|
|
|
||||||||||||||||||||
setNotifyOptions(int) |
|
|
|
|
|
||||||||||||||||||||
setReturnOption(int) |
|
|
|
|
|
||||||||||||||||||||
setSendPartial(boolean) |
|
|
|
|
|
||||||||||||||||||||
setSubmitter(String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.smtp;
18:
19: import java.io.*;
20: import jakarta.mail.*;
21: import jakarta.mail.internet.*;
22:
23: /**
24: * This class is a specialization of the MimeMessage class that allows
25: * you to specify various SMTP options and parameters that will be
26: * used when this message is sent over SMTP. Simply use this class
27: * instead of MimeMessage and set SMTP options using the methods on
28: * this class. <p>
29: *
30: * See the <a href="package-summary.html">org.eclipse.angus.mail.smtp</a> package
31: * documentation for further information on the SMTP protocol provider. <p>
32: *
33: * @author Bill Shannon
34: * @see        jakarta.mail.internet.MimeMessage
35: */
36:
37: public class SMTPMessage extends MimeMessage {
38:
39: /** Never notify of delivery status */
40: public static final int NOTIFY_NEVER = -1;
41: /** Notify of delivery success */
42: public static final int NOTIFY_SUCCESS = 1;
43: /** Notify of delivery failure */
44: public static final int NOTIFY_FAILURE = 2;
45: /** Notify of delivery delay */
46: public static final int NOTIFY_DELAY = 4;
47:
48: /** Return full message with delivery status notification */
49: public static final int RETURN_FULL = 1;
50: /** Return only message headers with delivery status notification */
51: public static final int RETURN_HDRS = 2;
52:
53: private static final String[] returnOptionString = { null, "FULL", "HDRS" };
54:
55: private String envelopeFrom; // the string to use in the MAIL FROM: command
56: private int notifyOptions = 0;
57: private int returnOption = 0;
58: private boolean sendPartial = false;
59: private boolean allow8bitMIME = false;
60: private String submitter = null;        // RFC 2554 AUTH=submitter
61: private String extension = null;        // extensions to use with MAIL command
62:
63: /**
64: * Default constructor. An empty message object is created.
65: * The <code>headers</code> field is set to an empty InternetHeaders
66: * object. The <code>flags</code> field is set to an empty Flags
67: * object. The <code>modified</code> flag is set to true.
68: *
69: * @param        session        the Session
70: */
71: public SMTPMessage(Session session) {
72:         super(session);
73: }
74:
75: /**
76: * Constructs an SMTPMessage by reading and parsing the data from the
77: * specified MIME InputStream. The InputStream will be left positioned
78: * at the end of the data for the message. Note that the input stream
79: * parse is done within this constructor itself.
80: *
81: * @param session        Session object for this message
82: * @param is        the message input stream
83: * @exception        MessagingException for failures
84: */
85: public SMTPMessage(Session session, InputStream is)
86:                         throws MessagingException {
87:         super(session, is);
88: }
89:
90: /**
91: * Constructs a new SMTPMessage with content initialized from the
92: * <code>source</code> MimeMessage. The new message is independent
93: * of the original. <p>
94: *
95: * Note: The current implementation is rather inefficient, copying
96: * the data more times than strictly necessary.
97: *
98: * @param        source        the message to copy content from
99: * @exception        MessagingException for failures
100: */
101: public SMTPMessage(MimeMessage source) throws MessagingException {
102:         super(source);
103: }
104:
105: /**
106: * Set the From address to appear in the SMTP envelope. Note that this
107: * is different than the From address that appears in the message itself.
108: * The envelope From address is typically used when reporting errors.
109: * See <A HREF="http://www.ietf.org/rfc/rfc821.txt">RFC 821</A> for
110: * details. <p>
111: *
112: * If set, overrides the <code>mail.smtp.from</code> property.
113: *
114: * @param        from        the envelope From address
115: */
116: public void setEnvelopeFrom(String from) {
117:         envelopeFrom = from;
118: }
119:
120: /**
121: * Return the envelope From address.
122: *
123: * @return        the envelope From address, or null if not set
124: */
125: public String getEnvelopeFrom() {
126:         return envelopeFrom;
127: }
128:
129: /**
130: * Set notification options to be used if the server supports
131: * Delivery Status Notification
132: * (<A HREF="http://www.ietf.org/rfc/rfc1891.txt">RFC 1891</A>).
133: * Either <code>NOTIFY_NEVER</code> or some combination of
134: * <code>NOTIFY_SUCCESS</code>, <code>NOTIFY_FAILURE</code>, and
135: * <code>NOTIFY_DELAY</code>. <p>
136: *
137: * If set, overrides the <code>mail.smtp.dsn.notify</code> property.
138: *
139: * @param        options        notification options
140: */
141: public void setNotifyOptions(int options) {
142:•        if (options < -1 || options >= 8)
143:          throw new IllegalArgumentException("Bad return option");
144:         notifyOptions = options;
145: }
146:
147: /**
148: * Get notification options. Returns zero if no options set.
149: *
150: * @return        notification options
151: */
152: public int getNotifyOptions() {
153:         return notifyOptions;
154: }
155:
156: /**
157: * Return notification options as an RFC 1891 string.
158: * Returns null if no options set.
159: */
160: String getDSNNotify() {
161:•        if (notifyOptions == 0)
162:          return null;
163:•        if (notifyOptions == NOTIFY_NEVER)
164:          return "NEVER";
165:         StringBuilder sb = new StringBuilder();
166:•        if ((notifyOptions & NOTIFY_SUCCESS) != 0)
167:          sb.append("SUCCESS");
168:•        if ((notifyOptions & NOTIFY_FAILURE) != 0) {
169:•         if (sb.length() != 0)
170:                 sb.append(',');
171:          sb.append("FAILURE");
172:         }
173:•        if ((notifyOptions & NOTIFY_DELAY) != 0) {
174:•         if (sb.length() != 0)
175:                 sb.append(',');
176:          sb.append("DELAY");
177:         }
178:         return sb.toString();
179: }
180:
181: /**
182: * Set return option to be used if server supports
183: * Delivery Status Notification
184: * (<A HREF="http://www.ietf.org/rfc/rfc1891.txt">RFC 1891</A>).
185: * Either <code>RETURN_FULL</code> or <code>RETURN_HDRS</code>. <p>
186: *
187: * If set, overrides the <code>mail.smtp.dsn.ret</code> property.
188: *
189: * @param        option        return option
190: */
191: public void setReturnOption(int option) {
192:•        if (option < 0 || option > RETURN_HDRS)
193:          throw new IllegalArgumentException("Bad return option");
194:         returnOption = option;
195: }
196:
197: /**
198: * Return return option. Returns zero if no option set.
199: *
200: * @return        return option
201: */
202: public int getReturnOption() {
203:         return returnOption;
204: }
205:
206: /**
207: * Return return option as an RFC 1891 string.
208: * Returns null if no option set.
209: */
210: String getDSNRet() {
211:         return returnOptionString[returnOption];
212: }
213:
214: /**
215: * If set to true, and the server supports the 8BITMIME extension, text
216: * parts of this message that use the "quoted-printable" or "base64"
217: * encodings are converted to use "8bit" encoding if they follow the
218: * RFC 2045 rules for 8bit text. <p>
219: *
220: * If true, overrides the <code>mail.smtp.allow8bitmime</code> property.
221: *
222: * @param        allow        allow 8-bit flag
223: */
224: public void setAllow8bitMIME(boolean allow) {
225:         allow8bitMIME = allow;
226: }
227:
228: /**
229: * Is use of the 8BITMIME extension is allowed?
230: *
231: * @return        allow 8-bit flag
232: */
233: public boolean getAllow8bitMIME() {
234:         return allow8bitMIME;
235: }
236:
237: /**
238: * If set to true, and this message has some valid and some invalid
239: * addresses, send the message anyway, reporting the partial failure with
240: * a SendFailedException. If set to false (the default), the message is
241: * not sent to any of the recipients if there is an invalid recipient
242: * address. <p>
243: *
244: * If true, overrides the <code>mail.smtp.sendpartial</code> property.
245: *
246: * @param partial        send partial flag
247: */
248: public void setSendPartial(boolean partial) {
249:         sendPartial = partial;
250: }
251:
252: /**
253: * Send message if some addresses are invalid?
254: *
255: * @return        send partial flag
256: */
257: public boolean getSendPartial() {
258:         return sendPartial;
259: }
260:
261: /**
262: * Gets the submitter to be used for the RFC 2554 AUTH= value
263: * in the MAIL FROM command.
264: *
265: * @return        the name of the submitter.
266: */
267: public String getSubmitter() {
268:         return submitter;
269: }
270:
271: /**
272: * Sets the submitter to be used for the RFC 2554 AUTH= value
273: * in the MAIL FROM command. Normally only used by a server
274: * that's relaying a message. Clients will typically not
275: * set a submitter. See
276: * <A HREF="http://www.ietf.org/rfc/rfc2554.txt">RFC 2554</A>
277: * for details.
278: *
279: * @param        submitter        the name of the submitter
280: */
281: public void setSubmitter(String submitter) {
282:         this.submitter = submitter;
283: }
284:
285: /**
286: * Gets the extension string to use with the MAIL command.
287: *
288: * @return        the extension string
289: *
290: * @since        JavaMail 1.3.2
291: */
292: public String getMailExtension() {
293:         return extension;
294: }
295:
296: /**
297: * Set the extension string to use with the MAIL command.
298: * The extension string can be used to specify standard SMTP
299: * service extensions as well as vendor-specific extensions.
300: * Typically the application should use the
301: * {@link SMTPTransport SMTPTransport}
302: * method {@link SMTPTransport#supportsExtension
303: * supportsExtension}
304: * to verify that the server supports the desired service extension.
305: * See <A HREF="http://www.ietf.org/rfc/rfc1869.txt">RFC 1869</A>
306: * and other RFCs that define specific extensions. <p>
307: *
308: * For example:
309: *
310: * <blockquote><pre>
311: * if (smtpTransport.supportsExtension("DELIVERBY"))
312: * smtpMsg.setMailExtension("BY=60;R");
313: * </pre></blockquote>
314: *
315: * @param        extension        the extension string
316: * @since        JavaMail 1.3.2
317: */
318: public void setMailExtension(String extension) {
319:         this.extension = extension;
320: }
321: }