• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How To Send Multiple Attachments In An Email

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

Hope this helps ..
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic