Page view counter
HomeLearnVideosHow Do I: Create a Reusable Component for Sending Email to a Distribution List  

[How Do I:] Create a Reusable Component for Sending Email to a Distribution List

Please install Silverlight or click download to watch video locally.

In this video Chris Pels shows how to create a component that can be used on multiple web pages and web sites that sends emails to a list of recipients. First, the ASP.NET web site will be configured to send email using the in the web.config file. Then a class and several methods are created to read a list of recipients from a data source (DB, XML, etc.) and send an email message to each of the recipients using the System.Net.Mail classes. As part of this process exception handling is included. In addition, a user interface is created to allow the user to specify items such as the From address, Subject, add an attachment, etc.

Presented by Chris Pels

Duration: 35 minutes, 28 seconds

Date: 04 December 2008

Watch    Video   |   Download    Video   |   VB Code    C# Code

Video downloads: WMV  |  Zune  |  iPod  |  PSP  |  MPEG-4  |  3GP

Audio downloads: AAC  |  WMA  |  MPEG-3  |  MPEG-2

Comments : 17

Leave a Comment

rami_nassar : On December 04, 2008 8:05 PM said:

Good job Chris Pels

rami_nassar : On December 04, 2008 8:05 PM said:

Good job Chris Pels

jasbits : On December 05, 2008 12:58 AM said:

Thanks for a great tutorial Chris.

yousaid : On December 05, 2008 9:34 AM said:

Chris, YOU ROCK.

I was about to start woring on similar stuff for my CMS.

THANKS a bunch !!

javabeans : On December 05, 2008 12:53 PM said:

Chris, i must really commend your tutorials. I have benefitted intensely from them. I have been following your series on emailing in .netand i like the way you carefully explain. God bless you and I would appreciate a tutorial also on Instant messenging and chatting. Thanks a lot

ramy ramadan : On December 06, 2008 6:58 AM said:

Thanks Chris Pels

ronto1956 : On December 10, 2008 10:16 AM said:

chris you're the man..

faremax : On December 13, 2008 2:46 PM said:

I am getting "System.InvalidOperationException: An asynchronous call is already in progress. It must be completed or canceled before you can call this method." error. Did anybody else experience the same error?

Also, shouldn't SendMessage(mailMessage) call be outside of the For loop.  When I move it out I get no error message but email is only being sent to the last person in the table probably because of the mailMessage.To.Clear. When I remove To.Clear and move SendMessage(mailMessage) out it works but I get a list of addresses in the To field and I really don't want people on the dist list to see that. Is there a way to hide that list?  I want people to only see their own email address in the To field.

bill32 : On December 23, 2008 12:55 PM said:

I get the same exception, and am pretty sure this code is flawed.  Since the demo code swallows exceptions, he's not seeing it, but MS doesn't let you use SendAsync this way.

jwhanes : On February 11, 2009 12:04 AM said:

Yeah, pretty sure this code is bugged. The InvalidOperationException gets thrown when sending multiple emails. 'An asynchronous call is already in progress. It must be completed or canceled before you can call this method.' How can this be fixed?

bohunt : On February 13, 2009 5:43 PM said:

Thanks..

Dunfalach : On February 26, 2009 2:07 PM said:

I've just been researching this same thing (which is how I ended up here). According to the MSDN page http://msdn.microsoft.com/en-us/library/x5x13z6h(VS.80).aspx trying to use SendAsync as a way of rapidly cycling through multiple emails doesn't appear to be what they had in mind: "After calling SendAsync, you must wait for the e-mail transmission to complete before attempting to send another e-mail message using Send or SendAsync."

Basically, it looks like SendAsync is meant to allow you to send a single email and process code that comes after it, rather than to allow you to send the next without waiting for the first to complete. I did see one forum that said that if you created a new instance of smtpclient for each email, you could process faster. I can't currently confirm whether this works, however.

uwspstar : On February 27, 2009 5:39 PM said:

excellent job

poseidonredsea : On March 06, 2009 10:55 AM said:

Hello

Thanks for this great article . I followed the steps but I get an exception says :

ex = {"An asynchronous call is already in progress. It must be completed or canceled before you can call this method."}

I don't know why this happen . can someone help me with it

Regards

Tamer Kamal

poseidonredsea : On March 06, 2009 10:55 AM said:

Hello

Thanks for this great article . I followed the steps but I get an exception says :

ex = {"An asynchronous call is already in progress. It must be completed or canceled before you can call this method."}

I don't know why this happen . can someone help me with it

Regards

flybentley : On March 19, 2009 2:18 PM said:

Hi Chris Pels and everyone,

This code is good for a sample, but will never work in practice.

1) Because in the loop you are sending emails asynchronously. That is when you get to the next reciepient in the loop, you are not done with the previous. The prevois email is being sent and being used. That is why you cannot just clear the To property.

2) You cannot return the sentEmails property with a correct number of sent emails, because when you exit the method the emails are still being sent and the OnSendCompleted Eventhandler will continue to fire long after you have left the method.

I could propose, Chris, you fixed the issues and come out with a new video, if this is possible. Because there is a lot of wrong in this video, that will confuse the folks out there trying to get started with such an application.

Regards

ADPrevost : On July 02, 2009 3:30 PM said:

I agree with flybentley. I wasted a lot of time. Next time I'll read the posts first.

Leave a Comment

You must be logged in to leave a comment. Click here to log in.

Microsoft Communities