Sendgrid-csharp: stuck on await client.SendEmailAsync(msg)

Created on 20 Mar 2017  ยท  4Comments  ยท  Source: sendgrid/sendgrid-csharp

Issue Summary

it seems that SendEmailAsync is not returning (stuck on await).

this is the code i am using:

static async Task Execute(string fromName, string fromEmail, string subject, string message, string text, IEnumerable<string> recipients, int? dealId, int? userId, int? dealType, string category = "", string eventType = "", List<ReplacementTag> replacementtags = null, bool unsubscribe = false, bool bccAdmin = false, bool disableAnalytics = false)
{
    string apiKey = ConfigurationManager.AppSettings["SendgridKey"];
    dynamic client = new SendGridClient(apiKey);

    var msg = new SendGridMessage();
    foreach (var recipient in recipients)
    {
        msg.AddTo(new EmailAddress(recipient));
    }
    if (replacementtags != null)
    {
        foreach (var tag in replacementtags)
        {
            foreach (var v in tag.Val)
            {
                LoggingProvider.LogAudit(tag.Key + " : " + v);
                msg.AddSubstitution(tag.Key, v);
            }
        }
    }

    msg.AddTo(new EmailAddress(ConfigurationManager.AppSettings["AdminEmail"]));
    msg.From = new EmailAddress(fromEmail, fromName);
    msg.ReplyTo = new EmailAddress(fromEmail, fromName);
    msg.Subject = subject;

    var categories = new List<string>();
    if (category != "")
    {
        msg.AddCategory(category);
    }
    else
    {
        msg.AddCategory("User " + userId.ToString());
        msg.AddCategory("Campaign " + dealId.ToString());
    }

    //Add the HTML and Text bodies

    msg.AddContent("text/plain", text);
    msg.AddContent("text/html", message);

    msg.AddCustomArg("orig_send_time", DateTime.Now.ToString());
    if (dealId != null)
    {
        msg.AddCustomArg("dealid", dealId.ToString());
    }
    if (userId != null)
    {
        msg.AddCustomArg("userid", userId.ToString());
    }
    if (dealType != null)
    {
        msg.AddCustomArg("dealtype", dealType.ToString());
    }
    if (eventType != "")
    {
        msg.AddCustomArg("eventtype", eventType);
    }

    var trackingSettings = new TrackingSettings();
    var clickTracking = new ClickTracking()
    {
        Enable = true,
        EnableText = false
    };
    trackingSettings.ClickTracking = clickTracking;
    var openTracking = new OpenTracking()
    {
        Enable = true
    };
    trackingSettings.OpenTracking = openTracking;
    var ganalytics = new Ganalytics()
    {
        Enable = false
    };
    trackingSettings.Ganalytics = ganalytics;
    msg.TrackingSettings = trackingSettings;
    LoggingProvider.LogAudit("email v9 before send: \n" + msg.Serialize());
    var response = await client.SendEmailAsync(msg);
    LoggingProvider.LogAudit("email v9 send " + subject + ": " + response.StatusCode);
}

public static void SendMailThread(string fromName, string fromEmail, string subject, string message, string text, List<string> recipients, int? dealId, int? userId, int? dealType, string category = "", string eventType = "", List<ReplacementTag> replacementtags = null, bool unsubscribe = false, bool bccAdmin = false, bool disableAnalytics = false)
{
    Execute(fromName, fromEmail, subject, message, text, recipients, dealId, userId, dealType, category, eventType, replacementtags, unsubscribe, bccAdmin, disableAnalytics).Wait();
}

the log message "email v9 before send" is printed but "email v9 send" is not
this is the output of the msg.serialize() :

{
    "from": {
        "name": "ืœื‘ืžื‘",
        "email": "[email protected]"
    },
    "subject": "admin:: Campaign Created : ืฉืœื™ื—ื” ื‘ืฉื ืื•ืคื˜ืื™ืŸ1",
    "personalizations": [
        {
            "to": [
                {
                    "email": "[email protected]"
                },
                {
                    "email": "[email protected]"
                }
            ],
            "custom_args": {
                "orig_send_time": "20/03/2017 15:16:39",
                "dealid": "0",
                "userid": "1179",
                "dealtype": "40"
            }
        }
    ],
    "content": [
        {
            "type": "text/plain",
            "value": "\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003ctitle\u003e\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\n\u003cdiv marginwidth=\u00220\u0022 marginheight=\u00220\u0022 style=\u0022padding: 20px 0;background-color:#f3f3f3;padding:70px;font-family:Helvetica Neue OTS;\u0022\u003e\n    \u003ctable width=\u0022100%\u0022 cellspacing=\u00220\u0022 style=\u0022background-color:#f3f3f3;font-family:Arial;\u0022\u003e\n        \u003ctr\u003e\n            \u003ctd valign=\u0022top\u0022 align=\u0022center\u0022\u003e\n                \u003ctable cellspacing=\u00220\u0022 cellpadding=\u00220\u0022 width=\u0022650\u0022 style=\u0022border:0px none #000000;margin-top:10px\u0022\u003e\n                    \u003ctr\u003e\n                        \u003ctd\u003e\n                            \u003ctable width=\u0022600\u0022 cellpadding=\u002220\u0022 cellspacing=\u00220\u0022\u003e\n                                \u003ctr\u003e\n                                    \u003ctd valign=\u0022top\u0022 align=\u0022left\u0022 style=\u0022background-color: #FFFFFF;border: 0 none #FFFFFF;color: #333333;font-family: Arial;font-size: 12px;line-height: 150%;padding: 25px 10px 90px;text-align: center;\u0022\u003e\n                                        \u003cdiv style=\u0022color: #666;font-family: TAHOMA;font-size: 20px;margin: 0 0 20px;padding: 20px 0px 30px;text-align: center;border-bottom:1px solid #ddd\u0022\u003e\n                                            ืœื‘ืžื‘\n                                        \u003c/div\u003e\n                                        \u003cdiv style=\u0022color: #666;direction: rtl;font-size: 22px;line-height: 32px;padding: 30px 40px 10px;text-align: center;\u0022\u003e\n                                            ืฉืœื™ื—ื” ื‘ืฉื ืื•ืคื˜ืื™ืŸ1\n                                        \u003c/div\u003e\n                                        \u003cbr\u003e\n\n                                        \u003cdiv style=\u0022font-family: Arial; margin: 30px 0px 60px;\u0022\u003e\n                                            \u003c!--\u003cimg alt=\u0022ืคืชื—ื• ืืช ื”ื”ื˜ื‘ื”\u0022 src=\u0022%DEAL_PICTURE%\u0022\u003e--\u003e\n                                            \u003ca href=\u0022https://dev.broadcust.co.il/deal/display/5968?cid=%customerid%\u0026src=email\u0022\u003e\n                                                \u003cimg alt=\u0022ืคืชื—ื• ืืช ื”ืงืžืคื™ื™ืŸ\u0022 src=\u0022%DEAL_PICTURE%\u0022\u003e\n                                            \u003c/a\u003e\n                                        \u003c/div\u003e\n\n                                        \u003c!-- Campaign Details --\u003e\n                                        \u003cdiv class=\u0022details\u0022 style=\u0022direction:rtl;line-height: 230%;width: 600px;padding: 20px;\u0022\u003e\n                                            \n                                        \u003c/div\u003e\n\n                                        \u003cdiv style=\u0022font-family:Arial;margin:70px 30px;\u0022\u003e\n                                            \u003ca href=\u0022https://dev.broadcust.co.il/deal/display/5968?cid=%customerid%\u0026src=email\u0022\u003e\n                                                \u003cimg src=\u0022http://2fa3e35729c1f93e1503-4bd9cbd7c940079c5eb5fd18e93586b2.r78.cf2.rackcdn.com/email-tpl-cta-button-blue.png\u0022\u003e\n                                            \u003c/a\u003e\n                                        \u003c/div\u003e\n                                    \u003c/td\u003e\n                                \u003c/tr\u003e\n                                \u003ctr\u003e\n                                    \u003ctd align=\u0022left\u0022 class=\u0022footerRow\u0022 style=\u0022text-align:center;background-color: #f3f3f3;border-top: 0px none #FFFFFF;padding: 20px 0;\u0022 valign=\u0022top\u0022\u003e\n                                        \u003cdiv class=\u0022footerText\u0022 style=\u0022position:relative;color: rgb(102, 102, 102); font-family: Arial; font-size: 10px; line-height: 150%;text-align:left;\u0022\u003e\n\n                                            \u003cdiv style=\u0022position: absolute;top:0px;left:00px;width:200pxbackground-color: #f3f3f3;text-align:left;\u0022\u003e\n                                                \u003cdiv style=\u0022direction: ltr;bottom:15px;opacity:0.9;\u0022\u003e\n                                                    Powered by \u0026nbsp;\n                                                    \u003ca href=\u0022https://beta.broadcust.co.il\u0022\u003e\n                                                        \u003cimg style=\u0022height: 15px;\u0022 src=\u0022http://2fa3e35729c1f93e1503-4bd9cbd7c940079c5eb5fd18e93586b2.r78.cf2.rackcdn.com/logo-bcust-email.png\u0022\u003e\n                                                    \u003c/a\u003e\n                                                \u003c/div\u003e\n\n                                                \u003cdiv style=\u0022direction: rtl;position:absolute;top: 20px;left:0;opacity:0.7;width: 200px;margin-top: 5px;\u0022\u003e\n                                                    ื ื•ืฆืจ ื‘ืืžืฆืขื•ืช\n                                                    \u003ca href=\u0022https://broadcust.co.il\u0022\u003e\n                                                        ื‘ืจื•ื“ืืงืืกื˜\n                                                    \u003c/a\u003e\n                                                    \u0026nbsp;-\u0026nbsp;\n                                                    ืฉื™ื•ื•ืง ืžืชืงื“ื ืœืขืกืงื™ื\n                                                \u003c/div\u003e\n                                            \u003c/div\u003e\n\n                                            \u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\n                                            \u003cdiv style=\u0022text-align:center;width:650px;direction:rtl\u0022\u003e\n                                            \u003cem\u003eCopyright ยฉ 2016 Broadcust All rights reserved.\u003c/em\u003e\n                                            \u003cbr\u003e\n                                            ืื ื• ืžืงื•ื•ื™ื ื›ื™ ืžืฆืืชื ื”ื•ื“ืขื” ื–ื• ื›ื‘ืขืœืช ืขืจืš ืขื‘ื•ืจื›ื. ืื ืื™ื ื›ื ืžืขื•ื ื™ื™ื ื™ื ืœืงื‘ืœ ืžืืชื ื•\n                                            ื”ื•ื“ืขื•ืช ื ื•ืกืคื•ืช ืขืชื™ื“.\n                                            \u003ca href=\u0022%UNSUBSCRIBE_LINK%\u0022\u003e\n                                ืื ื ืœื—ืฆื• ื›ืืŸ.\n                                            \u003c/a\u003e\n                                            \u003c/div\u003e\n                                        \u003c/div\u003e\n                                    \u003c/td\u003e\n\n                                \u003c/tr\u003e\n                            \u003c/table\u003e\n                        \u003c/td\u003e\n                    \u003c/tr\u003e\n                \u003c/table\u003e\n            \u003c/td\u003e\n        \u003c/tr\u003e\n    \u003c/table\u003e\n    \u003cspan style=\u0022padding:0px\u0022\u003e\u003c/span\u003e\n\u003c/div\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n\u003c/html\u003e`"
        },
        {
            "type": "text/html",
            "value": "\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003ctitle\u003e\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\n\u003cdiv marginwidth=\u00220\u0022 marginheight=\u00220\u0022 style=\u0022padding: 20px 0;background-color:#f3f3f3;padding:70px;font-family:Helvetica Neue OTS;\u0022\u003e\n    \u003ctable width=\u0022100%\u0022 cellspacing=\u00220\u0022 style=\u0022background-color:#f3f3f3;font-family:Arial;\u0022\u003e\n        \u003ctr\u003e\n            \u003ctd valign=\u0022top\u0022 align=\u0022center\u0022\u003e\n                \u003ctable cellspacing=\u00220\u0022 cellpadding=\u00220\u0022 width=\u0022650\u0022 style=\u0022border:0px none #000000;margin-top:10px\u0022\u003e\n                    \u003ctr\u003e\n                        \u003ctd\u003e\n                            \u003ctable width=\u0022600\u0022 cellpadding=\u002220\u0022 cellspacing=\u00220\u0022\u003e\n                                \u003ctr\u003e\n                                    \u003ctd valign=\u0022top\u0022 align=\u0022left\u0022 style=\u0022background-color: #FFFFFF;border: 0 none #FFFFFF;color: #333333;font-family: Arial;font-size: 12px;line-height: 150%;padding: 25px 10px 90px;text-align: center;\u0022\u003e\n                                        \u003cdiv style=\u0022color: #666;font-family: TAHOMA;font-size: 20px;margin: 0 0 20px;padding: 20px 0px 30px;text-align: center;border-bottom:1px solid #ddd\u0022\u003e\n                                            ืœื‘ืžื‘\n                                        \u003c/div\u003e\n                                        \u003cdiv style=\u0022color: #666;direction: rtl;font-size: 22px;line-height: 32px;padding: 30px 40px 10px;text-align: center;\u0022\u003e\n                                            ืฉืœื™ื—ื” ื‘ืฉื ืื•ืคื˜ืื™ืŸ1\n                                        \u003c/div\u003e\n                                        \u003cbr\u003e\n\n                                        \u003cdiv style=\u0022font-family: Arial; margin: 30px 0px 60px;\u0022\u003e\n                                            \u003c!--\u003cimg alt=\u0022ืคืชื—ื• ืืช ื”ื”ื˜ื‘ื”\u0022 src=\u0022\u0022\u003e--\u003e\n                                            \u003ca href=\u0022https://dev.broadcust.co.il/deal/display/5968?cid=%customerid%\u0026src=email\u0022\u003e\n                                                \u003cimg alt=\u0022ืคืชื—ื• ืืช ื”ืงืžืคื™ื™ืŸ\u0022 src=\u0022\u0022\u003e\n                                            \u003c/a\u003e\n                                        \u003c/div\u003e\n\n                                        \u003c!-- Campaign Details --\u003e\n                                        \u003cdiv class=\u0022details\u0022 style=\u0022direction:rtl;line-height: 230%;width: 600px;padding: 20px;\u0022\u003e\n                                            \n                                        \u003c/div\u003e\n\n                                        \u003cdiv style=\u0022font-family:Arial;margin:70px 30px;\u0022\u003e\n                                            \u003ca href=\u0022https://dev.broadcust.co.il/deal/display/5968?cid=%customerid%\u0026src=email\u0022\u003e\n                                                \u003cimg src=\u0022http://2fa3e35729c1f93e1503-4bd9cbd7c940079c5eb5fd18e93586b2.r78.cf2.rackcdn.com/email-tpl-cta-button-blue.png\u0022\u003e\n                                            \u003c/a\u003e\n                                        \u003c/div\u003e\n                                    \u003c/td\u003e\n                                \u003c/tr\u003e\n                                \u003ctr\u003e\n                                    \u003ctd align=\u0022left\u0022 class=\u0022footerRow\u0022 style=\u0022text-align:center;background-color: #f3f3f3;border-top: 0px none #FFFFFF;padding: 20px 0;\u0022 valign=\u0022top\u0022\u003e\n                                        \u003cdiv class=\u0022footerText\u0022 style=\u0022position:relative;color: rgb(102, 102, 102); font-family: Arial; font-size: 10px; line-height: 150%;text-align:left;\u0022\u003e\n\n                                            \u003cdiv style=\u0022position: absolute;top:0px;left:00px;width:200pxbackground-color: #f3f3f3;text-align:left;\u0022\u003e\n                                                \u003cdiv style=\u0022direction: ltr;bottom:15px;opacity:0.9;\u0022\u003e\n                                                    Powered by \u0026nbsp;\n                                                    \u003ca href=\u0022https://beta.broadcust.co.il\u0022\u003e\n                                                        \u003cimg style=\u0022height: 15px;\u0022 src=\u0022http://2fa3e35729c1f93e1503-4bd9cbd7c940079c5eb5fd18e93586b2.r78.cf2.rackcdn.com/logo-bcust-email.png\u0022\u003e\n                                                    \u003c/a\u003e\n                                                \u003c/div\u003e\n\n                                                \u003cdiv style=\u0022direction: rtl;position:absolute;top: 20px;left:0;opacity:0.7;width: 200px;margin-top: 5px;\u0022\u003e\n                                                    ื ื•ืฆืจ ื‘ืืžืฆืขื•ืช\n                                                    \u003ca href=\u0022https://broadcust.co.il\u0022\u003e\n                                                        ื‘ืจื•ื“ืืงืืกื˜\n                                                    \u003c/a\u003e\n                                                    \u0026nbsp;-\u0026nbsp;\n                                                    ืฉื™ื•ื•ืง ืžืชืงื“ื ืœืขืกืงื™ื\n                                                \u003c/div\u003e\n                                            \u003c/div\u003e\n\n                                            \u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\u003cbr\u003e\n                                            \u003cdiv style=\u0022text-align:center;width:650px;direction:rtl\u0022\u003e\n                                            \u003cem\u003eCopyright ยฉ 2016 Broadcust All rights reserved.\u003c/em\u003e\n                                            \u003cbr\u003e\n                                            ืื ื• ืžืงื•ื•ื™ื ื›ื™ ืžืฆืืชื ื”ื•ื“ืขื” ื–ื• ื›ื‘ืขืœืช ืขืจืš ืขื‘ื•ืจื›ื. ืื ืื™ื ื›ื ืžืขื•ื ื™ื™ื ื™ื ืœืงื‘ืœ ืžืืชื ื•\n                                            ื”ื•ื“ืขื•ืช ื ื•ืกืคื•ืช ืขืชื™ื“.\n                                            \u003ca href=\u0022https://dev.broadcust.co.il/business/1179/unsubscribe?token=%unsubscribe_token%\u0022\u003e\n                                ืื ื ืœื—ืฆื• ื›ืืŸ.\n                                            \u003c/a\u003e\n                                            \u003c/div\u003e\n                                        \u003c/div\u003e\n                                    \u003c/td\u003e\n\n                                \u003c/tr\u003e\n                            \u003c/table\u003e\n                        \u003c/td\u003e\n                    \u003c/tr\u003e\n                \u003c/table\u003e\n            \u003c/td\u003e\n        \u003c/tr\u003e\n    \u003c/table\u003e\n    \u003cspan style=\u0022padding:0px\u0022\u003e\u003c/span\u003e\n\u003c/div\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n\u003c/html\u003e`"
        }
    ],
    "categories": [
        "User 1179",
        "Campaign 0"
    ],
    "tracking_settings": {
        "click_tracking": {
            "enable": true,
            "enable_text": false
        },
        "open_tracking": {
            "enable": true
        },
        "ganalytics": {
            "enable": false
        }
    },
    "reply_to": {
        "name": "ืœื‘ืžื‘",
        "email": "[email protected]"
    }
}

Steps to Reproduce

just trying to send an email

Technical details:

  • .NET Version: 4.5.2
help wanted question

Most helpful comment

Changing

var response = await client.SendEmailAsync(msg);

to

var response = await client.SendEmailAsync(msg).ConfigureAwait(false);

resolved this issue for me.

All 4 comments

Hello @lebamb2,

I see a few issues:

  1. dynamic client = new SendGridClient(apiKey); should be var client = new SendGridClient(apiKey); -- no need to use dynamic any more
  2. msg.From --> msg.SetFrom
  3. msg.ReplyTo --> msg.SetReplyTo
  4. msg.Subject --> msg.SetSubject
  5. For setting Click Tracking: msg.SetClickTracking
  6. For setting Open Tracking: msg.SetOpenTracking
  7. For setting Google Analytics: msg.SetGoogleAnalytics

Please make these updates and let me know your result.

Thank you!

Changing

var response = await client.SendEmailAsync(msg);

to

var response = await client.SendEmailAsync(msg).ConfigureAwait(false);

resolved this issue for me.

@thinkingserious i made those changes and got the same result.
@bvirkler setting .ConfigureAwait(false) worked!!
can anyone explain why SendEmailAsync is not returning when ConfigureAwait is set to true?
thanks!!

Hi @lebamb2,

Thanks for the follow up! Here is the article it seems like everyone points to with regards to ConfigureAwait: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

@bvirkler,

Thanks for sharing the solution!

Was this page helpful?
0 / 5 - 0 ratings