title: C# Asynchronous Tasks - MANDRILL API
body: <p>I am setting up the Mandrill Api for our internal systems.</p>
  
  <p>I can get the API working ok, and can receive the emails from it, the issue I have is when I try and get the result from the Send request, the system just hangs indefinitely.</p>
  
  <p>My code is as below:</p>
  
  <pre><code>public async Task&lt;bool&gt; SendEmail(MandrillSendTemplateRequest request)
  {
      var result = await _mandrill.SendMessageTemplate(Map(request));
      return result[0].Status.ToString() == "sent";
  }
  </code></pre>
  
  <p>If I run the above code WITHOUT the return, then the email sends fine.
  If I run the above code WITH the return, then the email sends fine, but my program just hangs.</p>
  
  <p>Can anyone shed any light on this issue?</p>
  
  <p><em>Edit. For reference, I am using the Mandrill .Net Wrapper By Shawn Mclean from NuGet Packages. <a href="https://www.nuget.org/packages/Mandrill/" rel="nofollow">Mandrill API 2.2.7</a></em></p>
  
  <p><em>Edit. Button Click Event that calls the SendEmail method:</em></p>
  
  <pre><code>  Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
  
      Try
          WaitCursor = True
  
          Dim mgr As New CustomerCommunicationsManager()
          Dim r As New MandrillSendTemplateRequest()
          Dim m As New MandrillMessage()
  
          m.FromEmail = "from@icloud.com"
          m.FromName = "fromName"
  
          m.Subject = "Test"
          m.ForeName = "Software"
          m.Surname = "Dept"
          m.EmailAddress = "software@icloud.com"
  
          r.Message = m
          r.TemplateName = "WelcomeBasic"
  
          Dim sent As Boolean = mgr.Mandrill.SendEmail(r).Result
  
      Catch ex As Exception
  
      Finally
          WaitCursor = False
      End Try
  
  
  End Sub
  </code></pre>
  
  <p><em>Edit. Code for Map(request) (SendEmail Method)</em></p>
  
  <pre><code>private SendMessageTemplateRequest Map(MandrillSendTemplateRequest obj)
  {
      if (obj == null) { return null; }
      return new SendMessageTemplateRequest(Map(obj.Message), obj.TemplateName, Map(obj.TemplateContent));
  }
  
  private EmailMessage Map(MandrillMessage obj)
  {
      if (obj == null) { return null; }
  
      return new EmailMessage
      {
          AutoHtml = obj.AutoHtml,
          AutoText = obj.AutoText,
          FromEmail = obj.FromEmail,
          FromName = obj.FromName,
          Important = obj.Important,
          PreserveRecipients = obj.PreserveRecipients,
          Subject = obj.Subject,
          Tags = obj.tags,
          To = new EmailAddress[] { new EmailAddress(obj.EmailAddress, obj.FullName) },
          TrackOpens = obj.TrackOpens,
          TrackClicks = obj.TrackClicks
      };
  }
  
  private IEnumerable&lt;TemplateContent&gt; Map(IEnumerable&lt;MandrillTemplateContent&gt; obj)
  {
      if (obj == null) { return null; }
  
      List&lt;TemplateContent&gt; content = new List&lt;TemplateContent&gt;();
  
      foreach (MandrillTemplateContent c in obj)
      {
          content.Add(new TemplateContent{ Content = c.Content, Name = c.Name });
      }
  
      return content;
  }
  </code></pre>