【发布时间】:2018-11-08 21:04:52
【问题描述】:
我正在尝试从 sendgrid 发送电子邮件,如下所示-
public static async Task<int> SendEmail(string fromEmail, string toEmail, string emailMessage, string subject)
{
try
{
// Create the email object first, then add the properties.
var myMessage = new SendGridMessage();
// Add the message properties.
myMessage.AddTo(toEmail);
myMessage.From = new MailAddress(fromEmail);
myMessage.Subject = subject;
myMessage.Html = emailMessage;
var username = ConfigurationManager.AppSettings["NetworkUserId"];
var pswd = ConfigurationManager.AppSettings["NetworkUserPwd"];
var domain = ConfigurationManager.AppSettings["HostName"];
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential(username, pswd, domain);
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
if (transportWeb != null)
await transportWeb.DeliverAsync(myMessage);
else
{
await Task.FromResult(0);
}
}
catch (System.Exception ex)
{
//throw ex;
}
return 1;
}
这里我使用 async 和 await,我成功收到了邮件,但我期望返回值为 1。我认为我的调试器会进入 await。
【问题讨论】:
-
没有办法(如果使用正确的话)这段代码不应该返回
1。如果您没有获得该值,那么我猜您正在发生某种死锁情况。虽然这只是一个猜测,但没有看到调用代码。如果您更清楚这里到底发生了什么,这会有所帮助 -
实际上,我在“await transportWeb.DeliverAsync(myMessage);”处丢失了调试器并没有找到我调用此方法的任何响应。我认为 await 在这里不起作用。
-
我猜你遇到了某种死锁情况。虽然这只是一个猜测,但没有看到调用代码
标签: c# asp.net async-await sendgrid