This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Send Multiple Attachments In An Email  RSS feed

 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use javax.mail.Multipart and addbodypart method for adding attachment using java mail api .

Hope this helps ..
 
Nishita Jain
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!