如何使用 SendGrid 将文件作为附件附加到电子邮件中?

How to attach a file as an attachment in email using SendGrid?

我想使用 SendGrid 和 C# 将文件附加为附件 - 我有下面运行的代码,但返回的 response.StatusCode

BadResponse

如何更改此代码以便附加文件并成功发送电子邮件?

var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var listAtta = new List<FileAttachment>();
emailProducts.Select(o => o.tp).ToList().ForEach(o =>
{
    string file = o.ProductPdf;
    var fileBytes = FileToByteArray(o.ProductPdf);
    if (fileBytes != null && fileBytes.Count() > 0)
    {
        listAtta.Add(new FileAttachment
        {
            FileData = fileBytes,
            FileName = o.ProductPdf
        }); ;
    }
    msg.AddAttachment(o.ProductPdf, fileBytes.ToString());
});

var response = await client.SendEmailAsync(msg);
var success = response.StatusCode;

你需要对其进行base64编码 msg.AddAttachment(o.ProductPdf, Convert.ToBase64String(bytes) )