Application freezes when waiting for API return code from SendGrid

Viewed 477

My application is basically an email client. It's generally works fine—there are no problems or errors. When it send an email to a recipient, they successfully receive the message. When that async task completes, however, the whole application is freezing.

A hotfix that I have used is to remove the await keyword from the API response. This works, but using this approach you can't get the status code determining if the message is sent successfully since you're not waiting for the response.

Here is the code:

using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;

namespace SendMailClass{
    internal class FunctionCall{

        #region Getting user info
        static bool EmailHasBeenSend = false;
        static string Documents = Environment.GetFolderPath(Environment.SpecialFolder
            .MyDocuments) + "\\Documents.txt";
        static string UserName = File.ReadLines(Documents).Skip(0).Take(1).First();
        static string UserEmail = File.ReadLines(Documents).Skip(1).Take(1).First();
        #endregion

        public static void SendFunction(string Recipient, string SubjectVariable,
            string CC, string BCC, String Messange,string FileName,
            string AttachmentPath)
        {
            Execute(Recipient, SubjectVariable ,CC,BCC, Messange ,FileName,
                AttachmentPath).Wait();
        }

        static async Task Execute(string Recipient, string SubjectVariable,
            string CC, string BCC, string Messange,string FileName,
            string AttachmentPath)
        {
            #region Email Values
            var apiKey = Environment.GetEnvironmentVariable("InMail_Api_Key");
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress(UserEmail, UserName);
            var subject = SubjectVariable == "" ? "(No subject)" : SubjectVariable;
            var to = new EmailAddress(Recipient);
            var plainTextContent = Messange == "" ? " " : Messange; ;
            var htmlContent = Messange;
            #endregion

            var Message = MailHelper.CreateSingleEmail(from, to, subject,
                plainTextContent, htmlContent);

            #region Attachment
            if (AttachmentPath != "") {
                var bytes = System.IO.File.ReadAllBytes(AttachmentPath);
                var File = Convert.ToBase64String(bytes);
                Message.AddAttachment(FileName, File);
            }
            #endregion

            /*
            #region CC/BCC
            if (CC != "") { 
                Message.AddCcs("anemail@example.com");
            }
            if (BCC != ""){
            Message.AddBccs("anemail@example.com")}
            #endregion
            */

            var response =await client.SendEmailAsync(Message);
            //var response =client.SendEmailAsync(Message); ---> the hotfix
        }
    }
}

After debugging the only results I get are from the debugger:

The thread --- has exited with code 0 (0x0).

And at the breakpoint:

The APIResponse is equal to NULL

It doesn't make any sense why it causes the application to freeze.

1 Answers

I have mention that my last hotfix was : APIResponse =client.SendEmailAsync(Message);

I found out that this is solving the problem:

APIResponse = await client.SendEmailAsync(Message).ConfigureAwait(false);

I want to test it a bit more before I am sure that this is the best solution. So far the responses are Accepted (if email is in correct form) and null if the connection isn't active (no internet).

Related