• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Sending multiple attachment files with javamail

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bill Melendez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul:

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.


 
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took your code, removed bits which didn't seem like they were part of the problem, and created a couple of methods to reduce repeated code.

This example does work - maybe you can use it to help debug your code.

Staff note (Ron McLeod) :

Note: line 81 should be: messageBodyPart.setHeader("Content-Type", mimeType);

 
Sheriff
Posts: 22213
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bill Melendez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul, Ron:

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.

Any help would be greatly appreciated.
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the current version of your code so that we can better understand the issues that you are describing.
 
Bill Melendez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! Here's the code. I am using the one posted last, though either one would work with just one attachment --but not two or three.
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bill Melendez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried to post the other day --seems it didn't take. So I'm trying again. The attached program still will not let me send more than one attachment. I get a null exception.

 
Paul Clapham
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Melendez wrote:Tried to post the other day --seems it didn't take. So I'm trying again. The attached program still will not let me send more than one attachment. I get a null exception.



Does that mean that when you tried the identical code, but with only one attachment, you didn't get that exception?
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I see when I run what you posted (without any Swing code):

 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which version of JavaMail are you using?  I did my tests with:
    javax.mail-1.6.2.jar (com.sun.mail:javax.mail:1.6.2)
    activation-1.1.jar (javax.activation:activation:1.1) [dependency]
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a typo in the example code that I posted (missing dash in Content-Type header name) - not sure if it would have any effect on the problem that you are seeing:

This:   messageBodyPart.setHeader("ContentType", mimeType);

Should be this:   messageBodyPart.setHeader("Content-Type", mimeType);
 
Bill Melendez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bill Melendez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron:

I changed the JavaMail to 1.6.2 but get the same error codes.
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bill Melendez
Greenhorn
Posts: 20
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron:

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?
 
Ron McLeod
Marshal
Posts: 3572
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome - glad to see that you stuck it out and got it working in the end.  Sometimes the most obvious problems are to hardest ones to spot.

Our 'uptick' is a click on the 'thumbs-up' icon:
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic