{"id":6426,"date":"2014-04-18T05:21:31","date_gmt":"2014-04-18T05:21:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/smtpclient-sendasync-blocking-my-asp-net-mvc-request-collection-of-common-programming-errors\/"},"modified":"2014-04-18T05:21:31","modified_gmt":"2014-04-18T05:21:31","slug":"smtpclient-sendasync-blocking-my-asp-net-mvc-request-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/18\/smtpclient-sendasync-blocking-my-asp-net-mvc-request-collection-of-common-programming-errors\/","title":{"rendered":"SmtpClient.SendAsync blocking my ASP.NET MVC Request-Collection of common programming errors"},"content":{"rendered":"<p>I have a Action that sends a simple email:<\/p>\n<pre><code>    [HttpPost, ActionName(\"Index\")]\n    public ActionResult IndexPost(ContactForm contactForm)\n    {\n        if (ModelState.IsValid)\n        {\n            new EmailService().SendAsync(contactForm.Email, contactForm.Name, contactForm.Subject, contactForm.Body, true);\n\n            return RedirectToAction(MVC.Contact.Success());\n        }\n        return View(contactForm);\n    }\n<\/code><\/pre>\n<p>And a email service:<\/p>\n<pre><code>    public void SendAsync(string fromEmail, string fromName, string subject, string body, bool isBodyHtml)\n    {\n        MailMessage mailMessage....\n        ....\n        SmtpClient client = new SmtpClient(settingRepository.SmtpAddress, settingRepository.SmtpPort);\n\n        client.EnableSsl = settingRepository.SmtpSsl;\n        client.Credentials = new NetworkCredential(settingRepository.SmtpUserName, settingRepository.SmtpPassword);\n        client.SendCompleted += client_SendCompleted;\n        client.SendAsync(mailMessage, Tuple.Create(client, mailMessage));\n    }\n\n    private void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)\n    {\n        Tuple data = (Tuple)e.UserState;\n        data.Item1.Dispose();\n        data.Item2.Dispose();\n\n        if (e.Error != null)\n        {\n\n        }\n    }\n<\/code><\/pre>\n<p>When I send a email, I am using Async method, then my method SendAsync return immediately, then RedirectToAction is called. But the response(in this case a redirect) isn\u00b4t sent by ASP.NET until client_SendCompleted is completed.<\/p>\n<p>Here&#8217;s what I&#8217;m trying to understand:<\/p>\n<p>When watching the execution in Visual Studio debugger, the SendAsync returns immediately (and RedirectToAction is called), but nothing happens in the browser until email is sent?<\/p>\n<p>If i put a breakpoint inside client_SendCompleted, the client stay at loading&#8230;. until I hit F5 at debugger.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a Action that sends a simple email: [HttpPost, ActionName(&#8220;Index&#8221;)] public ActionResult IndexPost(ContactForm contactForm) { if (ModelState.IsValid) { new EmailService().SendAsync(contactForm.Email, contactForm.Name, contactForm.Subject, contactForm.Body, true); return RedirectToAction(MVC.Contact.Success()); } return View(contactForm); } And a email service: public void SendAsync(string fromEmail, string fromName, string subject, string body, bool isBodyHtml) { MailMessage mailMessage&#8230;. &#8230;. SmtpClient client = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6426","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6426"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6426\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}