附件第一行之前的JavaMail内容处置

附件第一行之前的JavaMail内容处置,java,jakarta-mail,attachment,content-disposition,Java,Jakarta Mail,Attachment,Content Disposition,我正在尝试将多个文件附加到电子邮件 除了文本文件缺少第一行之外,它工作正常 注意:为了可读性,删除了所有错误处理。此外,假设收件人/发件人/主题等设置正确(电子邮件发送完美-除了附件问题) 首先,以下是我使用的代码: MimeMessage oMessage = new MimeMessage(oSession); // Create a multipart message Multipart oMultiPart = new MimeMultipart(); // Create the me

我正在尝试将多个文件附加到电子邮件

除了文本文件缺少第一行之外,它工作正常

注意:为了可读性,删除了所有错误处理。此外,假设收件人/发件人/主题等设置正确(电子邮件发送完美-除了附件问题)

首先,以下是我使用的代码:

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);
以下是调试输出:

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

根据我在几个涉及电子邮件的项目中的经验,无论有没有附件,我都知道以下几点可以完美地工作。我一直使用Java激活框架在我的代码和电子邮件合成的各种数据源之间提供额外的抽象层。这个框架在几年前已经集成到标准Java发行版中,所以您已经拥有了它。下面你可以找到一个链接,介绍一下它的用法,所以我不会解释它的工作原理,只是给你看一段我最近的一个项目的摘录,其中包括发送多部分电子邮件。鉴于通知对象中提供的电子邮件规范,下面是配置空mimessage的代码。通知对象有一个附件对象数组。附件对象提供字节数组和一些元数据,以帮助在电子邮件中创建文件附件

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,那么就使用一个URL数据源;如果您有一个文件,则使用FileDataSource。 在本例中,我们只处理数据是在其他地方生成的字节数组的附件。因此,数据源是ByteArrayDataSource

下面是一个简单的激活框架

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