我試圖將多個文件附加到電子郵件。JavaMail內容 - 在第一行附件之前處置
它工作正常,除了文本文件丟失的第一行。
注意:爲便於閱讀,刪除了所有錯誤處理。此外,假設正確設置收件人/發件人/主題等(電子郵件發送完美 - 除了附件問題)。
首先,這裏我使用的代碼:
MimeMessage oMessage = new MimeMessage(oSession);
// Create a multipart message
Multipart oMultiPart = new MimeMultipart();
// Create the message part
BodyPart oMessageBodyPart = new MimeBodyPart();
// Set the Message Body
String strFormat = oEmail.getFormat();
String strBody = oEmail.getBody();
oMessageBodyPart.setContent(strBody,strFormat);
oMultiPart.addBodyPart(oMessageBodyPart);
List<String> oAttachmentNames = oEmail.getAttachments();
for (String strAttachmentName : oAttachmentNames)
{
// Parse file from URL
URL oURL = new URL(strAttachmentName);
MimeBodyPart oAttachmentPart = new MimeBodyPart(oURL.openStream());
oAttachmentPart.setFileName(strAttachmentName);
oMultiPart.addBodyPart(oAttachmentPart);
}
// Add all contents (body + attachments)
oMessage.setContent(oMultiPart);
的文本文件的方式如下:
This is the Test file
(intentional line break)
Line 1
Line 2
這裏的調試輸出:
Content-Type: multipart/mixed;
boundary="----=_Part_0_29194312.1354442889470"
------=_Part_0_29194312.1354442889470
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Plain Text Email.
------=_Part_0_29194312.1354442889470
This is the Test file
Content-Disposition: attachment;
filename="http://mysite.com/temp/Test.txt"
Line 1
Line 2
------=_Part_0_29194312.1354442889470--
.
250 OK id=1Tf6T5-0004E9-Nn
QUIT