File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 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: 19312

Amel MaGdo,
Your post was moved to a new topic.
I agree. Here's the link:
subject: How to send email with multiple attachments