Could not parse this multi-user mime text in Java

I am not writing a mail application, so I do not have access to all the headers and the like. Everything I have seems like a block at the end of this question. I tried using the JavaMail API to parse this using something like

Session s = Session.getDefaultInstance(new Properties()); InputStream is = new ByteArrayInputStream(<< String to parse >>); MimeMessage message = new MimeMessage(s, is); Multipart multipart = (Multipart) message.getContent(); 

But that just tells me that message.getContent is a String, not a Multipart or MimeMultipart. Also, I don't need all the overhead of the entire JavaMail API, I just need to parse the text on it. Here is an example:

<Preview> This is from several parts of the message in the MIME format a \ n \ n ------ = _ NextPart_000_005D_01CC73D5.3BA43FB0 \ nContent-Type :. Text / Plain; \ n \ tcharset = "ISO-8859-1" \ nContent-Transfer -Encoding: quoted-printable \ n \ nStuff: \ n \ n Read this material at the beginning of each week. = \ nFree to discuss it for a week. \ n \ n \ n - = 20 \ n \ nMrs. Suzy M. Smith \ n555-555-5555 \ nsuzy@suzy.com \ n ------ = _ NextPart_000_005D_01CC73D5.3BA43FB0 \ nContent-Type: Text / html; \ n \ tcharset = "ISO-8859-1" \ nContent-Transfer-Encoding: quotation mark for printing \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ Nstuff: \ n = 20 \ nPlease read this material at the beginning of each = \ nweek. Feel = 20 \ nfree to discuss this for a week. \ N
-

Mrs. Susie M. Smith
555-555-5555
suzy@suzy.com \ n \ n ------ = _ NextPart_000_005D_01CC73D5.3BA43FB0 - \ n \ n
+4
source share
4 answers

There are a few errors in the text you posted.

This is not a valid multiplayer meme. Check out the wikipedia link , which, although not normative, is still correct.

Mimic border is not defined. From the wikipedia example: Content-Type: multipart/mixed; boundary="frontier" Content-Type: multipart/mixed; boundary="frontier" indicates that the border is a "border". In your example, "---- = _ NextPart_000_005D_01CC73D5.3BA43FB0" is the border, but this can only be determined by scanning text ( ie ). You need to instruct goofball, which gives you mime content, which you also need to know the mime border value that is not defined in the message header. If you get all the text of the message, which you will have enough, because the body of the message starts with MIME-Version: 1.0 , followed by Content-Type: multipart/mixed; boundary=" frontier " Content-Type: multipart/mixed; boundary=" frontier " , where the frontier will be replaced by the border value for the encoded mime.

If the person sending the body is goofball (changed from a monkey because the monkey is too subjective - my bad DwB) and will not (rather know how) send the full body, you can get borders by scanning the text for the line that starts and ends with the symbol "-" ( ie --boundary--). Please note that I mentioned the "line". The terminal border is actually "- borderline - \ n".

Finally, the material you published is in two parts. The first part seems to determine the replacements to be made in the second part. If this is the case, the Content-Type: of the first part should probably be something other than "text / plain". Maybe "companyname / substitution-definition" or something like that. This will allow the use of multiple (as in future improvements) replacement formats.

+6
source

First, I took your sample message and replaced all occurrences of \n with newlines and \t with tabs.

Then I downloaded the JAR from the Mime4J project, the Apache James subproject, and performed a GUI analysis on the org.apache.james.mime4j.samples.tree.MessageTree GUI example with the converted message above as input. And apparently Mime4J was able to parse the message and extract part of the HTML message.

+7
source

Can create MimeMultipart from http request.

 javax.mail.internet.MimeMultipart m = new MimeMultipart(new ServletMultipartDataSource(httpRequest)); public class ServletMultipartDataSource implements DataSource { String contentType; InputStream inputStream; public ServletMultipartDataSource(ServletRequest request) throws IOException { inputStream = new SequenceInputStream(new ByteArrayInputStream("\n".getBytes()), request.getInputStream()); contentType = request.getContentType(); } public InputStream getInputStream() throws IOException { return inputStream; } public OutputStream getOutputStream() throws IOException { return null; } public String getContentType() { return contentType; } public String getName() { return "ServletMultipartDataSource"; } } 

To get the presented form parameter, you need to analyze the BodyPart headers:

 public String getStringParameter(String name) throws MessagingException, IOException { for (int i = 0; i < getCount(); i++) { BodyPart bodyPart = m.getBodyPart(i); String[] nameHeader = bodyPart.getHeader("Content-Disposition"); if (nameHeader != null && content instanceof String) { for (String bodyName : nameHeader) { if (bodyName.contains("name=\"" + name + "\"")) return String.valueOf(bodyPart.getContent()); } } } return null; } 
+4
source

If you use javax.servlet.http.HttpServlet to receive the message, you will need to use HttpServletRequests.getHeaders to get the value of the content type of the HTTP header. You will then use org.apache.james.mime4j.stream.MimeConfig.setHeadlessParsing to set up MimeConfig with information so that it can correctly handle the mime message.

It seems that you are using HttpServletRequest.getInputStream to read the contents of the request. The returned input stream has only the message content after the HTTP headers (ends with an empty string). This is why you need to extract the content type from the HTTP headers and pass it to the parser using setHeadlessParsing.

+2
source

All Articles