Bookmark Topic Watch Topic
  • New Topic

How to send email with multiple attachments

 
Rodricks george
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Rodricks george
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Sheriff
Pie
Posts: 20735
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
Amel MaGdo,
Your post was moved to a new topic.
 
    Bookmark Topic Watch Topic
  • New Topic