c# - 将图像添加到 System.Net.Mail 消息

标签 c# image visual-studio-2010

我的解决方案中的 Resources.resx 文件中存储了一些图像。我想在我的电子邮件中使用这些图像。我已将图像加载到变量中:

Bitmap myImage = new Bitmap(Resources.Image);

现在我想把它放在我用来创建 HTML 电子邮件的 AlternateView 字符串的 HTML 中。只需要一些帮助。

这是 HTML 字符串(部分):

 body += "</HEAD><BODY><DIV style='height:100%; width:700px;'><div style='height:70px; width:700px; background-color:red;'><img src='" + myImage + "' width='104' height='27' alt='img' style='margin: 20px 0px 0px 20px;'/></div>

如有任何帮助,我们将不胜感激谢谢!

编辑:这是整个代码块。我想我快要明白了,只是缺乏经验妨碍了我:)我试着把它转换成建议的字节,这让我走得更远。仍然没有渲染图像。这里有些事情我做的不对。非常感谢大家对大家的帮助!这是代码(图像所需的 HTML 在字符串 body = 代码的第 3 行中):

 if (emailTo != null)
        {

            Bitmap myImage = new Bitmap(Resources.comcastHeader);
            ImageConverter ic = new ImageConverter();
            Byte[] ba = (Byte[])ic.ConvertTo(myImage, typeof(Byte[]));
            MemoryStream image1 = new MemoryStream(ba);

            LinkedResource headerImage = new LinkedResource(image1, "image/jpeg");
            headerImage.ContentId = "companyLogo";


            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add("" + emailTo + "");
            message.Subject = "" + customer + " Your order is being processed...";
            message.From = new System.Net.Mail.MailAddress("noreply@stormcopper.com");



            string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
            body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
            body += "</HEAD><BODY><DIV style='height:100%; width:700px;'><div style='height:70px; width:700px; background-color:red;'><img src=\"cid:companyLogo\" width='104' height='27' alt='img' style='margin: 20px 0px 0px 20px;'/></div><P>Hello " + customer + ",</P><P>Thank you for shopping at <a href='" + store + "'>" + store + "</A>.  Your order is being processed and will be shipped to you soon.  We would like to take this time to thank you for choosing Storm Copper Components, and we hope you are completely satisfied with your purchase. Please review your information and make sure all the information is correct.  If there are any errors in your order, please contact us immediately <A href='mailto:busbar@stormcopper.com'>here.</A></P>";               
            body += "<P><B>Here is your order information:</B></P>";
            body += "<H3>Contact Information</H3><TABLE><TR><TD><B>Name:</B> " + customer + "</TR></TD><TR><TD><B>Address:</B> " + street + " " + city + ", " + state + " " + zip + "</TR></TD><TR><TD><B>Email:</B> " + emailTo + "</TR></TD><TR><TD><B>Phone:</B> " + phone + "</TR></TD><TR><TD></TD></TR></TABLE>";
            body += "<H3>Products Ordered</H3><TABLE>" + productInformation + "</TABLE><BR /><BR />";
            body += "<H3>Pricing Information</H3><TABLE><TR><TD>Subtotal: $" + subTotal + "</TD></TR><TR><TD>Shipping: $" + shippingInfo + " </TD></TR><TR><TD>Tax: $" + taxInfo + "</TD></TR><TR><TD><B>Total:</B> $" + total + "</TD></TR><BR /></TABLE>";
            body += "<P>Thank you for shopping with us!</P><A href='stormcopper.com'>Storm Copper Components</A>";
            body += "<P><I>This is an Auto-Generated email sent by store copper.  Your email will not be sent to Storm Copper Components if you reply to this message.  If you need to change any information, or have any questions about your order, please contact us using the information provided in this email.</I></P></DIV></BODY></HTML>";



            ContentType mimeType = new System.Net.Mime.ContentType("text/html");

            AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);

            message.AlternateViews.Add(alternate);
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("########");
            smtp.Send(message);

        }

最佳答案

您需要将它们添加到电子邮件中作为 CID 的/链接的资源。

这是我以前用过的一些代码,效果很好。我希望这能给你一些指导:

创建一个AlternateView:

AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, isHTML ? System.Net.Mime.MediaTypeNames.Text.Html : System.Net.Mime.MediaTypeNames.Text.Plain)

创建链接资源:

LinkedResource logo = new LinkedResource("SomeRandomValue", System.Net.Mime.MediaTypeNames.Image.Jpeg);
logo.ContentId = currentLinkedResource.Key;
logo.ContentType = new System.Net.Mime.ContentType("image/jpg");

//将其添加到备选 View

av.LinkedResources.Add(logo);

//最后,将备选 View 添加到消息中:

msg.AlternateView.Add(av);

这里有一些文档可以帮助您了解 AlternativeView 和 LinkedResources 是什么以及它是如何工作的:

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.net.mail.linkedresource(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ms144669(v=vs.110).aspx

在 HTML 本身中,您需要执行以下操作:

"<img style=\"width: 157px; height: 60px;\" alt=\"blah blah\" title=\"my title here\" src=\"cid:{0}\" />";

注意 CID 后跟一个字符串格式 {0} - 然后我用它来用一个随机值替换它。

更新

返回并评论发帖人的评论...这是发帖人的工作解决方案:

string body = "blah blah blah... body goes here with the image tag: <img src=\"cid:companyLogo\" width="104" height="27" />";

byte[] reader = File.ReadAllBytes("E:\\TestImage.jpg");
MemoryStream image1 = new MemoryStream(reader);
AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, System.Net.Mime.MediaTypeNames.Text.Html);

LinkedResource headerImage = new LinkedResource(image1, System.Net.Mime.MediaTypeNames.Image.Jpeg);
headerImage.ContentId = "companyLogo";
headerImage.ContentType = new ContentType("image/jpg");
av.LinkedResources.Add(headerImage);


System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.AlternateViews.Add(av);
message.To.Add(emailTo);
message.Subject = " Your order is being processed...";
message.From = new System.Net.Mail.MailAddress("xxx@example.com");


ContentType mimeType = new System.Net.Mime.ContentType("text/html");
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);

//然后发送消息!

关于c# - 将图像添加到 System.Net.Mail 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19910871/

相关文章:

c# - 如何在 Unity 中为 Material 分配纹理

visual-studio-2010 - T4MVC似乎正在丢失它已经生成的代码

visual-studio-2010 - VS2010有什么办法可以导出watch窗口的内容

javascript - 将 C# 数组传递给参数中的 Jquery 函数

c# - 如何在 C# 中修复此合并排序的实现

c# - 获取 Enumerable.First() 的 MethodInfo 与 Enumerable.OfType() 的 MethodInfo?

java - 为什么应该使用 JLabel 而不是 Image?

python - OpenCV:OCR 之前阴影图像的轮廓检测

android - 在运行时向 Activity 背景添加半透明覆盖

c# - 如何将 VS 命令提示符添加到 Visual Studio 2010 C# Express?