代码在发送电子邮件后挂起,但是电子邮件发送正常(静态异步任务)

问题描述 投票:0回答:1

我正在编写一些C#代码以发送电子邮件(通过Mailjet / Azure)。它确实发送电子邮件,但是由于某些原因,在逐步执行代码时,我从没走过这一行代码。...

MailjetResponse response = await client.PostAsync(request);

它只是挂在那一点上。知道为什么吗?再次,电子邮件发送正常!

  public static async Task<bool> SendEmailWithAttachment(string toAddress, string subject, string messageBody, bool sendBCCYesNo, bool sendFromInfoAddressYesNo, MemoryStream attachment = null, string attachmentFilename = null)
    {
        bool successYesNo = true;

        try
        {
            MailjetClient client = new MailjetClient("xxxxxx", "xxxxx")
            {
                Version = ApiVersion.V3_1,
            };
            MailjetRequest request = new MailjetRequest
                {
                    Resource = Send.Resource,
                }
                .Property(Send.Messages, new JArray {
                    new JObject {
                        {"From", new JObject {
                            {"Email", "[email protected]"},
                            {"Name", "xxxxx"}
                        }},
                        {"To", new JArray {
                            new JObject {
                                {"Email", toAddress},
                                {"Name", toAddress}
                            }
                        }},
                        {"Subject", subject},
                        {"TextPart", messageBody},
                        {"HTMLPart", messageBody}
                    }
                });
            MailjetResponse response = await client.PostAsync(request);
            if (response.IsSuccessStatusCode) // I never get to this point
            {
              :

我正在使用此调用代码。...

        if (Utility.SendEmailWithAttachment("[email protected]", "Test Email", "Test Body", false, false,
                po, "AAA.pdf").Result == false)
        {
            lblStatus.Text = "Email send failure. Please contact support.";
            return false;
        }

有趣的是,当我运行示例mailjet提供的代码时,我的电子邮件发送得很好,我确实在PostAsync之后到达了这一行。据我所知,唯一的主要区别是我使用Task返回布尔值,而不仅仅是Task。这是mailjet提供的代码,效果很好。...

    static void Main(string[] args)
    {
        RunAsync().Wait();
    }
    static async Task RunAsync()
    {
        MailjetClient client = new MailjetClient("xxxx", "xxxx")
        {
            Version = ApiVersion.V3_1,
        };
        MailjetRequest request = new MailjetRequest
            {
                Resource = Send.Resource,
            }
            .Property(Send.Messages, new JArray {
                new JObject {
                    {"From", new JObject {
                        {"Email", "[email protected]"},
                        {"Name", "xxxx"}
                    }},
                    {"To", new JArray {
                        new JObject {
                            {"Email", "[email protected]"},
                            {"Name", "xxxx"}
                        }
                    }},
                    {"Subject", "Your email flight plan!"},
                    {"TextPart", "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!"},
                    {"HTMLPart", "<h3>Dear passenger 1, welcome to <a href='https://www.mailjet.com/'>Mailjet</a>!</h3><br />May the delivery force be with you!"}
                }
            });
        MailjetResponse response = await client.PostAsync(request);
        if (response.IsSuccessStatusCode) // this line is reached!
        {

提前感谢!

c# multithreading mailjet
1个回答
0
投票

尝试下面的代码。一个好的经验法则是除非首先等待,否则不要使用* .Result。

if (await Utility.SendEmailWithAttachment("[email protected]", "Test Email", "Test Body", false, false,
            po, "AAA.pdf")== false)
{
    lblStatus.Text = "Email send failure. Please contact support.";
    return false;
}
© www.soinside.com 2019 - 2024. All rights reserved.