AWS lambda、C# 异步邮件发送问题 - MailJet API
AWS lambda, C# async mail sending issue - MailJet API
我最初使用 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 立即发送。
来自问题:
it is completely random
来自评论:
it seems like the lambda just keeps running and does not wait for the reply from MailJet
这听起来像是一个 async/await 问题,但可能不是您所想的那样。请注意,您正在正确地等待 MailJet 操作的结果:
MailjetResponse response = await client.PostAsync(request);
但这只是在这个方法中。这个方法当然是async
:
public async Task<bool> SendEmailAsync(EmailModel model)
当你调用这个方法的时候,你await
了吗?你调用它的方法,也应该是 async
。你等着吗?基本上,你 "async all the way down"?
听起来好像在代码库的某个地方有一个异步操作被调用然后被遗忘。
我最初使用 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 立即发送。
来自问题:
it is completely random
来自评论:
it seems like the lambda just keeps running and does not wait for the reply from MailJet
这听起来像是一个 async/await 问题,但可能不是您所想的那样。请注意,您正在正确地等待 MailJet 操作的结果:
MailjetResponse response = await client.PostAsync(request);
但这只是在这个方法中。这个方法当然是async
:
public async Task<bool> SendEmailAsync(EmailModel model)
当你调用这个方法的时候,你await
了吗?你调用它的方法,也应该是 async
。你等着吗?基本上,你 "async all the way down"?
听起来好像在代码库的某个地方有一个异步操作被调用然后被遗忘。