我使用以下代码(C#)将图片嵌入到html电子邮件中:
Bitmap image = new Bitmap();
// set something to image
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
ContentType contentType = new ContentType(MediaTypeNames.Image.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
Attachment attachment = new Attachment(ms, contentType);
attachment.ContentId = "image@email";
MailMessage mail = new MailMessage();
mail.From = new MailAddress("...");
mail.To.Add("...");
mail.Subject = "...";
mail.Body = body; // view below
mail.IsBodyHtml = true;
mail.Attachments.Add(attachment);
smtpClient.Send(mail);
ms.Close();
这里是html正文的相关部分:
<img align='center' alt='' src='cid:image@email' width='100' style='padding-bottom: 0; display: inline !important; vertical-align: bottom;'>";
在gmail网站上查看电子邮件看起来是正确的:即图像嵌入到html正文中。
相反,使用Thunderbird,图像作为附件被接收,但不被识别为jpg。该附件被命名为&#34;第1.2和第34部分。如果我双击它,将打开图像,并在计算机上安装默认图像查看器。
为了以与最常见的电子邮件阅读器兼容的方式正确发送嵌入图像,我需要添加什么内容?
为附加图像命名足以添加以下内容:
attachment.Name = "name.jpg";
Still Thunderbird拒绝在html中显示它。
答案 0 :(得分:1)
正如Scarnet所指出的,这是将嵌入式图像放入html电子邮件的正确方法:
AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource(filename, MediaTypeNames.Image.Jpeg);
inline.ContentId = "image@email";
view.LinkedResources.Add(inline);
MailMessage mail = new MailMessage();
mail.From = new MailAddress(username);
mail.To.Add(address);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.AlternateViews.Add(view);
await smtpClient.SendMailAsync(mail);