mjawwad
11-14-2008, 03:29 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", "user", "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.
================================================== ===========================
Imap i = new Imap("host", "user", "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.