Add Attachment base64 image in MailMessage and read it in html body(在MailMessage中添加附件Base64图像并在html正文中读取它)
本文介绍了在MailMessage中添加附件Base64图像并在html正文中读取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我必须使用MailMessage
和SmtpClient
发送电子邮件,但我需要发送的图片当前位于base64
string
正文中。
我知道有必要把它放在Attachment
中,但我不知道如何将base64
放在MailMessage
类中,然后阅读它,以便在电子邮件正文中可视化图像。我没有URL图像路径。
推荐答案
在邮件中嵌入图像:(与向邮件中添加附件不同)
如果您使用的是system.net.mail命名空间来发送邮件,则不需要将图像转换为Base64。
var mail = new MailMessage();
var imageToInline = new LinkedResource("Your image full path", MediaTypeNames.Image.Jpeg);
imageToInline.ContentId = "MyImage";
alternateView.LinkedResources.Add(imageToInline);
mail.AlternateViews.Add(body);
更新:
这是将图像嵌入到邮件中的一种有点老套的方式。
Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage("Your base64 image string"));
System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData);
public static string FixBase64ForImage(string Image)
{
System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image, Image.Length);
sbText.Replace("
", string.Empty); sbText.Replace(" ", string.Empty);
return sbText.ToString();
}
var mail = new MailMessage();
var imageToInline = new LinkedResource(streamBitmap , MediaTypeNames.Image.Jpeg);
imageToInline.ContentId = "MyImage";
alternateView.LinkedResources.Add(imageToInline);
mail.AlternateViews.Add(body);
并且您的html邮件正文应具有以下标记:
<img alt ="IiBzcmMgPQ=="cid:MyImage"/>
这篇关于在MailMessage中添加附件Base64图像并在html正文中读取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在MailMessage中添加附件Base64图像并在html正文中读


猜你喜欢
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01