3 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個贊
使用multipart/mixed
與第一部分為multipart/alternative
及以后的零件的附件。反過來,在零件內(nèi)使用text/plain
和text/html
零件multipart/alternative
。
有能力的電子郵件客戶端應(yīng)然后識別該multipart/alternative
部分,并在必要時顯示文本部分或html部分。它還應(yīng)將所有隨后的零件顯示為附件。
這里要注意的重要一點(diǎn)是,在多部分MIME消息中,部分內(nèi)包含部分是完全有效的。從理論上講,嵌套可以擴(kuò)展到任何深度。然后,任何具有合理能力的電子郵件客戶端都應(yīng)該能夠遞歸處理所有消息部分。

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個贊
消息有內(nèi)容。內(nèi)容可以是文本,html,DataHandler或Multipart,并且只能有一個內(nèi)容。多個零件只有身體零件,但可以有多個零件。像消息一樣,BodyParts可以包含已經(jīng)描述的內(nèi)容。
帶有HTML,文本和附件的消息可以按如下所示分層查看:
message
mainMultipart (content for message, subType="mixed")
->htmlAndTextBodyPart (bodyPart1 for mainMultipart)
->htmlAndTextMultipart (content for htmlAndTextBodyPart, subType="alternative")
->textBodyPart (bodyPart2 for the htmlAndTextMultipart)
->text (content for textBodyPart)
->htmlBodyPart (bodyPart1 for htmlAndTextMultipart)
->html (content for htmlBodyPart)
->fileBodyPart1 (bodyPart2 for the mainMultipart)
->FileDataHandler (content for fileBodyPart1 )
以及構(gòu)建此類消息的代碼:
// the parent or main part if you will
Multipart mainMultipart = new MimeMultipart("mixed");
// this will hold text and html and tells the client there are 2 versions of the message (html and text). presumably text
// being the alternative to html
Multipart htmlAndTextMultipart = new MimeMultipart("alternative");
// set text
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText(text);
htmlAndTextMultipart.addBodyPart(textBodyPart);
// set html (set this last per rfc1341 which states last = best)
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(html, "text/html; charset=utf-8");
htmlAndTextMultipart.addBodyPart(htmlBodyPart);
// stuff the multipart into a bodypart and add the bodyPart to the mainMultipart
MimeBodyPart htmlAndTextBodyPart = new MimeBodyPart();
htmlAndTextBodyPart.setContent(htmlAndTextMultipart);
mainMultipart.addBodyPart(htmlAndTextBodyPart);
// attach file body parts directly to the mainMultipart
MimeBodyPart filePart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("/path/to/some/file.txt");
filePart.setDataHandler(new DataHandler(fds));
filePart.setFileName(fds.getName());
mainMultipart.addBodyPart(filePart);
// set message content
message.setContent(mainMultipart);
- 3 回答
- 0 關(guān)注
- 434 瀏覽
添加回答
舉報