<p>I may be wrong, but if you are working with SmtpClient.SendAsync in ASP.NET 
2.0 and it throws an exception, the thread processing the request waits 
indefinitely for the operation to complete.</p>

<p>To reproduce this problem, simply use an invalid SMTP address for the host 
that could not be resolved when sending an email.</p>

<p>Note that you should set Page.Async = true to use SendAsync.</p>

<p>If Page.Async is set to false and Send throws an exception the thread 
does not block, and the page is processed correctly.</p>

<p>TIA.</p>

[{'answer_id': 80887, 'author': 'bzlm', 'author_id': 7724, 'author_profile': 'https://Stackoverflow.com/users/7724', 'pm_score': 2, 'selected': False, 'text': '<p><strike></p>\n\n<blockquote>\n  <p>Note that you should set Page.Async = true to use SendAsync.</p>\n</blockquote>\n\n<p>Please explain the rationale behind this. Misunderstanding what Page.Async does may be the cause of your problems.</strike></p>\n\n<p>Sorry, I was unable to get an example working that reproduced the problem.</p>\n\n<p>See <a href="http://msdn.microsoft.com/en-us/magazine/cc163725.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/cc163725.aspx</a> (WICKED CODE: Asynchronous Pages in ASP.NET 2.0)</p>\n\n<p><strong>EDIT:</strong> Looking at your code example, I can see that you\'re not using <code>RegisterAsyncTask()</code> and the <code>PageAsyncTask</code> class. I think you must do this when executing asynchronous tasks on a Page where <code>@Async</code> is set to true. The example from MSDN Magazine looks like this:</p>\n\n<pre><code>protected void Page_Load(object sender, EventArgs e)\n{\n    PageAsyncTask task = new PageAsyncTask(\n        new BeginEventHandler(BeginAsyncOperation),\n        new EndEventHandler(EndAsyncOperation),\n        new EndEventHandler(TimeoutAsyncOperation),\n        null\n        );\n    RegisterAsyncTask(task);\n}\n</code></pre>\n\n<p>Inside <code>BeginAsyncOperation</code>, then, should you send a mail asynchronously.</p>\n'}, {'answer_id': 83592, 'author': 'Olivier MATROT', 'author_id': 15186, 'author_profile': 'https://Stackoverflow.com/users/15186', 'pm_score': 0, 'selected': False, 'text': '<p>Here is mine. Give it a try.</p>\n\n<pre><code>public partial class _Default : System.Web.UI.Page\n{\n    protected void Page_Load(object sender, EventArgs e)\n    {\n        // Using an incorrect SMTP server\n        SmtpClient client = new SmtpClient(@"smtp.nowhere.private");\n        // Specify the e-mail sender.\n        // Create a mailing address that includes a UTF8 character\n        // in the display name.\n        MailAddress from = new MailAddress("someone@somewhere.com",\n           "SOMEONE" + (char)0xD8 + " SOMEWHERE",\n        System.Text.Encoding.UTF8);\n        // Set destinations for the e-mail message.\n        MailAddress to = new MailAddress("someone@somewhere.com");\n        // Specify the message content.\n        MailMessage message = new MailMessage(from, to);\n        message.Body = "This is a test e-mail message sent by an application. ";\n        // Include some non-ASCII characters in body and subject.\n        string someArrows = new string(new char[] { \'\\u2190\', \'\\u2191\', \'\\u2192\', \'\\u2193\' });\n        message.Body += Environment.NewLine + someArrows;\n        message.BodyEncoding = System.Text.Encoding.UTF8;\n        message.Subject = "test message 1" + someArrows;\n        message.SubjectEncoding = System.Text.Encoding.UTF8;\n        // Set the method that is called back when the send operation ends.\n        client.SendCompleted += new\n        SendCompletedEventHandler(SendCompletedCallback);\n        // The userState can be any object that allows your callback \n        // method to identify this send operation.\n        // For this example, the userToken is a string constant.\n        string userState = "test message1";\n        try\n        {\n            client.SendAsync(message, userState);\n        }\n        catch (System.Net.Mail.SmtpException ex)\n        {\n            Response.Write(string.Format("Send Error [{0}].", ex.InnerException.Message));\n        }\n        finally\n        {\n        }\n    }\n    private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)\n    {\n        // Get the unique identifier for this asynchronous operation.\n        String token = (string)e.UserState;\n\n        if (e.Cancelled)\n        {\n            Response.Write(string.Format("[{0}] Send canceled.", token));\n        }\n        if (e.Error != null)\n        {\n            Response.Write(string.Format("[{0}] {1}", token, e.Error.ToString()));\n        }\n        else\n        {\n            Response.Write("Message sent.");\n        }\n    }\n\n}\n</code></pre>\n'}, {'answer_id': 201596, 'author': 'Olivier MATROT', 'author_id': 15186, 'author_profile': 'https://Stackoverflow.com/users/15186', 'pm_score': 1, 'selected': False, 'text': '<p>RegisterAsyncTask could not be used.\nLook at the BeginEventHandler delegate:</p>\n\n<p>public delegate IAsyncResult BeginEventHandler(\n    Object sender,\n    EventArgs e,\n    AsyncCallback cb,\n    Object extraData\n)</p>\n\n<p>It should return an IAsyncResult.\nNow look at the SmtpClient.SendAsync function :</p>\n\n<p>public void SendAsync(\n    MailMessage message,\n    Object userToken\n)</p>\n\n<p>There is no return value.</p>\n\n<p>Anyway this is working fine, as long as SmtpClient.SendAsync does not throw an exception.</p>\n'}]
['https://Stackoverflow.com/questions/80653', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15186/']