SendGrid 未发送电子邮件?

SendGrid not delivering the email?

我已经围绕 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
    }

测试class我用的是看邮件是否发送:

[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

    }

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

我从来没有收到电子邮件是问题所在。我缺少什么才能真正发送电子邮件。

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

[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) 将测试更改为 return async Task 而不是 void

2) await 你的异步方法

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