Search code examples
javajakarta-mail

JavaMail MimeBodyPart is not being added to MimeMultipart "properly"


Let it be clear that Java is working correctly. The problem is with the programmer!

I need to add 3 attachments to an email (1 zip, 1 png, 1 jpeg). Initially I wrote code that can add each item on it's own - and it works. Then to add all 3 items at the same time I took the same code (with very minor modifications) and put it in a for loop. This is where I am having an issue. The loop adds 3 attachments to the email, but the problem is that all the attachments are same identical attachment. Specifically, the first two attachments that should be attached in the first 2 iterations of the for loop are not attached, and the third attachment (the attachment that is up to bat at the third iteration) is attached 3 times.

I read the Java docs, I tried all kinds of changes and I am having a hard time understanding where I am going wrong. The reality is that I don't have enough programming ability to move forward. I'm stuck. Any advice would be greatly appreciated! I attached most of the class, but the real problem is in the for loop - why are all 3 unique objects not being attached?

Thanks in advance.

    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 output looks like this:

The file is present!
Sending cc to: [email protected]
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....

Solution

  • Looks like you are reusing the same object and it is getting overwritten on every iteration of the loop. Thant's why the only entry persists is the last mimepart when you send the message:

    You can do following:

    Create a different object of MimebodyPart for every new file:

         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());
         }