The below code sends 1 attachment to a recipient. However, I need to send 3 files to the recipient. This is my first attempt at using JavaMail and the am finding difficulty resolving the "multipart" section to accept 3 files. The issue is that while each file is selected and each multipart is there, the last portion of the multipart (multipart3 and multipart4) is not added to the message. If I comment out the last two (multipart3 and multipart4) lines of the msg.setContent, then the message goes through with the first attachment. Really could use some help in this.
It's been a long, long time since I wrote JavaMail code. But let me take a try:
Yes. That sounds like a good idea. But your subsequent code doesn't do that. Instead it creates several Multipart objects, and adds one MimeBodyPart object to each of them. I'd guess that using only one Multipart object would be a better strategy.
But just to be sure, since it's been well over a decade since I did this, I spent a moment to track down a tutorial. And yes, I'm right. Here's the tutorial: Sending Emails with Attachments in Java.
Thank you so much for your willingness to help me with this. I redid the multipart section to reduce some of the clutter and am still getting just one attachment being sent. I read your tutorial and it helped me reduce all the excess code lines. However, I still can't seem to attach the 2nd and 3rd file attachments. It attaches the first file with no problem and sends the email to the recipient. Below are the multipart changes I made. I use System print to verify that the file selection and the attachment part were not empty. Both seem to be working okay. I'm just missing something in my code to tell the multipart that it needs to recognize each file independently and keep them as attachments.
The problem is with the call to msg.setContent. That overrides the previous content. What you need to do, and what Ron did, was create one single MimeMultipart outside the loop, set that as content once, and add parts to it inside the loop; not create a new one for every attachment and overwrite the one for the previous attachment.
Ron -- I see what you are referring to -the msg.setContent is within the loop so it gets replaced each time. That seems to be the problem even with the version you posted. Here's why: I can comment out the 2nd and 3rd files and the program will send the 1st file easily in both Paul's and yours. In both versions (Paul & Ron) the program can't find the following files even though all three files are in the same directory. However, I did set print so I can see that the array is working fine and the files are being accessed sequentially. However, only the last file itinerary is actually used. Since I have 4 loops, the last file added is the first one.
I tried moving the setContent outside the "for loop" but then I get an error stating that multipart can't be found. I do agree with your assessment as to the setContent getting files replaced each time the loop calls a new file. Just don't know how to resolve that. The problem is consistent with each version sample I use. All other portions work fine --multipart just will not keep all three files -- merely replaces each file as the new one is called.
This looks problematic:
The third parameter is supposed to be a MIME Type, which for a MS Word document would normally be: application/msword or application/vnd.openxmlformats-officedocument.wordprocessingml.document .
Try changing that and see if you have any more more success.
With the messageBodyPart3 and messageBodyPart4 included in the code I get an error message that states it can't find the next file (Plumb1-13052021.xlsx). However, all three files are in the same directory and it has no problem finding the first attachment (Plumb-13052021.docx). So it runs okay with the 3rd and 4th attachments "commented out." The error states it can't find the next attachment (Plumb1-13052021.xlsx) even though it has no problem locating the first one.
I'm using Java.mail-1.6.0 and activation-1.1. Here's error I'm getting:
It seems like even though you believe that the xlsx files do exist, they do not (to the application anyway). Maybe it is a typo that is difficult to see, maybe a permissions problem, ???
To troubleshoot, put something like this in your code to print out what Plumb files are actually present/visible from the application's perspective.If the output looks ok, then try copying the file names from the output, and use those copied strings to replace the file name in your code just to be certain that the file name on disk matches the file name in your application code.
Thank you so much for your persistence and support on this. I added the two lines you sent me and the reason the program didn't send the other 2 attachments was that I had the wrong file ending. It should have been "csv" and not "xlsx". Honestly, I was embarrassed since I then remember that the two spreadsheet files were actually exported database field entries --so the extension should have been "csv". The program now sends all three files without any issues. Thank you.
Is there some checkmark or uptick for this to show my approval of your support?