aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes  How To Send Multiple Attachments In An Email Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark " How To Send Multiple Attachments In An Email " Watch " How To Send Multiple Attachments In An Email " New topic
Author

How To Send Multiple Attachments In An Email

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi,

Can anybody tell me that how to send more than one file in attachment of email using java mail api.

In my case the number of attachment is not fixed. But i have to pick it up from one given location.
Like location of folder is : d:\backup
now in backup folder there may be more than one file, which i need to attach.

Please help me.

Thanks,
Kaya.
karimkhan pathan
Ranch Hand

Joined: Jul 14, 2008
Posts: 86
you can use javax.mail.Multipart and addbodypart method for adding attachment using java mail api .

Hope this helps ..


karim
Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
Hi,
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText(message);
multipart.addBodyPart(messageBodyPart);

//attachment...
messageBodyPart = new MimeBodyPart();

String fileAttachment = "D:\\timer.txt";
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName("a1.txt");
multipart.addBodyPart(messageBodyPart);

//second attachment
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
DataSource source2 = new FileDataSource("D:\\searchQuery.txt");
messageBodyPart2.setDataHandler( new DataHandler(source2));
messageBodyPart2.setFileName("a2.txt");
multipart.addBodyPart(messageBodyPart2);
. . .

msg.setContent(multipart);

Hope this is helpful

Cheers


Nishita
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To Send Multiple Attachments In An Email