无法使用sendgrid api发送附件

时间:2019-02-08 09:34:42

标签: c# asp.net asp.net-core sendgrid

我有一个byte [],这是我的ASP.NET Core 2.1应用程序中下载的pdf文件。

我正在尝试将此附件作为sendgrid电子邮件的附件。

public async Task SendEmail(byte[] Attachment = null)
{
    var client = new SendGridClient(apiKey);

    var msg = new SendGridMessage();

    // I also set the To, Subject, body etc etc

    msg.AddAttachment("test.pdf",Convert.ToBase64String(Attachment) ,"application/pdf","inline");
    var response = await client.SendEmailAsync(msg);

}

我得到一个“ BadReqest”状态码。如果我删除AddAttachment行,则该消息将被接受。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

 using (var stream = new MemoryStream(Attachment))
    {
        msg.AddAttachment("test.pdf", stream);
        var response = await client.SendEmailAsync(msg);
    }