FileDocCategorySizeDatePackage
MimeMessage.javaAPI DocAndroid 1.5 API16868Wed May 06 22:42:46 BST 2009com.android.email.mail.internet

MimeMessage

public class MimeMessage extends com.android.email.mail.Message
An implementation of Message that stores all of it's metadata in RFC 822 and RFC 2045 style headers.

Fields Summary
protected MimeHeader
mHeader
protected com.android.email.mail.Address[]
mFrom
protected com.android.email.mail.Address[]
mTo
protected com.android.email.mail.Address[]
mCc
protected com.android.email.mail.Address[]
mBcc
protected com.android.email.mail.Address[]
mReplyTo
protected Date
mSentDate
protected SimpleDateFormat
mDateFormat
protected com.android.email.mail.Body
mBody
protected int
mSize
private static final Pattern
REMOVE_OPTIONAL_BRACKETS
Constructors Summary
public MimeMessage()


      
        /*
         * Every new messages gets a Message-ID
         */
        try {
            // TODO: This is wasteful, since we overwrite it on incoming or locally-read messages.
            // Should only generate it on as-needed basis.
            setMessageId(generateMessageId());
        }
        catch (MessagingException me) {
            throw new RuntimeException("Unable to create MimeMessage", me);
        }
    
public MimeMessage(InputStream in)
Parse the given InputStream using Apache Mime4J to build a MimeMessage.

param
in
throws
IOException
throws
MessagingException

        parse(in);
    
Methods Summary
public voidaddHeader(java.lang.String name, java.lang.String value)

        mHeader.addHeader(name, value);
    
private java.lang.StringgenerateMessageId()

        StringBuffer sb = new StringBuffer();
        sb.append("<");
        for (int i = 0; i < 24; i++) {
            sb.append(Integer.toString((int)(Math.random() * 35), 36));
        }
        sb.append(".");
        sb.append(Long.toString(System.currentTimeMillis()));
        sb.append("@email.android.com>");
        return sb.toString();
    
public com.android.email.mail.BodygetBody()

        return mBody;
    
public java.lang.StringgetContentId()

        String contentId = getFirstHeader(MimeHeader.HEADER_CONTENT_ID);
        if (contentId == null) {
            return null;
        } else {
            // remove optionally surrounding brackets.
            return REMOVE_OPTIONAL_BRACKETS.matcher(contentId).replaceAll("$1");
        }
    
public java.lang.StringgetContentType()

        String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE);
        if (contentType == null) {
            return "text/plain";
        } else {
            return contentType;
        }
    
public java.lang.StringgetDisposition()

        String contentDisposition = getFirstHeader(MimeHeader.HEADER_CONTENT_DISPOSITION);
        if (contentDisposition == null) {
            return null;
        } else {
            return contentDisposition;
        }
    
protected java.lang.StringgetFirstHeader(java.lang.String name)

        return mHeader.getFirstHeader(name);
    
public com.android.email.mail.Address[]getFrom()

        if (mFrom == null) {
            String list = MimeUtility.unfold(getFirstHeader("From"));
            if (list == null || list.length() == 0) {
                list = MimeUtility.unfold(getFirstHeader("Sender"));
            }
            mFrom = Address.parse(list);
        }
        return mFrom;
    
public java.lang.String[]getHeader(java.lang.String name)

        return mHeader.getHeader(name);
    
public java.io.InputStreamgetInputStream()

        return null;
    
public java.lang.StringgetMessageId()
Get the mime "Message-ID" header. Note, this field is preset (randomly) in every new message, so it should never return null.

return
the Message-ID header string
throws
MessagingException

        String[] headers = getHeader("Message-ID");
        if (headers != null) {
            // There should really only be one Message-ID here
            return headers[0];
        }
        throw new MessagingException("A message was found without a Message-ID header");
    
public java.lang.StringgetMimeType()

        return MimeUtility.getHeaderParameter(getContentType(), null);
    
public java.util.DategetReceivedDate()

        return null;
    
public com.android.email.mail.Address[]getRecipients(RecipientType type)
Returns a list of the given recipient type from this message. If no addresses are found the method returns an empty array.

        if (type == RecipientType.TO) {
            if (mTo == null) {
                mTo = Address.parse(MimeUtility.unfold(getFirstHeader("To")));
            }
            return mTo;
        } else if (type == RecipientType.CC) {
            if (mCc == null) {
                mCc = Address.parse(MimeUtility.unfold(getFirstHeader("CC")));
            }
            return mCc;
        } else if (type == RecipientType.BCC) {
            if (mBcc == null) {
                mBcc = Address.parse(MimeUtility.unfold(getFirstHeader("BCC")));
            }
            return mBcc;
        } else {
            throw new MessagingException("Unrecognized recipient type.");
        }
    
public com.android.email.mail.Address[]getReplyTo()

        if (mReplyTo == null) {
            mReplyTo = Address.parse(MimeUtility.unfold(getFirstHeader("Reply-to")));
        }
        return mReplyTo;
    
public java.util.DategetSentDate()

        if (mSentDate == null) {
            try {
                DateTimeField field = (DateTimeField)Field.parse("Date: "
                        + MimeUtility.unfoldAndDecode(getFirstHeader("Date")));
                mSentDate = field.getDate();
            } catch (Exception e) {

            }
        }
        return mSentDate;
    
public intgetSize()

        return mSize;
    
public java.lang.StringgetSubject()
Returns the unfolded, decoded value of the Subject header.

        return MimeUtility.unfoldAndDecode(getFirstHeader("Subject"));
    
protected voidparse(java.io.InputStream in)

        // Before parsing the input stream, clear all local fields that may be superceded by
        // the new incoming message.
        mHeader.clear();
        mFrom = null;
        mTo = null;
        mCc = null;
        mBcc = null;
        mReplyTo = null;
        mSentDate = null;
        mBody = null;

        MimeStreamParser parser = new MimeStreamParser();
        parser.setContentHandler(new MimeMessageBuilder());
        parser.parse(new EOLConvertingInputStream(in));
    
public voidremoveHeader(java.lang.String name)

        mHeader.removeHeader(name);
    
public voidsaveChanges()

        throw new MessagingException("saveChanges not yet implemented");
    
public voidsetBody(com.android.email.mail.Body body)

        this.mBody = body;
        if (body instanceof com.android.email.mail.Multipart) {
            com.android.email.mail.Multipart multipart = ((com.android.email.mail.Multipart)body);
            multipart.setParent(this);
            setHeader(MimeHeader.HEADER_CONTENT_TYPE, multipart.getContentType());
            setHeader("MIME-Version", "1.0");
        }
        else if (body instanceof TextBody) {
            setHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("%s;\n charset=utf-8",
                    getMimeType()));
            setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
        }
    
public voidsetFrom(com.android.email.mail.Address from)

        if (from != null) {
            setHeader("From", from.toString());
            this.mFrom = new Address[] {
                    from
                };
        } else {
            this.mFrom = null;
        }
    
public voidsetHeader(java.lang.String name, java.lang.String value)

        mHeader.setHeader(name, value);
    
public voidsetMessageId(java.lang.String messageId)
Set the mime "Message-ID" header

param
messageId the new Message-ID value
throws
MessagingException

        setHeader("Message-ID", messageId);
    
public voidsetRecipients(RecipientType type, com.android.email.mail.Address[] addresses)

        if (type == RecipientType.TO) {
            if (addresses == null || addresses.length == 0) {
                removeHeader("To");
                this.mTo = null;
            } else {
                setHeader("To", Address.toString(addresses));
                this.mTo = addresses;
            }
        } else if (type == RecipientType.CC) {
            if (addresses == null || addresses.length == 0) {
                removeHeader("CC");
                this.mCc = null;
            } else {
                setHeader("CC", Address.toString(addresses));
                this.mCc = addresses;
            }
        } else if (type == RecipientType.BCC) {
            if (addresses == null || addresses.length == 0) {
                removeHeader("BCC");
                this.mBcc = null;
            } else {
                setHeader("BCC", Address.toString(addresses));
                this.mBcc = addresses;
            }
        } else {
            throw new MessagingException("Unrecognized recipient type.");
        }
    
public voidsetReplyTo(com.android.email.mail.Address[] replyTo)

        if (replyTo == null || replyTo.length == 0) {
            removeHeader("Reply-to");
            mReplyTo = null;
        } else {
            setHeader("Reply-to", Address.toString(replyTo));
            mReplyTo = replyTo;
        }
    
public voidsetSentDate(java.util.Date sentDate)

        setHeader("Date", mDateFormat.format(sentDate));
        this.mSentDate = sentDate;
    
public voidsetSubject(java.lang.String subject)

        final int HEADER_NAME_LENGTH = 9;     // "Subject: "
        setHeader("Subject", MimeUtility.foldAndEncode2(subject, HEADER_NAME_LENGTH));
    
public voidwriteTo(java.io.OutputStream out)

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
        mHeader.writeTo(out);
        writer.write("\r\n");
        writer.flush();
        if (mBody != null) {
            mBody.writeTo(out);
        }