在 MailMessage 中添加附件 base64 图片并在 html body 中阅读
Add Attachment base64 image in MailMessage and read it in html body
目前我必须发送带有 MailMessage
和 SmtpClient
的电子邮件,但我需要发送一张目前在 base64
string
中的图片 MailAddress
body。
我明白了必须要放在Attachment
里,但是不知道怎么把base64
放在MailMessage
里class然后阅读它以可视化电子邮件 body 中的图像。我没有 url 图片路径。
<body>
<img src='data:image/jpeg;base64, <!-- base64 data --> />
</body>
在 HTML of mail
中使用上面的 img 标签
或者您可以按如下方式附上
Attachment attachment = new Attachment(base64String);
attachment.TransferEncoding = TransferEncoding.Base64;
mailmessage.Attachments.Add(attachment);
要将图像嵌入到您的邮件消息中:(与向消息中添加附件文件不同)
如果您使用 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("\r\n", 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 ="" src ="cid:MyImage"/>
将正文 HTML 转换为 AlternateView
的完整方法
bodyHtml 示例(您可以将其传递到下面的 MailMessage 代码块中)
<p>example</p>
<p><img src=\ "data:image/jpeg;base64,---base64string---"></p>
<p>example</p>
<p><img src=\ "data:image/png;base64,---base64string---"></p>
<p>something</p>
使用此方法,您可以通过许多 ESP(gmail、outlook 等)可视化多个图像
private static AlternateView ContentToAlternateView(string content)
{
var imgCount = 0;
List<LinkedResource> resourceCollection = new List<LinkedResource>();
foreach (Match m in Regex.Matches(content, "<img(?<value>.*?)>"))
{
imgCount++;
var imgContent = m.Groups["value"].Value;
string type = Regex.Match(imgContent, ":(?<type>.*?);base64,").Groups["type"].Value;
string base64 = Regex.Match(imgContent, "base64,(?<base64>.*?)\"").Groups["base64"].Value;
if (String.IsNullOrEmpty(type) || String.IsNullOrEmpty(base64))
{
//ignore replacement when match normal <img> tag
continue;
}
var replacement = " src=\"cid:" + imgCount + "\"";
content = content.Replace(imgContent, replacement);
var tempResource = new LinkedResource(Base64ToImageStream(base64), new ContentType(type))
{
ContentId = imgCount.ToString()
};
resourceCollection.Add(tempResource);
}
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);
foreach (var item in resourceCollection)
{
alternateView.LinkedResources.Add(item);
}
return alternateView;
}
将 Base64 转换为流:
public static Stream Base64ToImageStream(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
return ms;
}
设置邮件消息:
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
AlternateView alterView = ContentToAlternateView(bodyHtml);
mail.AlternateViews.Add(alterView);
//more settings
//...
//////////////
SmtpClient smtp = new SmtpClient(Host, Port) { EnableSsl = false };
smtp.Send(mail);
目前我必须发送带有 MailMessage
和 SmtpClient
的电子邮件,但我需要发送一张目前在 base64
string
中的图片 MailAddress
body。
我明白了必须要放在Attachment
里,但是不知道怎么把base64
放在MailMessage
里class然后阅读它以可视化电子邮件 body 中的图像。我没有 url 图片路径。
<body>
<img src='data:image/jpeg;base64, <!-- base64 data --> />
</body>
在 HTML of mail
中使用上面的 img 标签或者您可以按如下方式附上
Attachment attachment = new Attachment(base64String);
attachment.TransferEncoding = TransferEncoding.Base64;
mailmessage.Attachments.Add(attachment);
要将图像嵌入到您的邮件消息中:(与向消息中添加附件文件不同)
如果您使用 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("\r\n", 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 ="" src ="cid:MyImage"/>
将正文 HTML 转换为 AlternateView
的完整方法bodyHtml 示例(您可以将其传递到下面的 MailMessage 代码块中)
<p>example</p>
<p><img src=\ "data:image/jpeg;base64,---base64string---"></p>
<p>example</p>
<p><img src=\ "data:image/png;base64,---base64string---"></p>
<p>something</p>
使用此方法,您可以通过许多 ESP(gmail、outlook 等)可视化多个图像
private static AlternateView ContentToAlternateView(string content)
{
var imgCount = 0;
List<LinkedResource> resourceCollection = new List<LinkedResource>();
foreach (Match m in Regex.Matches(content, "<img(?<value>.*?)>"))
{
imgCount++;
var imgContent = m.Groups["value"].Value;
string type = Regex.Match(imgContent, ":(?<type>.*?);base64,").Groups["type"].Value;
string base64 = Regex.Match(imgContent, "base64,(?<base64>.*?)\"").Groups["base64"].Value;
if (String.IsNullOrEmpty(type) || String.IsNullOrEmpty(base64))
{
//ignore replacement when match normal <img> tag
continue;
}
var replacement = " src=\"cid:" + imgCount + "\"";
content = content.Replace(imgContent, replacement);
var tempResource = new LinkedResource(Base64ToImageStream(base64), new ContentType(type))
{
ContentId = imgCount.ToString()
};
resourceCollection.Add(tempResource);
}
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);
foreach (var item in resourceCollection)
{
alternateView.LinkedResources.Add(item);
}
return alternateView;
}
将 Base64 转换为流:
public static Stream Base64ToImageStream(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
return ms;
}
设置邮件消息:
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
AlternateView alterView = ContentToAlternateView(bodyHtml);
mail.AlternateViews.Add(alterView);
//more settings
//...
//////////////
SmtpClient smtp = new SmtpClient(Host, Port) { EnableSsl = false };
smtp.Send(mail);