【问题标题】:SendGrid - Image not showing up in HTML emailSendGrid - 图像未显示在 HTML 电子邮件中
【发布时间】:2020-10-03 23:34:52
【问题描述】:

我正在使用 SendGrid v3 API 和 C# 库 (v7) 发送电子邮件。 在我的电子邮件中,我有一个 png 标题。标头嵌入如下:

<img src="cid:emailheader"/>

在 C# 代码中,我将图像作为具有相同 ContentId 的附件发送

var mail = new Mail(发件人、主题、收件人、内容);

var headerPath = HttpContext.Current.Server.MapPath("~/Resources/email-header.png");

var attachment = new SendGrid.Helpers.Mail.Attachment();
attachment.ContentId = "emailheader";
attachment.Content = Convert.ToBase64String(File.ReadAllBytes(headerPath));
attachment.Type = "image/png";
attachment.Filename = "email-header.png";
mail.AddAttachment(attachment);

var send = sg.client.mail.send.post(requestBody: mail.Get());

然而,当我打开电子邮件时,它说找不到来源,即使图像正确显示在附件中

【问题讨论】:

标签: c# sendgrid


【解决方案1】:

我不是 Sendgrid 的专家,但我在那里找到了 blog post 这建议直接在您的 html 中进行内联编码。这样你就不需要添加附件了。 (我经常用这个)

<img alt="My Image" src="data:image/jpeg;base64,/9j/4S/+RXhpZgAATU0AKgA...more encoding" />

也许这是一个适合你的解决方法。

作为第二种选择: 用于发送带有我正在使用的图片的电子邮件

System.Net.Mail

我在这里添加了一个带有链接资源的 AlternateView。

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, null, "text/html");
LinkedResource imageResource = new LinkedResource(Imagepath + "Monitoring.png", "image/png")
{
   ContentId = "1",
   TransferEncoding = System.Net.Mime.TransferEncoding.Base64
};
htmlView.LinkedResources.Add(imageResource);
message.AlternateViews.Add(htmlView);

html中的语法和你使用的一样

<img src="cid:1">

我希望这会有所帮助。 巴蒂

【讨论】:

    【解决方案2】:

    节点

     //imageData= "data:image/png;base64,ine793nfdsf......."
    
        imageb64 = imageData.replace('data:image/png;base64,' , ''); 
        //remove data:image/png;base64,            
    
        const msg = {
                    to: 'example@gmail.com',
                    from: 'test@gmail.com',
                    subject: "image attached",
                    html :'<img src="cid:myimagecid"/>',
                    attachments: [
                      {
                        filename: "imageattachment.png",
                        content: imageb64,
                        content_id: "myimagecid",
                      }
                    ]
                  };
    
              sgMail.send(msg);
    

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 2016-12-31
      相关资源
      最近更新 更多