如何将图像流嵌入到

如何将图像流嵌入到

本文介绍了如何将图像流嵌入到 MailMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 Properties.Resources 中的图像嵌入到 MailMessage 中时遇到了一些困难,目前该图像未显示在我收到的电子邮件中.

I'm having some difficulty embedding an image from the Properties.Resources to a MailMessage, currently the image does not show in the email i receive.

我已经成功地从目录位置嵌入了图像,但更希望图像来自内存/应用程序.

I have successfully embedded the image from a directory location but would prefer if the image came from memory/the application.

这是我正在做的事情的简化版本.

Here is a simplified version of what I am doing.

 Bitmap b = new Bitmap(Properties.Resources.companyLogo);
 MemoryStream logo = new MemoryStream();
 b.Save(logo, ImageFormat.Jpeg);



 MailMessage newEmail = new MailMessage(from, to);
 newEmail.Subject = subject;
 newEmail.IsBodyHtml = true;

 LinkedResource footerImg = new LinkedResource(logo, "image/jpeg");
 footerImg.ContentId = "companyLogo";
 AlternateView foot= AlternateView.CreateAlternateViewFromString(body + "<p> <img src=cid:companyLogo /> </p>", null, "text/html");

 foot.LinkedResources.Add(footerImg);

 newEmail.AlternateViews.Add(foot);

 SmtpClient server = new SmtpClient(host, port);
 server.Send(newEmail);

推荐答案

好的,我已经解决了问题.

Ok i have solved the problem.

我没有使用 BitMap 保存方法,而是将 BitMap 转换为 Byte[] 并为内存流提供了 Byte[]

Instead of using the BitMap save method I converted the BitMap to Byte[] and gave the memory stream the Byte[]

没有用:

 b.Save(logo, ImageFormat.Jpeg);

有效:

Bitmap b = new Bitmap(Properties.Resources.companyLogo);
ImageConverter ic = new ImageConverter();
Byte [] ba = (Byte[]) ic.ConvertTo(b,typeof(Byte[]));
MemoryStream logo = new MemoryStream(ba);

我认为它与 Bitmap.Save 方法有关,在 MSDN 库中它提到流的偏移量必须为 0.

I think it has something to do with the Bitmap.Save method, in the MSDN lib it mentioned that the stream has to have an offset of 0.

这篇关于如何将图像流嵌入到 MailMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 03:12