Today, the Java mail mimeutility. Quote function throws a null pointer exception. Looking for this problem does waste a lot of time: Look at the stack information, look at the source code, debug. Then finally found the cause of the problem:
In the email, the mimebody contains the following line:
Content-Type: Application/octet-stream; name = ""
This sentence is quite amazing. It throws NPE from javamail. But is there a problem with the javamail library, or is this email not compliant with RFC specifications?
As a result, I found the RFC document, Chapter 2045 in RFC 5.1, and found the following content:
Value: = token/quoted-stringtoken: = 1 * <Any (US-ASCII) Char character t space, CTLs, or tspecials> tspecials: = "("/") "/" <"/"> "/"@"/","/";"/": "/" \ "/<"> "/"/"["/"]"/"? "/" = "; Must be in quoted-string,; To use within parameter values
This is a syntax definition. The Lexical definition of the Content-Type header field is clearly written and should not be empty. So javamail finds that it is appropriate to throw an exception if it is null. Therefore, the error lies in the sender. Here, we can see how to discard the mail that cannot be well resolved. Then, report the bug to the sender. So software development... you still need to check the RFC from time to time...
In addition, the above problem is that the NULL pointer exception is thrown when the P. getcontent (); p. getdatahandler () or P. getinputstream () method is called after the multipart P is obtained.