You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Mailjet发送HTML邮件时CID嵌入图片无法正常显示问题

解决Mailjet CID嵌入图片不显示的问题

问题分析

从你提供的邮件原始内容来看,邮件的MIME类型是multipart/mixed——这种类型通常用于承载独立附件,而内嵌图片需要使用multipart/related结构来关联HTML内容和图片资源,这样客户端才会将图片解析为HTML的一部分,而非独立附件。

解决步骤

1. 使用SDK专属的内嵌附件方法

Mailjet的C# SDK提供了专门处理内嵌资源的WithInlineAttachment方法,替换原有的WithAttachment调用即可:

var email = new TransactionalEmailBuilder()
    .WithFrom(new SendContact(senderEmail, senderName))
    .WithSubject("titre")
    .WithHtmlPart(body)
    .WithTo(new SendContact(destEmail))
    .WithInlineAttachment(attach) // 改用内嵌附件方法
    .Build();

2. 手动设置附件的Content-Disposition

如果WithInlineAttachment方法不可用(取决于SDK版本),可以显式将附件的ContentDisposition属性设为Inline:

var attach = new Attachment(imageName, "image/png", imageBase64, contentId)
{
    ContentDisposition = ContentDisposition.Inline
};

3. 升级SDK到最新版本

旧版本的Mailjet SDK可能存在MIME结构生成错误,升级到最新版本可以解决这类兼容性问题。

4. 补充检查客户端设置

部分邮件客户端(如Gmail、Outlook)默认会屏蔽内嵌图片,需要确保客户端允许显示图片。但你的情况是图片被识别为附件,因此核心问题还是MIME结构,这一步仅作为补充验证。

修改后的完整代码示例

using Mailjet.Client.Resources;
using Mailjet.Client.TransactionalEmails;
using Mailjet.Client;

using System;
using System.Linq;
using System.Threading.Tasks;

namespace TestSendEmailWithEmbeddedImage
{
    internal class Program
    {
        static string API_KEY = @"API_KEY_REDACTED";
        static string SecretKey = @"SECRET_KEY_REDACTED";

        static async Task Main(string[] args)
        {
            var senderEmail = "SENDER_EMAIL_REDACTED";
            var destEmail = "DEST_EMAIL_REDACTED";
            var senderName = "support";
        
            MailjetClient _client = new MailjetClient(API_KEY, SecretKey);
            var imageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAEklEQVR4nGP8z4APMOGVHbHSAEEsAROxCnMTAAAAAElFTkSuQmCC";
            var contentId = "ABCD";

            var imageName = "image.png";
            var body = $@"
                    <html>
                    <body>
                        <p>Voici une image :</p>
                        <img src=""cid:{contentId}"" alt=""image""/>
                    </body>
                    </html>";

            var attach = new Attachment(imageName, "image/png", imageBase64, contentId)
            {
                ContentDisposition = ContentDisposition.Inline
            };
       
            var email = new TransactionalEmailBuilder()
                .WithFrom(new SendContact(senderEmail, senderName))
                .WithSubject("titre")
                .WithHtmlPart(body)
                .WithTo(new SendContact(destEmail))
                .WithInlineAttachment(attach)
                .Build();

            var response = await _client.SendTransactionalEmailAsync(email);

            bool good = (response.Messages.First().Errors == null);
        }
    }
}

关键说明

  • multipart/related结构会将HTML内容和内嵌图片绑定,客户端会自动解析CID引用,将图片显示在HTML中而非作为独立附件。
  • 确保Content-ID在单封邮件中唯一,避免资源引用冲突。

内容的提问来源于stack exchange,提问作者Teckel Cruel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 23:22:32