Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IMAP - javax.mail. - Fetching Only Body Without Attachment

I am trying to develop an IMAP email client using the javax.mail API. I have been able to contact the servers, fetch emails, attachments and other operations without any problem.

However, we would not want to fetch the attachment until the user wants to view it explit in order to improve the performance. This would mean that we would need information which would indicate if the email has an attachment, if yes, then the filename(s) and the size(s)but would not send me the actual content of the email. However, I was not able to find a method in the API description which would return just the multi-part content.

Is there a way I could fetch just the body and the details of the attachment but not the actual attachment?

Thanks, Aravind

like image 848
Aravind Avatar asked Dec 17 '09 14:12

Aravind


1 Answers

The method getContent() of javax.Part returns

the content as a Java object. The type of the returned object is of course dependent on the content itself. For example, the object returned for "text/plain" content is usually a String object. The object returned for a "multipart" content is always a Multipart subclass.

See http://download.oracle.com/javaee/6/api/javax/mail/Part.html#getContent%28%29

If a Message (which is a Part) contains attachments, the getContent method will return a Multipart object. In addition you can know the MIME type of the part with getContentType.

The information that is missing from the documentation is that this MultiPart object returned by getContent is just an empty representation of the Message's structure. The content of each Part of the Multipart will only be fetched from the server when you specifically ask for it. For example, to parse a multipart Message you would do:

if( p.isMimeType("multipart/*") ) {

    Multipart mp = (Multipart)p.getContent();
    // the content was not fetched from the server

    // parse each Part
    for (int i = 0; i < mp.getCount(); i++) {
        Part inner_part = mp.getBodyPart(i)

        if( inner_part.isMimeType("text/plain") ) {
            String text = inner_part.getText();
            // the content of this Part was fetched from the server
        }
    }
}

Something else to consider is that for optimal performance you should get the body structure information from the server in batch for the collection of messages you want to parse (see http://download.oracle.com/javaee/6/api/javax/mail/FetchProfile.Item.html#CONTENT_INFO). If not, each

Multipart mp = (Multipart)p.getContent();

will result in a request to the server to fetch the body structure. If the FetchProfile approach is used, the body structure for the collection of messages will be fetched with only one request. You can see the requests to the server in the log if you activate debug mode on the session:

session.setDebug(true);

This said, to get size and filenames of attachments just use Part.getSize() and Part.getFileName(), respectively.

like image 200
Paulo Pinto Avatar answered Oct 18 '22 23:10

Paulo Pinto