This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes How to send email with multiple attachments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply locked New topic

How to send email with multiple attachments

Rodricks george
Ranch Hand

Joined: Sep 10, 2006
Posts: 97
Hi.... i want to send all files inside a particular folder as attachments.... can i do that using javamail api

Thnx in advance

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Yes. Though I'm not sure the recipient of an email with loads of attachements would thank you for doing it!

An amil with attachments is just a MIME multipart message, each part representing one attachement. Look at the attachFile method of MimeBodyPart.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rodricks george
Ranch Hand

Joined: Sep 10, 2006
Posts: 97
File f = new File("e:\\mailFolder");
File[] attachments = f.listFiles();

Multipart multipart = new MimeMultipart();

// Part two is attachment
for( int i = 0; i < attachments.length; i++ ) {
messageBodyPart = new MimeBodyPart();
FileDataSource fileDataSource =new FileDataSource(attachments[i]);
messageBodyPart.setDataHandler(new DataHandler(fileDataSource));

// Put parts in message

// Send the message
Transport.send( message );

its working fine
Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

Amel MaGdo,
Your post was moved to a new topic.
Have you checked out Aspose?
subject: How to send email with multiple attachments
jQuery in Action, 3rd edition