This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply locked New topic
Author

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

-Rodricks
Paul Sturrock
Bartender

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();
multipart.addBodyPart(messageBodyPart);

// 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));
messageBodyPart.setFileName(attachments[i].getName());
multipart.addBodyPart(messageBodyPart);
}

// Put parts in message
message.setContent(multipart);

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

its working fine
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Amel MaGdo,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to send email with multiple attachments
 
Similar Threads
ejb environment
servlets
Email from Javaranch
Uploading files
automatic display of the java list on mouseover an image