Knowledgebase: Components > Java > Secure iNet Factory
Cannot retrieve HTML content from emails (Java)
Posted by Mohammad Jawwad, Last modified by 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");
Enumeration e = i.getMessages();
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());


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.
(107 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Post a new comment
Full Name:
CAPTCHA Verification 
Please enter the text you see in the image into the textbox below. This is required to prevent automated registrations and form submissions.

Help Desk Software by Kayako