Display the contents of composite mail with javamail [reproduced]

zhaozj2021-02-16  80

Use JavaMail to display the contents of the composite mail

Composite email: contains both pictures and text, usually HTML format display. If you understand, if you are very beautiful, it is very beautiful. (Individual Note, Not Very Comprehensive) When we need to make a program that charges mail, how should we display the contents of composite mail? Today, I have to write me some experience, I hope that later people can walk less. Of course, I also hope that comrades are more correct. First, we need to get a Message object from the folder. This step is not difficult to implement. You can see a lot of examples. When we get this Message object, the next step is to show his content. Before you begin, you first need to briefly introduce several important types of mormtype:

Text / HTML TEXL / Plainmultipart / AlternativeMultipart / Related

The first two don't have to be introduced, one is the body of the HTML format, one is a form of format. If your Message object is one of these two mimeType. So your email is not Multipart's mail. You can display them directly. as follows:

Object o = m.getContent (); if ("text / place") || m.ismensype ("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, you indicate that your email is Multipart. You need to do some processing and judgment to display the content normally. If Message is Multipart, just get the contents of each part of Multipart. The content of the entire Message is obtained.

IF (MULTIPARTYPE ("Multipart / *") {Multipart MP = (Multipart) O; int CNT = mp.getCount (); for (int i = 0; i

GetPart is a way to get part content.

Then, how do you get part to get?

One thing to emphasize here is that after the part object is obtained, call Part.getContent () to get the contents of Part. I was attacked here. why? The reason is that:

Theoretically specify the first part of the content, neither a text / place type, nor a Text / HTML type. Therefore, the 15th line is never settled. Later, after the study found that the first part of the specified content is another Multipart type. That is, this message can be divided into multiple parts, body, and pictures, etc., and the body can be divided into multiple parts, such as the body of PLAIN and the body of HTML. In this way, it is like a tree. Only when you reach the leaves, you can know that PLAIN is still HTML. Therefore, we have solved the problem in the transfer of the call itself in Part.

1 public string getpart (Part part, int partnum) 2 throws messagingException, ioException3 {4 string s = ""; 5 string S1 = "; 6 string s2 =" "; 7 string s3 =" "; 8 string SCT = Part .getContentType (); 9 if (SCT == NULL) 10 {11 s = "part invalid"; 12 RETURN S; 13} 14 ContentType CT = New ContentType (SCT); 15 IF ("text / plain ")) 16 {17 // Display text / plain inline18 S1 =" " " ""; 19} 20 else21 {22 string temp = ""; 23 IF ((Temp = Part) GetFileName ())! = null 24 S2 = "FileName:" Temp ""; 25 TEMP = NULL; 26 IF ((Temp = Part.getDescription ())! = null 27 S3 = "Description:" Temp ""; 28} 29 s = S1 S2 S3; 30 RETURN S; 31} The following is a transformed GetPart method (very crude, unimpeded), using it to display composite messages correctly.

Public String getPart (Part part, int x) throws messagingException, ioException {string s = "; string s1 ="; x parameter is determined to display in HTML format or in Plainstring S2 = "" String S3 = "" "; string Sct = part.getContentType (); if (SCT == NULL) {s =" part invalid "; returnit s;} ContentTy CT = New ContentType (SCT); if (CT.Match ("text / html") || ct.match ("text / place")) {// display text / plain inlines1 = " (string) Part.getContent () ";} else IF (partnum! = 0) {string temp = ""; if ((Temp = part.getFileName ())! = null) {s2 = "FileName:" TEMP "";} / * out.println ("

转载请注明原文地址:https://www.9cbs.com/read-12309.html

New Post(0)