我尝试使用Azure和SendGrid发送电子邮件。我已经完成了所有设置(我认为),我的代码如下所示,但是' DeliverAsync()'方法不起作用,没有“交付”()'选项可用。
以下是我的使用陈述:
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中的统计数据显示所有内容都为零,没有发送电子邮件,没有请求,没有退回等。
更新
我尝试过创建一个新的电子邮件类来删除任何混乱,并使其更清晰,“DeliverAsync'在transportWeb之后仍然无法识别方法。
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 System;
using System.Net.Mail;
using SendGrid;
我正在尝试任何事情!
答案 0 :(得分:1)
这是目前的控制台应用吗?您需要等待该方法,否则控制台应用程序主线程将完成执行并导致工作线程在成功传递消息之前被杀死。
尝试:
await transportWeb.DeliverAsync(myMessage);
还要在使用语句中添加以下内容:
using System.Threading.Tasks;
答案 1 :(得分:0)
我的项目(Windows服务)与调用SendGrid的特定线程基本上是同步的,因此我必须做的就是在<<>后添加 .Wait() EM> .DeliverAsync()
所以,试试:
static void Main()
以后:
transportWeb.DeliverAsync(myMessage).Wait();
在SendGrid文档中实际上有一点注意到这种技术。
干杯。
答案 2 :(得分:-1)
我不知道这是否已经解决,但尝试使用myMessage.Html代替myMessage.Text。特别是如果你在身体中使用html。我有几乎相同的设置,我的代码工作正常。