Display the contents of a composite message with JavaMail

Source: Internet
Author: User

Compound message: Contains both a picture and body text, which is usually displayed in HTML format. Simple to understand, is a very beautiful kind of mail.

(Personal note, not very comprehensive)

When we need to do a mail-receiving program, how do we display the contents of a composite message?

Today I have a little experience to write out, I hope that the latter can be less detours. Of course, I hope comrades will correct me.

First, we need to get a message object from the folder. This step is not difficult to achieve. You can see many examples. When we get the message object, the next step is to show his content. Before starting this section, you first need to briefly describe several important types of mimetype:

text/html
texl/plain
multipart/alternative
multipart/related

The first two do not have to introduce, one is HTML format body, one is unformatted body. If your message object is one of those two mimetype. Then your mail is not multipart mail. You can just go and show them. As follows:

Object o = m.getContent();
if (m.isMimeType("text/plain")||m.isMimeType("text/html"))
{
s="
"+(String)o+"
";
return s;
}

S is the content string to return. M is the message object.

If your message object is the latter two, it means your email is multipart. Need to do some processing and judgment to normal display content. If the message is multipart, just get the contents of the various parts (part) of the multipart. The content of the entire message is obtained.

if (m.isMimeType("multipart/*")
{
Multipart mp = (Multipart)o;
int cnt = mp.getCount();
for (int i = 0; i < cnt; i++)
{
w=w+"
"+getPart(mp.getBodyPart(i), i, 2,x);
}
}

Getpart is the way to get part content.

How did you get the part?

One thing to emphasize here is that when you do not get the part object, calling Part.getcontent () can get the contents of parts. I was stuck here at the beginning. Why, then? The reason is:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.