Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can SmtpClient.SendAsync only be called once?

Tags:

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?

like image 922
Daniel Avatar asked Dec 23 '08 18:12

Daniel


People also ask

What is SendAsync in c#?

SendAsync(HttpRequestMessage, HttpCompletionOption) Send an HTTP request as an asynchronous operation. SendAsync(HttpRequestMessage, CancellationToken) Send an HTTP request as an asynchronous operation.

What is SmtpClient C#?

Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.


1 Answers

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.

like image 148
Darin Dimitrov Avatar answered Oct 02 '22 13:10

Darin Dimitrov