JavaMail MimeBodyPart没有被添加到MimeMultipart"正确"

时间:2016-12-07 21:46:25

标签: java javamail

让我们清楚Java工作正常。问题在于程序员!

我需要在电子邮件中添加3个附件(1个zip,1个,1个jpeg)。最初我编写的代码可以在其上添加每个项目 - 并且它可以工作。然后在同一时间添加所有3个项目我使用相同的代码(进行非常小的修改)并将其放入for循环中。这是我遇到问题的地方。该循环为电子邮件添加了3个附件,但问题是所有附件都是相同的附件。具体而言,应该在for循环的前2次迭代中附加的前两​​个附件未附加,并且第三个附件(在第三次迭代中最多为附件的附件)附加3次。

我阅读了Java文档,我尝试了各种各样的更改,并且我很难理解我哪里出错了。现实情况是,我没有足够的编程能力继续前进。我被卡住了。任何建议将不胜感激!我附上了大部分类,但真正的问题是for循环 - 为什么所有3个独特的对象都没有附加?

提前致谢。

    try 
    {
        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress(userLogin));

        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(primaryRecipient));


         MimeBodyPart bodyPart = new MimeBodyPart(); //container to hold the email contents (body only - the text)

         bodyPart.setText(emailBody);

         MimeMultipart multipart = new MimeMultipart();


         multipart.addBodyPart(bodyPart);


         MimeBodyPart mimePart = new MimeBodyPart();

         String filename; //hold the current path
         FileDataSource resource;//object to grab the physical resource

         for(int i = 0; i < itemsToAttach.length; i++)
         {

             filename = itemsToAttach[i];
             System.out.println("Test: " + itemsToAttach[i]);//#############################test only

             resource = new FileDataSource(filename);

             mimePart.setDataHandler(new DataHandler(resource));

             mimePart.setFileName(filename);//###########fix the long ugly name


             multipart.addBodyPart(mimePart);
             System.out.println("multipart contents: " + multipart.toString());
         }


         message.setContent(multipart);

         message.setSubject(emailSubject);

         Transport.send(message);

         System.out.println("Sent message successfully....");
    } 
    catch (MessagingException e) 
    {
        throw new RuntimeException(e);
    }

输出如下:

The file is present!
Sending cc to: mikexxxxxxx@hotmail.com
Sending bcc to: mikexxxxxx@gmail
Test: C:\Users\Mike\workspace\Z_ToTransfer\Assig4_SendEmails\part2_send_email_with_attachment\attachments\test attachments.zip
multipart contents: javax.mail.internet.MimeMultipart@164da25
Test: C:\Users\Mike\workspace\Z_ToTransfer\Assig4_SendEmails\part2_send_email_with_attachment\attachments\axiom.jpeg
multipart contents: javax.mail.internet.MimeMultipart@164da25
Test: C:\Users\Mike\workspace\Z_ToTransfer\Assig4_SendEmails\part2_send_email_with_attachment\attachments\another attachment.png
multipart contents: javax.mail.internet.MimeMultipart@164da25
Sent message successfully....

1 个答案:

答案 0 :(得分:0)

看起来你正在重用同一个对象,它会在循环的每次迭代中被覆盖。 Thant为什么唯一的条目仍然是你发送消息的最后一个mimepart:

您可以执行以下操作:

为每个新文件创建一个MimebodyPart的不同对象:

     MimeBodyPart[] mimePart = new MimeBodyPart[itemsToAttach.length];

     String filename; //hold the current path
     FileDataSource resource;//object to grab the physical resource

     for(int i = 0; i < itemsToAttach.length; i++)
        {
        mimePart[i] = new MimeBodyPart();
         filename = itemsToAttach[i];
         System.out.println("Test: " + itemsToAttach[i]);//#############################test only

         resource = new FileDataSource(filename);

         mimePart[i].setDataHandler(new DataHandler(resource));

         mimePart[i].setFileName(filename);//###########fix the long ugly name


         multipart.addBodyPart(mimePart[i]);
         System.out.println("multipart contents: " + multipart.toString());
     }