【问题标题】:AWS lambda, C# async mail sending issue - MailJet APIAWS lambda,C# 异步邮件发送问题 - MailJet API
【发布时间】:2020-11-21 06:23:33
【问题描述】:

我最初使用 AWS SES 从我的 Lambda 发送邮件,但它的传递速度非常慢。

然后我决定改用 MailJet 并使用他们的 API 发送邮件。我正在使用 NuGet 包、API 的 V3.1 以及 MailJet 的示例代码来异步发送邮件。

    public async Task<bool> SendEmailAsync(EmailModel model)
    {            
        Boolean sent = false;

        MailjetClient client = new MailjetClient(Environment.GetEnvironmentVariable("EmailAPIKey"), Environment.GetEnvironmentVariable("EmailAPISecret"))
        {
            Version = ApiVersion.V3_1,
        };

        MailjetRequest request = new MailjetRequest
        {
            Resource = Send.Resource,
        }.Property(Send.Messages, new JArray {
             new JObject {
                 {"From", new JObject {
                         {"Email", Environment.GetEnvironmentVariable("SenderEmail")},
                         {"Name", Environment.GetEnvironmentVariable("SenderEmailName")}
                     }
                 },
                 {"HTMLPart", model.EmailHtmlBody},
                 {"Subject", model.EmailSubject},
                 {"TextPart", model.EmailTextBody},
                 {"To", new JArray {
                     new JObject {
                         {"Email", model.EmailTo},
                         {"Name", model.EmailTo}
                     }
                 }}
             }
            });

        try
        {
            MailjetResponse response = await client.PostAsync(request);

            if (response.IsSuccessStatusCode)
            {
                sent = true;
                LambdaLogger.Log(string.Format("Total: {0}, Count: {1}\n", response.GetTotal(), response.GetCount()));
                LambdaLogger.Log(response.GetData().ToString());
            }
            else
            {
                sent = false;
                LambdaLogger.Log(string.Format("StatusCode: {0}\n", response.StatusCode));
                LambdaLogger.Log(string.Format("ErrorInfo: {0}\n", response.GetErrorInfo()));
                LambdaLogger.Log(response.GetData().ToString());
                LambdaLogger.Log(string.Format("ErrorMessage: {0}\n", response.GetErrorMessage()));
            }
        }
        catch (Exception mailFail)
        {
            sent = false;
            LambdaLogger.Log(string.Format("Failed: {0}\n", mailFail.Message.ToString() + " : " + mailFail.InnerException.Message.ToString()));
        }
   
        return sent;
    }

当我在本地测试代码时,一切正常。

当我将 lambda 部署到 AWS 并调用发送邮件的方法时,发送邮件是完全随机的。我猜这是由于某种原因而失败的异步部分,我希望有人可以帮助我解决这个问题,因为现在我被困在这个问题上。

或者,如果有人可以告诉我如何让 Amazon SES 立即发送。

【问题讨论】:

  • 第二行代码好像有语法错误?除此之外,失败的具体细节是什么?不要“猜测它是异步部分”,除非有特定的观察结果表明这一点? MailJet 的回复是否成功,但从未收到电子邮件?显示的代码不会像the vendor's code 那样检查响应。也许有一个你没有注意到的错误?
  • 我现在更新了代码部分,以显示在本地工作的整个异步方法,但有时仅在 AWS 中运行时。我最好的猜测是我在 async / await 上做错了,因为 lambda 似乎只是继续运行,而不是等待 MailJet 的回复。
  • await关键字专门用于等待操作结果。 (虽然我当然不能保证正在使用的第 3 方库或其可靠性。)你怎么称呼SendEmailAsync?你在等待它的结果吗?你在等待调用它的方法的结果吗?等等。基本上,整个应用程序中是否维护了 async/await 结构?到目前为止的问题描述意味着您可能在某个地方调用了异步操作然后忘记了它。
  • 您的意见非常有帮助,它让我回去工作,您是对的,有一个地方我没有等待。现在一切似乎都运行顺利,即使使用 Amazon SES。如果您将您的输入作为答案,我会将其标记为正确的。

标签: amazon-web-services asynchronous .net-core aws-lambda mailjet


【解决方案1】:

来自问题:

完全随机

来自评论:

似乎 lambda 只是继续运行,不等待 MailJet 的回复

这听起来像是一个 async/await 问题,但可能不是您想的那样。请注意,您正在等待 MailJet 操作的结果:

MailjetResponse response = await client.PostAsync(request);

但这只是在这种方法中。这个方法当然是async

public async Task<bool> SendEmailAsync(EmailModel model)

当你调用this方法时,你await了吗?您调用它的方法也应该是async。你等着吗?基本上,你是"async all the way down"吗?

听起来好像在代码库的某个地方有一个异步操作被调用然后被遗忘。

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 2018-01-19
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    相关资源
    最近更新 更多