简体   繁体   中英

JAVA mail api: Why you need Multipart in order to send email with attachment?

I want to send an email which contains only an attached file, no text or anything else.

Why should i represent this as one bodypart of a multipart email?
Why i can't just write

message.setFileName(filepath);

When in the other hand this works

message.setText("blablabla");

Because this is the MIME standards, all email messages should follow the MIME RFC in order for email clients to properly display the email. An attached image is binary data, while a message body is always ASCII, a user cannot open an email with binary in the body displayd, that would be useless. The binary code of the image is translated to BASE64 so the binary can be transfered in sort of ASCII characters and this has to sit in an email MIME boundary. The email software knows how to find these boundaries and will either show the image to the user as an attachment, or displayed the right way in the body, depending on how you setup your MIME boundaries. Multipart means the email has multiple boundaries, where the ASCII email body is one of them, no matter if that one is blanc or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM