【问题标题】:SendGrid not delivering the email?SendGrid 不发送电子邮件?
【发布时间】:2016-04-04 19:26:36
【问题描述】:

我已经围绕 sendgrid 编写了一个包装服务,除了实际发送电子邮件之外,所有部分都可以工作。

服务:

public class SendGridService : ISendGridService
    {
        public async Task Send(Email email)
        {
            var preparedEmail = PrepareEmail(email);
            var apiKey = ConfigurationManager.AppSettings["sendGridApiKey"];

            var transportWeb = new Web(apiKey);
            await transportWeb.DeliverAsync(preparedEmail);

        }

        //other methods that prepare the email
    }

我用来查看邮件是否发送的测试类:

[Test]
    public void Send_ShouldSendEmailToOneAddress()
    {
        //arrange
        //uses NBuilder to mock the object
        var email = Builder<Email>.CreateNew()
            .With(x => x.Recipient = "me@me.com")
            .With(x => x.Sender = "me@me.com")
            .With(x => x.SenderName = "me")
            .With(x => x.FilePathAttachement = null)
            .With(x => x.Html = null)
            .Build();

        //act
        var temp = _sut.Send(email);


        //assert

    }

我意识到测试并没有真正测试任何东西,但我希望在我的收件箱中看到电子邮件,然后围绕代码编写真正的错误测试。

我从来没有收到过电子邮件是问题所在。让电子邮件实际发送我缺少什么。

【问题讨论】:

  • 如果您登录到发送网格,发送网格是否正在接收/处理任何内容?
  • 你的 DeliverAsync 方法是什么样的?
  • @cal5barton 不,就 SendGrid 而言,什么都没有发生
  • @X3074861X这不是我的方法。它带有 SendGrid API
  • 您是否从 sendgrid 获得成功的 api 调用?

标签: c# sendgrid


【解决方案1】:

您没有正确调用异步方法。在单元测试的上下文中,它应该是:

[Test]
public async Task Send_ShouldSendEmailToOneAddress()
{
    //arrange
    //uses NBuilder to mock the object
    var email = Builder<Email>.CreateNew()
        .With(x => x.Recipient = "me@me.com")
        .With(x => x.Sender = "me@me.com")
        .With(x => x.SenderName = "me")
        .With(x => x.FilePathAttachement = null)
        .With(x => x.Html = null)
        .Build();

    //act
    await _sut.Send(email);


    //assert

}

即:

1) 将您的测试更改为返回 async Task 而不是 void

2) await 你的异步方法

当您在程序中使用您的邮件发件人时,您需要确保您使用的async/await'all the way down'

【讨论】:

  • 这给了我一个编译器错误:无法将 void 分配给一个隐式类型的变量,我将测试下的系统分配给 temp。
  • @Robert 修正.. 只需删除 temp(您的 SendGridService 不会返回任何内容)
猜你喜欢
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 2018-11-08
  • 1970-01-01
相关资源
最近更新 更多