2

What it says on the tin.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
James
  • 80,725
  • 18
  • 167
  • 237

2 Answers2

7

According to the MSDN page on SmtpClient, the only purpose for SendAsync is to allow your current thread to continue processing instead of waiting for the transmission to process. The purpose of SendAsync isn't to allow you to send multiple messages at once, it's to allow you to continue processing while it sends the message. SendAsync and Send are both using the same pipeline, SendAsync just allows you to do other things while the message is sent.

Alexander Kahoun
  • 2,458
  • 24
  • 36
  • So what would be the best workaround if you want to send multiple emails in the one go and still catch the callback? Creating multiple instances of SmtpClient? – James Nov 20 '09 at 21:29
  • Decided to change the accepted answer to this one. The other answers answer the *underlying* issue I had, however, this answer actually addresses the initial question. – James Mar 24 '14 at 10:28
3

According to MSDN:

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 you could reuse the same instance but you must wait for the first mail to be sent.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yes but doesn't that defeat the whole purpose of SendAsync? You may aswell call Send. What I am getting at is shouldn't SendAsnyc queue each message and send when the resources are available? – James Nov 20 '09 at 21:18
  • If you want to send emails in parallel you need different instances. Probably that's due to the way `SmtpClient` class is designed. – Darin Dimitrov Nov 20 '09 at 21:20
  • 2
    ...all you have done is restated the question and agreed that it is factual. – David Pfeffer Nov 20 '09 at 21:22
  • I think my issue is really with how SmtpClient is designed. As if technically if you have 1000's of emails to send, that means you need to create 1000 SmtpClient's that are pretty much going to have the same configuration...seems a tad wasteful to me. – James Nov 20 '09 at 21:23
  • You could send the next message from the completed callback. That way you avoid locking up your program (or spawning millions of clients). – Matt Nov 20 '09 at 21:24
  • 3
    @James: No, Async means that your main thread is free to do something else, like preparing the next mail. It does not imply that SendAsync is re-entrant. – H H Nov 20 '09 at 21:27
  • I think that's an intentional design limiting to you to think about sending emails. Not just fire up the SmtpClient and locks up the response stream while you're at it but *think* about really doing the Async stuff right. – chakrit Nov 20 '09 at 21:30
  • Right so say I need to send out loads of emails at any one time AND catch the callback of each email. Is the only plausible way to create multiple instances of SmtpClient? – James Nov 20 '09 at 21:31
  • @James, you could also write your custom class implementing the SMTP protocol class that can send many emails, but I think that creating multiple instances wouldn't be so much of a problem. – Darin Dimitrov Nov 20 '09 at 21:33