メール内のインライン画像のトラブルシューティング
メール本文に画像を直接埋め込むと読みやすくなりますが、これらの画像が赤い X として表示される場合があります。このガイドでは解決策を提供します。
画像を埋め込むための修正されたコード例を次に示します。
<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
は、さまざまな電子メール クライアント間での互換性を保証します。 このアプローチでは画像が確実に埋め込まれ、赤い X の問題が回避されます。 "c:/image.png"
、"yourAddress@yourDomain"
、"recipient@hisDomain"
を実際のファイル パスとメール アドレスに置き換えてください。
以上がなぜ私のインラインの電子メール画像が赤XSとして表示されているのですか、そしてどうすればそれを修正できますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。