⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mimemessage.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License").  You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code.  If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license."  If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above.  However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * @(#)MimeMessage.java	1.95 07/05/14 */package javax.mail.internet;import javax.mail.*;import javax.activation.*;import java.lang.*;import java.io.*;import java.util.*;import java.text.ParseException;import com.sun.mail.util.*;import javax.mail.util.SharedByteArrayInputStream;/** * This class represents a MIME style email message. It implements * the <code>Message</code> abstract class and the <code>MimePart</code> * interface. <p> * * Clients wanting to create new MIME style messages will instantiate * an empty MimeMessage object and then fill it with appropriate  * attributes and content. <p> *  * Service providers that implement MIME compliant backend stores may * want to subclass MimeMessage and override certain methods to provide * specific implementations. The simplest case is probably a provider * that generates a MIME style input stream and leaves the parsing of * the stream to this class. <p> * * MimeMessage uses the <code>InternetHeaders</code> class to parse and * store the top level RFC 822 headers of a message. <p> * * The <code>mail.mime.address.strict</code> session property controls * the parsing of address headers.  By default, strict parsing of address * headers is done.  If this property is set to <code>"false"</code>, * strict parsing is not done and many illegal addresses that sometimes * occur in real messages are allowed.  See the <code>InternetAddress</code> * class for details. <p> * * <hr><strong>A note on RFC 822 and MIME headers</strong><p> * * RFC 822 header fields <strong>must</strong> contain only  * US-ASCII characters. MIME allows non ASCII characters to be present * in certain portions of certain headers, by encoding those characters. * RFC 2047 specifies the rules for doing this. The MimeUtility * class provided in this package can be used to to achieve this. * Callers of the <code>setHeader</code>, <code>addHeader</code>, and * <code>addHeaderLine</code> methods are responsible for enforcing * the MIME requirements for the specified headers.  In addition, these * header fields must be folded (wrapped) before being sent if they * exceed the line length limitation for the transport (1000 bytes for * SMTP).  Received headers may have been folded.  The application is * responsible for folding and unfolding headers as appropriate. <p> * * @author John Mani * @author Bill Shannon * @author Max Spivak * @author Kanwar Oberoi * @see	javax.mail.internet.MimeUtility * @see	javax.mail.Part * @see	javax.mail.Message * @see	javax.mail.internet.MimePart * @see	javax.mail.internet.InternetAddress */public class MimeMessage extends Message implements MimePart {    /**     * The DataHandler object representing this Message's content.     */    protected DataHandler dh;    /**     * Byte array that holds the bytes of this Message's content.     */    protected byte[] content;    /**     * If the data for this message was supplied by an     * InputStream that implements the SharedInputStream interface,     * <code>contentStream</code> is another such stream representing     * the content of this message.  In this case, <code>content</code>     * will be null.     *     * @since	JavaMail 1.2     */    protected InputStream contentStream;    /**     * The InternetHeaders object that stores the header     * of this message.     */    protected InternetHeaders headers;    /**     * The Flags for this message.      */    protected Flags flags;    /**     * A flag indicating whether the message has been modified.     * If the message has not been modified, any data in the     * <code>content</code> array is assumed to be valid and is used     * directly in the <code>writeTo</code> method.  This flag is     * set to true when an empty message is created or when the     * <code>saveChanges</code> method is called.     *     * @since	JavaMail 1.2     */    protected boolean modified = false;    /**     * Does the <code>saveChanges</code> method need to be called on     * this message?  This flag is set to false by the public constructor     * and set to true by the <code>saveChanges</code> method.  The     * <code>writeTo</code> method checks this flag and calls the     * <code>saveChanges</code> method as necessary.  This avoids the     * common mistake of forgetting to call the <code>saveChanges</code>     * method on a newly constructed message.     *     * @since	JavaMail 1.2     */    protected boolean saved = false;    /**     * If our content is a Multipart of Message object, we save it     * the first time it's created by parsing a stream so that changes     * to the contained objects will not be lost.     *     * XXX - must have package access for MimeBodyPart.updateHeaders     */    Object cachedContent;    // Used to parse dates    private static MailDateFormat mailDateFormat = new MailDateFormat();    // Should addresses in headers be parsed in "strict" mode?    private boolean strict = true;    /**     * Default constructor. An empty message object is created.     * The <code>headers</code> field is set to an empty InternetHeaders     * object. The <code>flags</code> field is set to an empty Flags     * object. The <code>modified</code> flag is set to true.     */    public MimeMessage(Session session) {	super(session);	modified = true;	headers = new InternetHeaders();	flags = new Flags();	// empty flags object	initStrict();    }    /**     * Constructs a MimeMessage by reading and parsing the data from the     * specified MIME InputStream. The InputStream will be left positioned     * at the end of the data for the message. Note that the input stream     * parse is done within this constructor itself. <p>     *     * The input stream contains an entire MIME formatted message with     * headers and data.     *     * @param session	Session object for this message     * @param is	the message input stream     * @exception	MessagingException     */    public MimeMessage(Session session, InputStream is) 			throws MessagingException {	super(session);	flags = new Flags(); // empty Flags object	initStrict();	parse(is);	saved = true;    }    /**     * Constructs a new MimeMessage with content initialized from the     * <code>source</code> MimeMessage.  The new message is independent     * of the original. <p>     *     * Note: The current implementation is rather inefficient, copying     * the data more times than strictly necessary.     *     * @param	source	the message to copy content from     * @exception	MessagingException     * @since		JavaMail 1.2     */    public MimeMessage(MimeMessage source) throws MessagingException {	super(source.session);	flags = source.getFlags();	ByteArrayOutputStream bos;	int size = source.getSize();	if (size > 0)	    bos = new ByteArrayOutputStream(size);	else	    bos = new ByteArrayOutputStream();	try {	    strict = source.strict;	    source.writeTo(bos);	    bos.close();	    SharedByteArrayInputStream bis =			    new SharedByteArrayInputStream(bos.toByteArray());	    parse(bis);	    bis.close();	    saved = true;	} catch (IOException ex) {	    // should never happen, but just in case...	    throw new MessagingException("IOException while copying message",					    ex);	}    }    /**     * Constructs an empty MimeMessage object with the given Folder     * and message number. <p>     *     * This method is for providers subclassing <code>MimeMessage</code>.     */    protected MimeMessage(Folder folder, int msgnum) {	super(folder, msgnum);	flags = new Flags();  // empty Flags object	saved = true;	initStrict();    }    /**     * Constructs a MimeMessage by reading and parsing the data from the     * specified MIME InputStream. The InputStream will be left positioned     * at the end of the data for the message. Note that the input stream     * parse is done within this constructor itself. <p>     *     * This method is for providers subclassing <code>MimeMessage</code>.     *     * @param folder	The containing folder.     * @param is	the message input stream     * @param msgnum	Message number of this message within its folder     * @exception	MessagingException     */    protected MimeMessage(Folder folder, InputStream is, int msgnum)		throws MessagingException {	this(folder, msgnum);	initStrict();	parse(is);    }    /**     * Constructs a MimeMessage from the given InternetHeaders object     * and content.     *     * This method is for providers subclassing <code>MimeMessage</code>.     *     * @param folder	The containing folder.     * @param headers	The headers     * @param content	The message content     * @param msgnum	Message number of this message within its folder     * @exception	MessagingException     */    protected MimeMessage(Folder folder, InternetHeaders headers,		byte[] content, int msgnum) throws MessagingException {	this(folder, msgnum);	this.headers = headers;	this.content = content;	initStrict();    }    /**     * Set the strict flag based on property.     */    private void initStrict() {	if (session != null) {	    String s = session.getProperty("mail.mime.address.strict");	    strict = s == null || !s.equalsIgnoreCase("false");	}    }    /**     * Parse the InputStream setting the <code>headers</code> and     * <code>content</code> fields appropriately.  Also resets the     * <code>modified</code> flag. <p>     *     * This method is intended for use by subclasses that need to     * control when the InputStream is parsed.     *     * @param is	The message input stream     * @exception	MessagingException     */    protected void parse(InputStream is) throws MessagingException {	if (!(is instanceof ByteArrayInputStream) &&	    !(is instanceof BufferedInputStream) &&	    !(is instanceof SharedInputStream))	    is = new BufferedInputStream(is);		headers = createInternetHeaders(is);	if (is instanceof SharedInputStream) {	    SharedInputStream sis = (SharedInputStream)is;	    contentStream = sis.newStream(sis.getPosition(), -1);	} else {	    try {		content = ASCIIUtility.getBytes(is);	    } catch (IOException ioex) {		throw new MessagingException("IOException", ioex);	    }	}	modified = false;    }    /**      * Returns the value of the RFC 822 "From" header fields. If this      * header field is absent, the "Sender" header field is used.      * If the "Sender" header field is also absent, <code>null</code>     * is returned.<p>     *     * This implementation uses the <code>getHeader</code> method     * to obtain the requisite header field.     *     * @return		Address object     * @exception	MessagingException     * @see	#headers     */    public Address[] getFrom() throws MessagingException {	Address[] a = getAddressHeader("From");	if (a == null)	    a = getAddressHeader("Sender");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -