如何将图像流嵌入到 MailMessage
我在将 Properties.Resources 中的图像嵌入到 MailMessage 中时遇到一些困难,目前该图像未显示在我收到的电子邮件中。
我已成功从目录位置嵌入图像,但更喜欢图像来自内存/应用程序。
这是我正在做的事情的简化版本。
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好的,我已经解决了问题。
我没有使用 BitMap 保存方法,而是将 BitMap 转换为 Byte[] 并将 Byte[] 提供给内存流
不起作用: 起作用
:
我认为它与 Bitmap.Save 方法有关,在 MSDN 库中提到流的偏移量必须为 0。
Ok i have solved the problem.
Instead of using the BitMap save method I converted the BitMap to Byte[] and gave the memory stream the Byte[]
Did not work :
Did Work:
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.
保存后,您必须“寻找”MemoryStream 回到开头。
After you do the save, you have to "seek" the MemoryStream back to the start.
您可以嵌入图像并通过将其转换为 Base64 来跳过使用资源:
并将其用作 html 图像 src :
请注意,转换为 Base64 会稍微增加图像的大小。
You can embed the image and skip working with resources by converting it to base64 instead:
and use it as html image src :
Note that converting to Base64 slighly increases the size of the image.
尝试看看这里:
http://www .eggheadcafe.com/community/aspnet/2/10219822/send-mail-with-attached-image.aspx
从上面的链接:
Try look here:
http://www.eggheadcafe.com/community/aspnet/2/10219822/send-mail-with-atttached-image.aspx
From the link above:
我没有将“徽标”添加到资源中,而是直接将其添加到项目中并将其设置为构建为“嵌入式资源”。
然后使用 System.Reflection.Assymbly 我可以将它(我假设直接)作为流获取:
Instead of adding the "logo" to Resources, I added it directly to the project and set it to build as an "Embedded Resource".
Then using System.Reflection.Assymbly I can get it (I assume directly) as a Stream:
这是我的实现方式:
这与上面的一些答案非常相似,但有点不同,我花了一段时间才获得适合我的案例的详细信息。
请注意,电子邮件客户端将使用最后支持的部分/替代视图,因此应始终首先添加文本版本。
Here's how I implemented it:
This is fairly similar to some of the above answers, but a little different, and took me a while to get the details right for my case.
Note that email clients will use the last supported part/alternative view, so the text version should always be added first.