如何将图像流嵌入到 MailMessage

发布于 2024-11-14 11:47:22 字数 833 浏览 2 评论 0原文

我在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帅气称霸 2024-11-21 11:47:22

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

我没有使用 BitMap 保存方法,而是将 BitMap 转换为 Byte[] 并将 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。

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.

星星的轨迹 2024-11-21 11:47:22
Bitmap b = new Bitmap(Properties.Resources.companyLogo);
MemoryStream logo = new MemoryStream();
b.Save(logo, ImageFormat.Jpeg);

保存后,您必须“寻找”MemoryStream 回到开头。

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

After you do the save, you have to "seek" the MemoryStream back to the start.

logo.Position = 0;
遗弃M 2024-11-21 11:47:22

您可以嵌入图像并通过将其转换为 Base64 来跳过使用资源:

public static string BitmapToBase64(Bitmap b)
{
   ImageConverter ic = new ImageConverter();
   byte[] ba = (byte[])ic.ConvertTo(b, typeof(byte[]));
   return Convert.ToBase64String(ba, 0, ba.Length);
}

并将其用作 html 图像 src :

string logoimage="<img src='data:image/png;base64," + BitmapToBase64(logo) + "'>";

请注意,转换为 Base64 会稍微增加图像的大小。

You can embed the image and skip working with resources by converting it to base64 instead:

public static string BitmapToBase64(Bitmap b)
{
   ImageConverter ic = new ImageConverter();
   byte[] ba = (byte[])ic.ConvertTo(b, typeof(byte[]));
   return Convert.ToBase64String(ba, 0, ba.Length);
}

and use it as html image src :

string logoimage="<img src='data:image/png;base64," + BitmapToBase64(logo) + "'>";

Note that converting to Base64 slighly increases the size of the image.

喜你已久 2024-11-21 11:47:22

尝试看看这里:

http://www .eggheadcafe.com/community/aspnet/2/10219822/send-mail-with-attached-image.aspx

从上面的链接:

static void EmbedImages()
{
   var mail = new MailMessage();

   mail.From = new MailAddress("[email protected]");
   mail.To.Add("[email protected]");
   mail.Subject = "This is an email";

   var plainView = AlternateView.CreateAlternateViewFromString(
      "This is my plain text content, viewable by those clients that don't support html",
      null, "text/plain");

   var htmlView = AlternateView.CreateAlternateViewFromString(
      "Here is an embedded image.<img src=cid:companylogo>", 
      null, "text/html");

   LinkedResource logo = new LinkedResource( "c:\\temp\\logo.gif" );
   logo.ContentId = "companylogo";

   htmlView.LinkedResources.Add(logo);

   mail.AlternateViews.Add(plainView);
   mail.AlternateViews.Add(htmlView);

   var smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
   smtp.Send(mail);
}

Try look here:

http://www.eggheadcafe.com/community/aspnet/2/10219822/send-mail-with-atttached-image.aspx

From the link above:

static void EmbedImages()
{
   var mail = new MailMessage();

   mail.From = new MailAddress("[email protected]");
   mail.To.Add("[email protected]");
   mail.Subject = "This is an email";

   var plainView = AlternateView.CreateAlternateViewFromString(
      "This is my plain text content, viewable by those clients that don't support html",
      null, "text/plain");

   var htmlView = AlternateView.CreateAlternateViewFromString(
      "Here is an embedded image.<img src=cid:companylogo>", 
      null, "text/html");

   LinkedResource logo = new LinkedResource( "c:\\temp\\logo.gif" );
   logo.ContentId = "companylogo";

   htmlView.LinkedResources.Add(logo);

   mail.AlternateViews.Add(plainView);
   mail.AlternateViews.Add(htmlView);

   var smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
   smtp.Send(mail);
}
最单纯的乌龟 2024-11-21 11:47:22

我没有将“徽标”添加到资源中,而是直接将其添加到项目中并将其设置为构建为“嵌入式资源”。

然后使用 System.Reflection.Assymbly 我可以将它(我假设直接)作为流获取:

  var _assembly = Assembly.GetExecutingAssembly();
  var _logoStream = _assembly.GetManifestResourceStream("[Project].[Filename].png");
  var logo = new LinkedResource(_logoStream, "image/png");
  logo.ContentId = "Logo";
  html.LinkedResources.Add(logo);

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:

  var _assembly = Assembly.GetExecutingAssembly();
  var _logoStream = _assembly.GetManifestResourceStream("[Project].[Filename].png");
  var logo = new LinkedResource(_logoStream, "image/png");
  logo.ContentId = "Logo";
  html.LinkedResources.Add(logo);
于我来说 2024-11-21 11:47:22

这是我的实现方式:

var htmlBody = "...<img src=\"cid:logo.jpg\" />...";
    
var message = new MailMessage();
message.To.AddRange(mailAddresses);
message.From = fromAddress;

var plaintextView = AlternateView.CreateAlternateViewFromString(plaintextBody, new ContentType(MediaTypeNames.Text.Plain));
plaintextView.ContentType.CharSet = Encoding.UTF8.WebName;
message.AlternateViews.Add(plaintextView);
    
var logoLinkedResource = new LinkedResource(new MemoryStream(Properties.Resources.Logo), MediaTypeNames.Image.Jpeg)
{
    ContentId = "logo.jpg",
    TransferEncoding = TransferEncoding.Base64
};
    
var htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, new ContentType(MediaTypeNames.Text.Html));
htmlView.ContentType.CharSet = Encoding.UTF8.WebName;
htmlView.LinkedResources.Add(logoLinkedResource);
message.AlternateViews.Add(htmlView);
    
message.Subject = subject;

这与上面的一些答案非常相似,但有点不同,我花了一段时间才获得适合我的案例的详细信息。

请注意,电子邮件客户端将使用最后支持的部分/替代视图,因此应始终首先添加文本版本。

Here's how I implemented it:

var htmlBody = "...<img src=\"cid:logo.jpg\" />...";
    
var message = new MailMessage();
message.To.AddRange(mailAddresses);
message.From = fromAddress;

var plaintextView = AlternateView.CreateAlternateViewFromString(plaintextBody, new ContentType(MediaTypeNames.Text.Plain));
plaintextView.ContentType.CharSet = Encoding.UTF8.WebName;
message.AlternateViews.Add(plaintextView);
    
var logoLinkedResource = new LinkedResource(new MemoryStream(Properties.Resources.Logo), MediaTypeNames.Image.Jpeg)
{
    ContentId = "logo.jpg",
    TransferEncoding = TransferEncoding.Base64
};
    
var htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, new ContentType(MediaTypeNames.Text.Html));
htmlView.ContentType.CharSet = Encoding.UTF8.WebName;
htmlView.LinkedResources.Add(logoLinkedResource);
message.AlternateViews.Add(htmlView);
    
message.Subject = subject;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文