【问题标题】:Can't send attachment with sendgrid api无法使用 sendgrid api 发送附件
【发布时间】:2019-02-08 09:34:42
【问题描述】:

我有一个 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 行,则该消息被接受。

我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    试试这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-05
      • 2014-05-02
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      相关资源
      最近更新 更多