【问题标题】:SendGrid 'DeliverAsync()' Not WorkingSendGrid 'DeliverAsync()' 不工作
【发布时间】: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”仍显示为黑色文本并且没有被认出...
  • 如果有人有更多想法,我已经更新了我的原始帖子?

标签: api azure sendgrid


【解决方案1】:

目前这是一个控制台应用程序吗?您需要等待该方法,否则控制台应用程序主线程将完成执行并导致工作线程在成功传递消息之前被杀死。

试试:

await transportWeb.DeliverAsync(myMessage);

还将以下内容添加到您的 using 语句中:

using System.Threading.Tasks;

【讨论】:

  • 嗨,它是一个 Asp.Net 网站应用程序。我试过 await 例如: await transportWeb.DeliverAsync(myMessage);但我收到一个错误消息“等待运算符只能在异步方法中使用”。
  • 好的,我通过在我的方法中添加“async”来等待工作,例如:“protected async void LogIn(object sender, EventArgs e)”但“DeliverAsync”仍显示为黑色文本并且不被认可......
  • “仍然显示为黑色文本且未被识别”是什么意思?
  • 我在 VS2015 中使用工具>选项>常规>蓝色主题。变量等通用代码为黑色字体,类为浅蓝色字体。 --- 我的声明应该显示为:await [Dark Blue Font] transportWeb[Black Font].DeliverAsync[Light Blue Font](myMessage)[Black Font]; --- 因为“DeliverAsync”是黑色的,所以我认为它没有被识别为一类。 --- 当我输入“transportWeb.”时,快速上下文菜单将 DeliverAsync 显示为“等待 System.Threading.Tasks.Task Web.DeliverAsync(ISendGrid message) Usage: await DeliverAsync(...);”
  • 当我输入“transportWeb.”时,其他选项都是对象:Equals、GetHashCode、GetType 或 ToString。
【解决方案2】:

我的项目(Windows 服务)相对于调用 SendGrid 的特定线程基本上是同步的,因此我必须做的是在 .Wait() 之后添加 .Wait() em>.DeliverAsync().

那么,试试吧:

static void Main()

及以后:

transportWeb.DeliverAsync(myMessage).Wait();

在 SendGrid 文档中实际上有一点脚注没有提到这种技术。

干杯。

【讨论】:

    【解决方案3】:

    我不知道这是否已经解决,但请尝试使用 myMessage.Html 而不是 myMessage.Text 作为正文。特别是如果您在正文中使用 html。我的设置几乎相同,而且我的代码运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      相关资源
      最近更新 更多