Why does SmtpClient.SendAsync hang?
<p>I am using <code>SmtpClient.SendAsync</code> (C#) to send emails in ASP.NET web applications and services. The web application/service runs on Windows Server 2012 R2 using IIS 8.</p>

<p>There are times where the call to <code>SendAsync</code> hangs up and does not appear to send the mail message asynchronously, so the calling thread blocks. This behavior seems to be sporadic. I cannot replicate the problem in a testing environment. This is especially problematic when sending the email as a result of a call to a web method because the timeout is 60 seconds (I'm using <code>SendAsync</code> for this very reason so the client doesn't experience any time delay).</p>

<p>Here is my code snippet. </p>

<pre><code>SmtpClient client;
MailMessage msg;

public void SendMail()
{
    try
    {
        client = new SmtpClient("smtpAddress@mydomain.com");
        msg = new MailMessage();
        msg.Subject = "Test";
        msg.Body = "This is the body";
        msg.From = "noreply@example.com";
        msg.To.Add("me@example.com");

        client.SendCompleted += new SendCompletedEventHandler(sendCompletedCallback);
        client.SendAsync(msg, "Test");
    }
    catch (Exception ex)
    {
        // Log error
    }
}

private void sendCompletedCallback(object send, AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        // Log error
    }
    else if (e.Cancelled)
    {
        // Log cancellation
    }
    else
    {
        // Log success
    }

    client.Dispose();
    msg.Dispose();
}
</code></pre>

<p>Why does the call to <code>SendAsync</code> hang and block the calling thread at times?</p>