Skip to main content
 首页 » 编程设计

c#之Exchange Web 服务之外部电子邮件客户端的内联图像未呈现

2024年01月13日33zhwl

我正在使用 EWS 接收/发送电子邮件。我在电子邮件中发送嵌入式图像以将用户导航到另一个页面。但是,当发送到 gmail 和 yahoo 等其他电子邮件客户端时,图像不会被设置为 html,而是文本。我在下面创建了一个控制台应用程序来演示:

static void Main(string[] args) 
    { 
        var service = getExchangeService(NetworkCreds.Credentials); 
 
        // Create the HTML body with the content identifier of the attachment. 
        string html = @"<html> 
                 <head> 
                 </head> 
                 <body> 
                    <img width=100 height=100 id=""1"" src=""cid:Party.jpg""> 
                 </body> 
                 </html>"; 
 
        // Create the email message. 
        EmailMessage email = new EmailMessage(service); 
        email.Subject = "Test Email"; 
        email.Body = new MessageBody(BodyType.HTML, html); 
        email.ToRecipients.Add("test@gmail.com"); 
 
        // Add the attachment to the local copy of the email message. 
        string file = @"C:\projects\Party.jpg"; 
        email.Attachments.AddFileAttachment("Party.jpg", file); 
        email.Attachments[0].IsInline = true; 
        email.Attachments[0].ContentId = "Party.jpg"; 
 
        // Save a copy of the email, add the attachment, and then send the email. This method results in three calls to EWS. 
        email.SendAndSaveCopy(); 
    } 

当我在 outlook 中收到邮件时,它工作正常,但在 gmail 中,它的正文中只有以下文本:[cid:Party.jpg] 以及下面的文件附件。我知道 Gmail 和其他客户端对图像进行了大量处理,这可能就是图像无法正确显示的原因。 我的问题是内联图像是否可以从交换网络服务发送到其他客户端并得到正确处理?

请您参考如下方法:

这就是解决此类问题的方法

bodyText = bodyText.Replace("cid:" + fileAttachment.ContentId, fileAttachment.Name); 

请务必将文件附加为“Image 001.png”或其他任何内容,然后您可以重新发送此电子邮件,其中包含图片 :)