Knowledgebase
Cannot retrieve HTML content from emails (Java)
Posted by Mohammad Jawwad on 14 November 2008 04:32 PM
Email messages contain messages from several MIME types including "text/html" and "text/plain". An easy way to retrieve the appropriate content type without worrying about the underlying mime type is as follows :

=============================================================================

Imap i = new Imap("host", "username", "pass");
i.setDebug(true);
i.connect();
Enumeration e = i.getMessages();
while(e.hasMoreElements())
{
MimeMessage message = (MimeMessage)e.nextElement();
Enumeration parts = message.getParts();
while (parts.hasMoreElements())
{
MimeMessage m = (MimeMessage)parts.nextElement();
if (m.getContentType().trim().equals("text/plain"))
System.out.println("Plain Text : " + m.getBody());
else if (m.getContentType().trim().equals("text/html"))
System.out.println("Html Text : " + m.getBody());
}
}
i.disconnect();

==============================================================================

Cast each result of getMessage() method call to MimeMessage instead of EmailMessage or HtmlEmailMessage. Use the getParts() method on each of the MimeMessage objects
to retrieve both the plaintext part and the html part. In this manner the caller does not have to worry about the mime type of the email message.
(106 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
Help Desk Software by Kayako fusion