I am doing a wcf C#.net project in which i need to send multiple emails asynchronously with one pdf attachment. I use smtp.sendAsync() to do it. Please guide me on this and clarify my doubts.
The logic i have used is, store all the receiver's email ids in a list. Then, in a foreach loop, inside each loop call my function say, sendMailsaAsynch(objList, filename). This function is responsible for opening the pdf file using the filename specified, and attach it to a new object of MailAddress and call smtp.sendAsync().
Expand|Select|Wrap|Line Numbers
- function sendMailsAsynch(objList, filename)
- {
- //open the file
- fs = new FileStream(...) //LINE 1
- mm = new MailMessage();
- Attachment a = ....
- .. smtp = new
- try
- {
- smtp.SendCompleted += new SendCompletedEventHandler(smtp_OnCompleted);
- smtp.sendAsynch();
- }
- catch{ throw;}
- finally { }
- }
The following is how i call the function in foreach loop.
Expand|Select|Wrap|Line Numbers
- funcion sendMails(List list)
- {
- //fs = new FileStream(....)
- foreach(List l in list)
- {
- sendMailsAsynch(list, filenme);
- }
- }
One more important thing is: If i keep breakpoint and check in the smtp_OnCompleted delegate function, sometimes the breakpoint is hit after all the try-catch-finally is completed. ie. after all the loops are finished. But sometimes i get the breakpoint hit on this delegate function after the end of each try-cath ie. at the end for each loop. Can anyone explain how threading is handled here.
I do not want to use MessageQueing. I think it is not much of a complex issue. Hope someone will surely solve this. Thanks in advance.