• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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...


 
0
jagadeesh_motamarri
Asked:
jagadeesh_motamarri
  • 3
1 Solution
 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now