dcsimg
sending multiple attachments at a time
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Sunday, July 8, 2001 02:36 AM

how can I send MULTIPLE attachments at a time. Could you please give me such code if you have.

Re: sending multiple attachments at a time

Posted By:   rommel_sharma  
Posted On:   Tuesday, January 1, 2002 10:05 PM

The following code works.I have two file upload fields (could be more) named as attachment1 and attachment2, and when the form is submitted then in the form-dat received, filenames specified in file upload fields are concatenated in one string, called 'filename' in the example below:


 MimeBodyPart bodyPart = new MimeBodyPart();
int attachmentCount = 0;
StringTokenizer sTokens = new StringTokenizer(filename);
while(sTokens.hasMoreTokens())
{
attachmentCount++;
String currentFile = sTokens.nextToken();
DataSource ds = new ByteArrayDataSource(buffer.toByteArray(),contentType,currentFile);
bodyPart.setDataHandler(new DataHandler(ds));
bodyPart.setDisposition("attachment"+attachmentCount+"; filename="" +currentFile + """);
bodyPart.setFileName(currentFile);
hashtableObject.put(name,bodyPart);
}



The next part of the code stores the value of the files as BodyPart:



BodyPart attachment1 = (BodyPart)hashtableObject.get("attachment1");
BodyPart attachment2 = (BodyPart)hashtableObject.get("attachment2");



Then add the bodyparts(repeat for attachment 2):



multipartObject.addBodyPart(attachment1);
msg.setContent(multipartObject);



Also please refer to the excellent tutorial on JavaMail by Benoit Marchal at http://www.marchal.com.

Re: sending multiple attachments at a time

Posted By:   John_Zukowski  
Posted On:   Sunday, July 8, 2001 06:22 AM

Sending attachments is already covered in the FAQ.
About | Sitemap | Contact