将base64图像添加到MailMessage中,并在html正文中读取它。
问题的原因是在邮件消息中添加了基于base64编码的图像附件,并在HTML正文中读取它们时,类属性和内联样式将被移除。
解决方法是通过将HTML正文转换为AlternateView来实现。该方法会遍历HTML正文中的所有图像标签,提取图像的类型和base64编码,并将其替换为“cid:图片编号”的形式。然后,将图像转换为LinkedResource对象并将其添加到AlternateView的LinkedResources集合中。最后,将AlternateView添加到MailMessage的AlternateViews集合中。
以下是完整的代码示例:
// 完成将HTML正文转换为AlternateView的方法 private static AlternateView ContentToAlternateView(string content) { var imgCount = 0; ListresourceCollection = new List (); foreach (Match m in Regex.Matches(content, ".*?)>")) { imgCount++; var imgContent = m.Groups["value"].Value; string type = Regex.Match(imgContent, ":(? .*?);base64,").Groups["type"].Value; string base64 = Regex.Match(imgContent, "base64,(? .*?)\"").Groups["base64"].Value; if (String.IsNullOrEmpty(type) || String.IsNullOrEmpty(base64)) { // 忽略普通的标签 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 MailMessage mail = new MailMessage(); mail.IsBodyHtml = true; AlternateView alterView = ContentToAlternateView(bodyHtml); mail.AlternateViews.Add(alterView); // 更多设置 // ... // 发送邮件 SmtpClient smtp = new SmtpClient(Host, Port) { EnableSsl = false }; smtp.Send(mail);
以上是如何添加基于base64编码的图像附件并在HTML正文中读取它们的方法。但是,该方法会移除类属性和内联样式。如果需要支持自定义属性,如样式,请参考链接中提供的修复代码。这段代码修复了移除类属性和内联样式的问题。
希望对您有所帮助!
问题的原因是想在邮件正文中添加一个以base64编码的图片,但是使用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);
更新的解决方法是将base64格式的图片转换为位图,并将其嵌入邮件正文中。
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代码中添加以下标签:
如果只是想将base64格式的图片转换为位图,可以参考此链接:[http://stackoverflow.com/a/5083417/6170142](http://stackoverflow.com/a/5083417/6170142)。
问题的原因是在MailMessage类中无法直接将base64图像附加到邮件中。解决方法是将base64图像作为附件添加到邮件中,然后在HTML正文中以img标签的形式读取它。
解决方法中的代码示例是将base64字符串直接作为附件添加到MailMessage类中的Attachments列表中。但是,由于附件文件名过长而导致的PathTooLongException异常。为了解决这个问题,可以使用流来创建附件,并在传递之前将流重置为起始位置。
在讨论中提到,使用编码的内联图像可能不被许多电子邮件客户端支持。因此,建议将图像以img标签的形式直接嵌入HTML正文中,而不是作为附件添加。
解决方法是将base64图像作为附件添加到MailMessage类中,或者将其直接嵌入HTML正文中的img标签中。但需要注意附件文件名过长可能导致异常的问题。此外,需要考虑到许多客户端不支持编码的内联图像的情况。