title: ASP.NET MailKit SMTP response
body: <p>I have this code which has been successfully sending emails - except for the other day when it did not. Therefore, I'd like to check the SMTP response, but not sure what to do.</p>
  
  <p>Here is my code now:</p>
  
  <pre><code>using (var client = new SmtpClient())
  {
    client.LocalDomain = "xxxxxxxxxxxx";
    await client.ConnectAsync("xxxxxxxxxxxx", xx, SecureSocketOptions.StartTls).ConfigureAwait(false);
    await client.AuthenticateAsync( new System.Net.NetworkCredential("xxxxxxxxxxxx", "xxxxxxxxxxxx")).ConfigureAwait(false);
    await client.SendAsync(emailMessage).ConfigureAwait(false);
    await client.DisconnectAsync(true).ConfigureAwait(false);
  }
  </code></pre>
  
  <p>So, I read in <a href="https://github.com/jstedfast/MailKit/issues/126" rel="nofollow noreferrer">here</a> the onMessageSent, or MessageSent functions can be used to see if there was a response - I'd really like to see an example of code though, how would those functions be used in code to determine if the message was really received?</p>
  
  <p>I do have the function which contains the async sending function as a <code>public void</code>, and the warning suppression quells the VisualStudio complaints about the call not being awaited.</p>
  
  <pre><code>public void SendEmail(string HtmlEmailContents, string SubjectLine, string CustomFromAddress = null, string CustomEmailRecipients = null)
  {
    string To = getRecipientString(mainRecipients);
    string Cc = getRecipientString(ccRecipients);
    string Bcc = getRecipientString(bccRecipients);
    if(CustomEmailRecipients != null &amp;&amp; CustomEmailRecipients != "")
    {
      To = CustomEmailRecipients;
      Cc = "";
      Bcc = "";
    }
    string finalizedFromAddress;
    if(CustomFromAddress != null)
    {
      finalizedFromAddress = CustomFromAddress;
    }
    else
    {
      finalizedFromAddress = FromAddress;
    }
    #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
    MessageServices.SendEmailAsync(
      To,
      finalizedFromAddress,
      Cc,
      Bcc,
      SubjectLine,
      HtmlEmailContents
    );
    #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  }
  </code></pre>
  
  <p>[New Edit]: So, let's imagine I straightened out the whole async thing, and now it's time to really catch those faulty messages. Here, I have read that the MessageSent and OnMessageSent functions can be used to see the results. And of course, I can't figure this out.
  I am looking <a href="https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendcompleted(v=vs.110).aspx" rel="nofollow noreferrer">here</a> for some examples which may be mirrored using the MailKit.
  In there, the line <code>client.SendCompleted += new         SendCompletedEventHandler(SendCompletedCallback);</code> seems to hold the key, and I wonder if in my code, using <code>client.MessageSent += ???</code> us the counterpart inside MailKit.</p>