mailjet / mailjet-apiv3-dotnet

[API v3] Official Mailjet API v3 .NET wrapper
https://dev.mailjet.com
MIT License
75 stars 31 forks source link

Postasync is not working in windows forms. #51

Closed kavithakolishetty closed 4 years ago

kavithakolishetty commented 4 years ago

Below method is getting into deadlock. I'm not able to use postasync in windows form button click.

please help me to resolve this.

I have also tried without await, but still the same.

private void btnSendEmail_Click(object sender, EventArgs e) { RunAsync(txtTo.Text, txtFrom.Text, txtSubject.Text, txtBody.Text).Wait(); }

static async Task RunAsync(string To,string From, string Subject,string Body) { MailjetClient client = new MailjetClient("95f23726a201bc9f9034dfd921ffb04e", "7d99b5e37c0b823d5780aa0aba74c6ec") { Version = ApiVersion.V3_1, }; MailjetRequest request = new MailjetRequest { Resource = Send.Resource, } .Property(Send.Messages, new JArray { new JObject { {"From", new JObject { {"Email",From}, {"Name", "Me"} }}, {"To", new JArray { new JObject { {"Email", To}, {"Name", "You"} } }}, {"Subject", Subject}, {"TextPart", "Greetings from DigiCorro"}, {"HTMLPart", Body} } }); try {

            MailjetResponse response = await client.PostAsync(request);

            if (!response.IsSuccessStatusCode)
            {
              //  return string.Empty;
            }               

        }
        catch (Exception ex)
        {

        }           
    }
sdiakovskyi-gd commented 4 years ago

@kavithakolishetty Hi, you shouldn't use blocking calls like Wait form UIthread in WinForms. Better to use Dispatcher to move to the work into the background thread, or at least make the button click handler async:


private async void btnSendEmail_Click(object sender, EventArgs e)
{
     await RunAsync(txtTo.Text, txtFrom.Text, txtSubject.Text, txtBody.Text);
}
sdiakovskyi-gd commented 4 years ago

I've added a comment to the readme to omit such issues.