Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The method <code>getContent()</code> of <code>javax.Part</code> returns </p> <blockquote> <p>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.</p> </blockquote> <p>See <a href="http://download.oracle.com/javaee/6/api/javax/mail/Part.html#getContent%28%29" rel="nofollow">http://download.oracle.com/javaee/6/api/javax/mail/Part.html#getContent%28%29</a></p> <p>If a <code>Message</code> (which is a <code>Part</code>) contains attachments, the getContent method will return a <code>Multipart</code> object. In addition you can know the MIME type of the part with <code>getContentType</code>.</p> <p>The information that is missing from the documentation is that this <code>MultiPart</code> object returned by <code>getContent</code> is just an empty representation of the Message's structure. The content of each <code>Part</code> of the <code>Multipart</code> will only be fetched from the server when you specifically ask for it. For example, to parse a multipart <code>Message</code> you would do:</p> <pre><code>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 &lt; 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 } } } </code></pre> <p>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 <a href="http://download.oracle.com/javaee/6/api/javax/mail/FetchProfile.Item.html#CONTENT_INFO" rel="nofollow">http://download.oracle.com/javaee/6/api/javax/mail/FetchProfile.Item.html#CONTENT_INFO</a>). If not, each</p> <p><code>Multipart mp = (Multipart)p.getContent();</code></p> <p>will result in a request to the server to fetch the body structure. If the <code>FetchProfile</code> 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:</p> <pre><code>session.setDebug(true); </code></pre> <p>This said, to get size and filenames of attachments just use <code>Part.getSize()</code> and <code>Part.getFileName()</code>, respectively.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload