views:

2347

answers:

6

I'm trying to write a notification service (for completely legit non-spam purposes) in .NET using SmtpClient. Initially I just looped through each message and sent it, however this is slow and I would like to improve the speed. So, I switched to using 'SendAsync', but now get the following error on the second call:

An asynchronous call is already in progress.

I read this to mean that MS crippled System.Net.Mail to prevent mass-mailers. Is this correct? If so, is there a better way to do this in .NET, and still be able to log the results of each email(which is important to our client). If not, why can SendAsync only be called once?

+1  A: 

Obviously, this is not an attempt to stop mass mailers.

The reason is that the SmtpClient class is not thread safe. If you want to send multiple emails simultaneously, you have to spawn a few worker threads (there are a few ways to do that in the .NET Framework) and create separate instances of SmtpClient in each of them.

DrJokepu
+11  A: 

According to the documentation:

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.

So to send multiple mails at the same time you need multiple SmtpClient instances.

Darin Dimitrov
A: 

Hello,

You may only send one at a time per SMTP client. If you wish to make more than one send call, create more than one SMTP client.

HTH,

Colby Africa

Colby Africa
+2  A: 

I think you misunderstand the XXXAsync class of methods. The purpose of these asynchronous calls is to allow the program to continue running, without the need of the method to finish processing and return first. You can then proceed with the result later by subscribe to something like XXXReceived event of the object.

To send more than one mail simultaneously, you may consider using more Threads.

m3rLinEz
Multiple threads calling the SendAsync is exactly what causes this exception
statichippo
@statichippo The OP didn't use async version of send mail in the first place. what I suggested is to use multiple threads with non-async call.
m3rLinEz
If you call Send (non-async) on multiple threads you will get an exception. The SmtpClient class is only capable of sending 1 message at a time regardless of how many threads are sending it. It's a protocol issue, not a C# issue really... You have to wait for one message to get sent before sending another -- regardless of whether you're using SendAsync or Send. Unless you want to create mutliple SmtpClients
statichippo
+1  A: 

As noticed by everyone else here, you can only send one email at a time, but the way to send another once the first has been sent is to handle the .SendCompleted event of the SmtpClient class, and then move on to the next email and send that.

If you want to send many emails simultaneously, then as the others have said, use multiple SmtpClient objects.

Lasse V. Karlsen
+1  A: 

You might be able to use the following:

ThreadPool.QueueUserWorkItem(state => client.Send(msg));

This should allow your messages to be queued and sent as threads become available.

kntcnrg