如果将字符串作为图像,如何在电子邮件中发送内嵌图像? [英] How can I send inline image in email when having a string as image?

查看:69
本文介绍了如果将字符串作为图像,如何在电子邮件中发送内嵌图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过保存在我的Web应用程序Images文件夹中的电子邮件发送图像,但我的问题是我在数据库中将图像作为字符串格式如何通过电子邮件发送?





对于发送文件夹中的图像,我使用以下代码:



I can send the image by email saved in my web application Images folder , but my question is i have image in database as string format how can i send it by email?


For Image in folder sending i am using below code:

LinkedResource logo = new LinkedResource(System.AppDomain.CurrentDomain.BaseDirectory + @"Images/logo.jpg");
logo.ContentId = "logo";
AlternateView av1 = AlternateView.CreateAlternateViewFromString("" + htmlTemplate, null, MediaTypeNames.Text.Html);

av1.LinkedResources.Add(logo);
message.AlternateViews.Add(av1);





如果我有字符串格式的图像,如何发送?



我尝试了什么:



i尝试过替换< img src = [abc]> 之类的,



How to Send if i have image in string format?

What I have tried:

i have tried to replace the <img src=[abc]> like,

htmlTemplate=htmlTemplate.Replace("[abc]", @"data:image/gif;base64," + software.SoftwareImage);  // does not worked



也尝试了


also tried

LinkedResource(@"data:image/gif;base64," + software.SoftwareImag);

//does not worked

推荐答案

假设您的图像是存储为Base64字符串的Gif:

Assuming your image is a Gif stored as a Base64 string:
byte[] imageBytes = Convert.FromBase64String(software.SoftwareImag);
Stream contentStream = new MemoryStream(imageBytes);
LinkedResource image = new LinkedResource(contentStream, MediaTypeNames.Image.Gif);
image.ContentId = "software";

AlternateView av1 = AlternateView.CreateAlternateViewFromString(htmlTemplate, null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(image);
message.AlternateViews.Add(av1);



注意:将图像作为Base64编码的字符串浪费存储在数据库中很多空间。您应该使用二进制类型存储它,或者将图像存储在文件系统中,并将路径存储在数据库中。


NB: Storing the image in the database as a Base64-encoded string wastes a lot of space. You should either be storing it using a binary type, or storing the image in the file system, and just storing the path in the database.


这篇关于如果将字符串作为图像,如何在电子邮件中发送内嵌图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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