2012-12-02 84 views
2

我試圖將多個文件附加到電子郵件。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 

回答

2

根據我在涉及電子郵件的幾個項目中的經驗,無論有沒有附件,我都知道以下內容是完美無缺的。我一直使用Java Activation框架在我的代碼和用於電子郵件組合的各種數據源之間提供額外的抽象層。這個框架已經在幾年前被整合到了標準的Java發行版中,所以你已經擁有它了。下面你可以找到一些關於它的用法的一些簡單介紹的鏈接,所以我不會解釋它的工作原理,但只是向你展示我的最新項目之一,其中包括髮送多部分電子郵件。給定通知對象中提供的電子郵件規範,以下是配置空MimeMessage的代碼。通知對象具有一個Attachment對象數組。 Attachment對象提供了一個字節數組和一些元數據來幫助在電子郵件中創建一個文件附件。

private void configureMessage(Message message, Notification notification) throws MessagingException { 
    DataHandler messageDataHandler = new DataHandler(notification.getMessage(), "text/plain; charset=\"UTF-8\""); 
    if (notification.getAttachments() != null && !notification.getAttachments().isEmpty()) { 
     log.debug("configureMessage: Adding attachments."); 
     MimeMultipart multipart = new MimeMultipart(); 

     // een body part voor de tekstuele boodschap 
     BodyPart mainBodyPart = new MimeBodyPart(); 
     mainBodyPart.setDataHandler(messageDataHandler); 
     multipart.addBodyPart(mainBodyPart); 

     for (Attachment attachment : notification.getAttachments()) { 
      log.debugv("configureMessage: Adding attachment {0}.", attachment); 
      // een body part voor de attachment 
      MimeBodyPart attachmentPart = new MimeBodyPart(); 
      ByteArrayDataSource attachmentDataSource = 
        new ByteArrayDataSource(attachment.getBytes(), attachment.getMimeType()); 
      attachmentPart.setDataHandler(new DataHandler(attachmentDataSource)); 
      attachmentPart.setDisposition(Part.ATTACHMENT); 
      attachmentPart.setFileName(attachment.getFileName()); 
      multipart.addBodyPart(attachmentPart); 
     } 
     message.setContent(multipart); 
    } else { 
     log.debug("configureMessage: No attachments."); 
     message.setDataHandler(messageDataHandler); 
    } 
} 

正如你看到的,那就是進入消息的所有數據首先被包裹在一個DataHandler的。該文本消息進入這樣的數據處理程序:

DataHandler messageDataHandler = new DataHandler(notification.getMessage(), "text/plain; charset=\"UTF-8\""); 

如果一個身體部位的輸入不只是一個字符串,但一些其他形式的,那麼你用一個數據源,具體爲你輸入的類型。如果你有一個URL,那麼使用一個URLDataSource;如果你有一個文件,然後使用FileDataSource。 在這個例子中,我們只處理其數據是在其他地方生成的字節數組的附件。因此數據源是一個ByteArrayDataSource。

這裏有一個簡單的introduction進入Activation Framework。

相關問題