> 在电子邮件中进行故障排除内联映像
>将图像直接嵌入电子邮件主体可增强可读性,但有时这些图像显示为红色XS。本指南提供了一个解决方案。
这是嵌入图像的校正代码示例:
><code class="language-csharp">MailMessage mailWithImg = GetMailWithImg(); MySMTPClient.Send(mailWithImg); // Ensure your SMTPClient is properly configured. private MailMessage GetMailWithImg() { MailMessage mail = new MailMessage(); mail.IsBodyHtml = true; mail.AlternateViews.Add(GetEmbeddedImage("c:/image.png")); mail.From = new MailAddress("yourAddress@yourDomain"); mail.To.Add("recipient@hisDomain"); mail.Subject = "yourSubject"; return mail; } private AlternateView GetEmbeddedImage(string filePath) { LinkedResource res = new LinkedResource(filePath); res.ContentId = Guid.NewGuid().ToString(); string htmlBody = $"<img src=\"cid:{res.ContentId}\"></img>"; // Note the escaped quotes AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html); alternateView.LinkedResources.Add(res); return alternateView; }</code>
此改进的代码使用AlternateViews
将图像嵌入HTML电子邮件中。 唯一的ContentId
分配给LinkedResource
>,在HTML的img
src
AlternateView
属性中引用。 "c:/image.png"
确保不同电子邮件客户端的兼容性。 这种方法可靠地嵌入图像,以防止红色X问题。 请记住,用实际的文件路径和电子邮件地址替换"yourAddress@yourDomain"
,"recipient@hisDomain"
和
以上是为什么我的内嵌电子邮件图像显示为红色 X,如何修复?的详细内容。更多信息请关注PHP中文网其他相关文章!