【发布时间】:2016-01-03 23:03:14
【问题描述】:
我正在尝试使用 Azure 和 SendGrid 发送电子邮件。我已经全部设置好了(我认为),我的代码如下所示,但是“DeliverAsync()”方法不起作用,并且没有“Deliver()”选项可用。
这是我的使用语句:
using System;
using System.Web;
using System.Web.UI;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using SendGrid;
这是我的代码:“transportWeb.DeliverAsync(myMessage)”显示为纯黑色文本。
// Create the email object first, then add the properties.
var myMessage = new SendGridMessage();
myMessage.AddTo("d@gmail.com");
myMessage.From = new MailAddress("d@gmail.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";
var apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// create a Web transport, using API Key
var transportWeb = new Web(apiKey);
// Send the email, which returns an awaitable task.
transportWeb.DeliverAsync(myMessage);
我希望有人以前见过这个并且知道解决方案。网上有很多类似的问题,但我没有找到解决此问题的方法。我正在使用 SendGrid v6.3.4。我曾尝试恢复到 v6.3.3,但没有帮助。我在 SendGrid 中的统计数据显示所有内容为零,没有发送电子邮件、没有请求、没有退回等。
更新:
我已经尝试创建一个新的电子邮件类来消除任何混乱并使这一点更清晰,在 transportWeb 之后仍然无法识别“DeliverAsync”方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using SendGrid;
using System.Threading.Tasks;
namespace CPWebsite
{
public class Email
{
static async void Main()
{
try
{
// Create the email object first, then add the properties.
var myMessage = new SendGridMessage();
myMessage.AddTo("d@gmail.com");
myMessage.From = new MailAddress("d@gmail.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";
var apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// create a Web transport, using API Key
var transportWeb = new Web(apiKey);
// Send the email, which returns an awaitable task.
await transportWeb.DeliverAsync(myMessage);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
我也尝试将var myMessage = new SendGridMessage(); 更改为SendGridMessage myMessage = new SendGridMessage();,但没有成功。仅根据需要显示以下 using 语句。
using System;
using System.Net.Mail;
using SendGrid;
我现在正在尝试任何事情!
【问题讨论】:
-
我不知道 SendGrid,但我会先尝试调用同步方法,看看凭据和一切是否到位。首先同步,因为这会给你一个返回值,它有状态码和消息告诉你发生了什么。
-
这只是一个提醒,为了回复 Allen 上面关于同步方法的评论,SendGrid C# 库不再有同步 Deliver() 方法。
-
我可能遗漏了 Async 方法的 using 语句...?
-
好的,我通过在我的方法中添加“async”来等待工作,例如:“protected async void LogIn(object sender, EventArgs e)”但“DeliverAsync”仍显示为黑色文本并且没有被认出...
-
如果有人有更多想法,我已经更新了我的原始帖子?