What are the best practices for using SmtpClient, SendAsync, and Dispose in .NET 4.0

I am a little puzzled by how to manage SmtpClient now that it is available, especially if I make calls using SendAsync. Presumably, I should not call Dispose until SendAsync is executed. But should I ever call it (for example, using "use"). A script is a WCF service that periodically sends emails when making calls. Most of the calculations are fast, but sending emails can take a second or so, so Async would be preferable.

Should I create a new SmtpClient every time I send mail? Should I create it for the entire WCF? Help!

Refresh . If that matters, each email is always customized for the user. WCF is hosted on Azure, and Gmail is used as an email program.

+82
c # smtpclient
Sep 01 2018-11-11T00:
source share
5 answers

Note..NET 4.5 SmtpClient implements the async awaitable SendMailAsync . For lower versions, use SendAsync as described below.




You should always IDisposable instances of IDisposable as soon as possible. In the case of asynchronous calls, this occurs in the callback after sending the message.

 var message = new MailMessage("from", "to", "subject", "body")) var client = new SmtpClient("host"); client.SendCompleted += (s, e) => { client.Dispose(); message.Dispose(); }; client.SendAsync(message, null); 

A bit annoying SendAsync does not accept the callback.

+106
Sep 01 2018-11-21T00:
source share

An initial question was asked for .NET 4, but if it helps with .NET 4.5, SmtpClient implements the asynchronous expected SendMailAsync method.

As a result, to asynchronously send email:

 public async Task SendEmail(string toEmailAddress, string emailSubject, string emailMessage) { using (var message = new MailMessage()) { message.To.Add(toEmailAddress); message.Subject = emailSubject; message.Body = emailMessage; using (var smtpClient = new SmtpClient()) { await smtpClient.SendMailAsync(message); } } } 

Better to avoid using the SendAsync method.

+120
Mar 18 '14 at 6:06
source share

In general, IDisposable objects should be deleted as soon as possible; the implementation of IDisposable on the object is intended to transmit information that the class in question contains expensive resources that should be determined. However, if creating these resources is expensive and you need to build many of these objects, it might be better (in terms of performance) to store one instance in memory and reuse it. There is only one way to find out if this matters: profile it!

Re: disposing and Async: you cannot use using , obviously. Instead, you usually place the object in the SendCompleted event:

 var smtpClient = new SmtpClient(); smtpClient.SendCompleted += (s, e) => smtpClient.Dispose(); smtpClient.SendAsync(...); 
+12
Sep 01 '11 at 21:29
source share

OK, the old question I know. But I myself came across this when I needed something like that. I just wanted to share some code.

I repeat several SmtpClients to send multiple emails asynchronously. My solution is similar to TheCodeKing, but instead I delete the callback object. I also pass MailMessage as userToken to receive it in the SendCompleted event so that I can also call it. Like this:

 foreach (Customer customer in Customers) { SmtpClient smtpClient = new SmtpClient(); //SmtpClient configuration out of this scope MailMessage message = new MailMessage(); //MailMessage configuration out of this scope smtpClient.SendCompleted += (s, e) => { SmtpClient callbackClient = s as SmtpClient; MailMessage callbackMailMessage = e.UserState as MailMessage; callbackClient.Dispose(); callbackMailMessage.Dispose(); }; smtpClient.SendAsync(message, message); } 
+5
Jul 17 '14 at 21:39
source share

You can understand why it is especially important to get rid of SmtpClient with the following comment:

 public class SmtpClient : IDisposable // Summary: // Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, // and releases all resources used by the current instance of the System.Net.Mail.SmtpClient // class. public void Dispose(); 

In my script sending multiple emails using Gmail without disposing of the client, I used:

Message: the service is unavailable, the transmission channel is closing. Server response: 4.7.0 Temporary system problem. Try again later (WS). oo3sm17830090pdb.64 - gsmtp

+4
Aug 20 '14 at 7:04
source share



All Articles