.NET/C# 3.5 では、画像が埋め込まれた電子メールを作成し、 smtp4dev (http://smtp4dev.codeplex.com/)で電子メールを受信しますが、画像は表示されません。 HTML Agility Pack を使用して HTML ドキュメントを解析し、クリーニングを行います (この部分は問題なく動作します)。
//uninteresting code above
var images = doc.DocumentNode.Descendants("img");
List<MemoryStream> listOfStreams = new List<MemoryStream>();
List<string> listOfCid = new List<string>();
List<string> listOfReplacedSrcValues = new List<string>();
foreach (var img in images)
{
var src = img.Attributes["src"];
if (src != null && src.Value.StartsWith("http://"))
{
listOfReplacedSrcValues.Add(src.Value);
//I build a string that looks like this inv_brandLogo_fr_gif
string cid = src.Value.Substring(src.Value.LastIndexOf('/'), src.Value.Length - src.Value.LastIndexOf('/')).Trim('/').Replace('.', '_');
string cidWithTimestamp = string.Format("cid:{0}_{1}", cid, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fffffff")); //append a timestamp to ensure the cid is unique !
PageResult = PageResult.Replace(src.Value, cidWithTimestamp);
listOfCid.Add(cidWithTimestamp);
WebClient wc = new WebClient();
byte[] originalData = wc.DownloadData(src.Value);
MemoryStream ms = new MemoryStream(originalData);
listOfStreams.Add(ms);
}
}
MailAlert.SendRecap(Context.User.Identity.Name, PageResult, listOfStreams, listOfCid);
//end of the 1st bit of code
public static void SendRecap(string connectedUser, string HTMLcontent, List<MemoryStream> listOfStreams, List<string> listOfCid)
{
try
{
SmtpClient sender = new SmtpClient();
MailMessage message = new MailMessage();
message.From = new MailAddress("recap@test.com");
message.To.Add(string.Format("{0}@vente-privee.com", connectedUser));
message.Subject = "Test Recap";
message.Body = HTMLcontent;
message.IsBodyHtml = true;
int i = 0;
string plainBody = "Plain text content, viewable by clients that don\'t support html";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(HTMLcontent, null, "text/html");
//create the LinkedResource (embedded image)
foreach (var ms in listOfStreams)
{
ContentType ct = new ContentType();
if (listOfCid[i].Contains("gif"))
{
ct = new ContentType(MediaTypeNames.Image.Gif);
}
else if (listOfCid[i].Contains("jpg") || listOfCid[i].Contains("jpeg"))
{
ct = new ContentType(MediaTypeNames.Image.Jpeg);
}
else if (listOfCid[i].Contains("png"))
{
//contentType = "image/png";
ct = new ContentType(MediaTypeNames.Image.Jpeg);
}
LinkedResource imageResource = new LinkedResource(ms, ct);
imageResource.ContentId = listOfCid[i];
imageResource.TransferEncoding = TransferEncoding.Base64;
htmlView.LinkedResources.Add(imageResource);
i++;
}
message.AlternateViews.Add(plainView);
message.AlternateViews.Add(htmlView);
sender.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
受信したメールを smtp4dev で調べると、左側の列「MIME パーツ」にすべてのパーツが表示されます。
- 名前: multipart/alternative (158959 バイト)
- 名前: テキスト/プレーン (50642 バイト)
- 名前: テキスト/プレーン (65 バイト)
- 名前: multipart/related (107752 バイト)
- 名前: text/html (50642 バイト)
- 名前: image/gif (4610 バイト)
- 名前: 画像/jpeg (1908 バイト)
- 名前: image/gif (540 バイト)
- 名前: image/gif (544 バイト)
- 名前: text/html (48466 バイト)
これらの画像のいずれかを選択して、[本文] タブをクリックし、[名前を付けて保存] を選択して、お気に入りの画像ビューアーで開くこともできます。
「Unnamed: text/html (50642 bytes)」行を選択してから「本文」タブを選択すると、メールの HTML ソースとすべての「cid:」が表示されます (例: src="cid:inv_brandLogo_fr_gif_2013-01-04 -18-50-34-4569409")
しかし、smtp4dev で [開く] をクリックすると、Internet Explorer で電子メールが画像なしで開きます。
IE を開くと、下部に警告が表示されます。「Internet Explorer は、この Web ページをスクリプトまたは ActiveX コントロールの実行から制限しました」と、「ブロックされたコンテンツを許可する」ボタンをクリックしましたが、役に立ちませんでした...
多数のサイトやブログを読みましたが、埋め込まれた画像が表示されない理由がわかりません。ここで何が欠けていますか?