如何嵌入的图像流MAILMESSAGE到 [英] How to embed an Image Stream to MailMessage

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

问题描述

我有一些困难嵌入从Properties.Resources的图像MAILMESSAGE一,目前的图像不会在我收到的电子邮件显示。

我已经成功地嵌入从目录位置的图像,但会preFER如果图像从内存/应用程序来了。

下面是我在做什么的简化版本。

 位图B =新位图(Properties.Resources.companyLogo);
 MemoryStream的标志=新的MemoryStream();
 b.Save(标志,ImageFormat.Jpeg); MAILMESSAGE NEWEMAIL = MAILMESSAGE新(从,到);
 newEmail.Subject =主体;
 newEmail.IsBodyHtml = TRUE; LinkedResource footerImg =新LinkedResource(标志,图像/ JPEG);
 footerImg.ContentId =CompanyLogo的;
 AlternateView英尺= AlternateView.CreateAlternateViewFromString(机身+< P>< IMG SRC = CID:CompanyLogo的/>< / P>中,空,text / html的); foot.LinkedResources.Add(footerImg); newEmail.AlternateViews.Add(足); SmtpClient服务器=新SmtpClient(主机,端口);
 server.Send(NEWEMAIL);


解决方案

好吧,我已经解决了这个问题。

而不是使用位图保存方法我转换的位图的byte [],给内存流的字节[]

没有工作:

  b.Save(标志,ImageFormat.Jpeg);

没有工作:

 位图B =新位图(Properties.Resources.companyLogo);
ImageConverter IC =新ImageConverter();
字节] BA =(字节[])ic.ConvertTo(B的typeof(字节[]));
MemoryStream的标志=新的MemoryStream(BA);

我觉得它有什么做的Bitmap.Save方法,在MSDN LIB它提到,流必须具有0偏移量。

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.

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

Did not work :

 b.Save(logo, ImageFormat.Jpeg);

Did Work:

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

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到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆