unable to frame multi-part for mailing..

Hi Guys,

Please help to frame....

I've SID, CID, MSG[1-n], String recipients[1-n], and


                          DiskFileUpload fu = new DiskFileUpload();
                          // If file size exceeds, a FileUploadException will be thrown
                          fu.setSizeMax(104857600);
                          
                          File []attachments= null;
                          List fileItems = fu.parseRequest(request);
                          Iterator itr = fileItems.iterator();
                          int i = 0;
                          
                          while (it.hasNext())  {  // loop for how many different should go out depending on email addresses ...don't bother
                          while(itr.hasNext()) {
                                FileItem fi = (FileItem)itr.next();
                                 if(!fi.isFormField()) {
                                                 String filename =  fi.getName();  // gets the name of the files into a string. i've many files as such.

//////so here i need a logic to attach all the files available and also sid, cid, msg[1] for first time loop and add only one recipient from recipients[1] list and send him email



                                      
            
                                      
                                      
                                 }
                                
                          }
                          }// end of     while (it.hasNext())
                              


How do i do this...


I know we can attach multiple files.....in this manner

File[] attachments=new File{" " ," "," "}
attachments contains the list pof file to be attached.
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);
}

// found at http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10805301.html?query=javamail+with+attachments&clearTAFilter=true

please help out with this...


 
LVL 10
jagadeesh_motamarriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrigankCommented:
You have to send n mail messages

for each mail message you have to send
a) SID
b) CID
c) Reipent[i] .. only one recipent to send mail to
d) Message[i] to be attached with ith mail.

Am I right here ?

Do you want to  add all attachments to all mails or ith Attachment to ith mail ?

0
mrigankCommented:
Multipart multiPart = new MimeMultipart();
BodyPart messageBody = new MimeBodyPart();
while(iter.hasNext()){
    File attachment =(File)iter.next();
   messageBody = new MimeBodyPart();
    DataSource fileSource = new FileDataSource(attachment);
   messageBody.setDataHandler(new DataHandler(fileSource));
   messageBody.setFileName(attachment.getName());
   multiPart.addBodyPart(messageBody);
}

The above code as you have posted will create a MultiPart object with the attachments.

If you want to send all the attachments to all the recipents

BodyPart messageBody = new MimeBodyPart();
multiPart.addBodyPart(messageBody);
// Now the loop to n
for(i=0 to n)
Create new MimeMessage
Create new MessageBody
Set recipent as recipent[i]
Set message as message[i]
mimeMessage.setContent(multiPart);
Send Mail

What I have tried is created a MulitPart object and added all files as attachment.
Now I create an empty messageBody and add it to multipart.
I populate the values for this messageBody inside the loop.
Going by the fact that Java works on reference, each loop should override the value set by the previous one.

Let me know if it helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrigankCommented:
Sorry the baove loop should be like

BodyPart messageBody = new MimeBodyPart();
multiPart.addBodyPart(messageBody);
// Now the loop to n
for(i=0 to n)
Create new MimeMessage
Do not Create new MessageBody, use existin messageBody (Do not do a new)
Set recipent as recipent[i]
Set message as message[i]
mimeMessage.setContent(multiPart);
Send Mail
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.