【问题标题】:Embedding .jpg image (base64 string representation) into email message body (System.Net.Mail)将 .jpg 图像(base64 字符串表示)嵌入电子邮件正文(System.Net.Mail)
【发布时间】:2019-01-24 19:26:12
【问题描述】:

我能够正确地将 jpg 图像转换为 base64String。但我无法使用转换后的图像字符串和 LinkedResource 将其嵌入到电子邮件正文中。该图像在电子邮件正文中显示为“未找到图像”图标。 任何帮助将不胜感激。

我已按照此链接中的示例进行操作:Iterate through an html string to find all img tags and replace the src attribute values

我正在使用 HtmlAgilityPack(nuget 包)通过以下代码定位 img 元素。

private string embedImageInMail(string html)
       {
            HtmlAgilityPack.HtmlDocument doc = new 
HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            doc.DocumentNode.Descendants("img").Where(e =>
            {
                string src = e.GetAttributeValue("src", null) ?? "";
                return !string.IsNullOrEmpty(src) && src.StartsWith("data:image");
            })
            .ToList()
            .ForEach(x =>
            {
                string currentSrcValue = x.GetAttributeValue("src", null);
                currentSrcValue = currentSrcValue.Split(',')[1];    //Base64 part of string
                byte[] imageData = Convert.FromBase64String(currentSrcValue);
                string contentId = Guid.NewGuid().ToString();
                using (MemoryStream ms = new MemoryStream(imageData))
                {
                    LinkedResource inline = new LinkedResource(ms, "image/jpeg");
                    inline.ContentId = contentId;
                    inline.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                    x.SetAttributeValue("src", "cid:" + inline.ContentId);
                }
            });

        return doc.DocumentNode.OuterHtml; 
    }

传递给函数的 html 参数包含一个 img 标签,其 src 等于图像的 base64 编码。 从此函数返回的内容被分配给电子邮件的 message.body。

【问题讨论】:

    标签: c# image-processing base64 system.net.mail


    【解决方案1】:

    通过以下其他帖子中的解决方案解决。如果有其他人尝试做同样的事情并且像我一样遇到麻烦,我将在下面发布代码。

    基本上,我必须将 html 中的 LinkedResource(图像)保存到一个列表中,然后遍历该列表并将所有图像添加到 foreach 循环之外的 AlternateView。

     // Embeds image to properly show in Email. Image element to show in html will not work in Email body. 
                            // https://stackoverflow.com/questions/39785600/iterate-through-an-html-string-to-find-all-img-tags-and-replace-the-src-attribut
                            // XmlAgilityPack gets used here to parse html string. 
                            List<LinkedResource> linkedResources = new List<LinkedResource>(); 
                            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                            int increment = 0; 
                            doc.LoadHtml(tempMsg);
                            doc.DocumentNode.Descendants("img").Where(z =>
                                {
                                    string src = z.GetAttributeValue("src", null) ?? "";
                                    return !string.IsNullOrEmpty(src) && src.StartsWith("data:image");
                                })
                                .ToList()
                                .ForEach(x =>
                                {
                                    string currentSrcValue = x.GetAttributeValue("src", null);
                                    currentSrcValue = currentSrcValue.Split(',')[1];    //Base64 part of string
                                    byte[] imageData = Convert.FromBase64String(currentSrcValue);
                                    string id = Guid.NewGuid().ToString();
                                    increment++;
    
                                    LinkedResource inlineImage = new LinkedResource(new MemoryStream(imageData), System.Net.Mime.MediaTypeNames.Image.Jpeg);
                                    inlineImage.ContentType.Name = "img " + increment; 
                                    inlineImage.ContentId = id;
                                    inlineImage.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                                    x.SetAttributeValue("src", "cid:" + inlineImage.ContentId);
                                    linkedResources.Add(inlineImage);
                                });
    
                            // Append multiple images from template to email message.
                            // https://stackoverflow.com/questions/7048758/how-to-embed-multiple-images-in-email-body-using-net
                            // Write html to message.body 
                            AlternateView altView = AlternateView.CreateAlternateViewFromString(doc.DocumentNode.OuterHtml, null, "text/html");
                            linkedResources.ForEach(x=>altView.LinkedResources.Add(x));
                            message.AlternateViews.Add(altView);
    
                            String[] attachments = txtAttachFiles.Text.Split(';');
                            foreach (String filename in attachments)
                            {
                                if (File.Exists(filename))
                                {
                                    Attachment attachment = new Attachment(filename);
                                    message.Attachments.Add(attachment);
                                }
                            }
    
                            // loop is set to 1 in the app.config file so technically this for loop is not needed, but will keep this loop just in case. 
                            for (int loop = 0; loop < Convert.ToInt32(ConfigurationManager.AppSettings["loop"]); loop++)
                            {
                                SmtpClient smtp = new SmtpClient();
                                smtp.Host = ConfigurationManager.AppSettings["mailHost"];
                                smtp.Port = Int32.Parse(ConfigurationManager.AppSettings["mailPort"]);
                                smtp.UseDefaultCredentials = Convert.ToBoolean(ConfigurationManager.AppSettings["mailDefaultCredentials"]);
                                smtp.Send(message);
                            }
                        }
    

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2019-12-14
      • 2013-01-17
      • 1970-01-01
      • 2014-04-16
      • 2011-10-30
      • 2022-12-15
      • 2013-09-26
      • 2014-03-09
      相关资源
      最近更新 更多