【问题标题】:How to attach a file as an attachment in email using SendGrid?如何使用 SendGrid 将文件作为附件添加到电子邮件中?
【发布时间】:2020-01-13 09:16:13
【问题描述】:

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

不良反应

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

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;

【问题讨论】:

  • 这里msg的类型是什么?
  • 如果我将鼠标悬停在味精上,它会显示味精 | {SendGrid.Helpers.Mail.SendGridMessage} - 这就是你所追求的吗?
  • 没有附件的邮件能否成功发送?
  • 是的,我可以发送没有附件的电子邮件。添加它不会发送的附件并给出错误。

标签: c# email sendgrid email-attachments sendgrid-api-v3


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多