/// <summary> /// Converts the SRC attribute of IMG tags into embedded content ids (cid). /// Example: <img src="filename.jpg" /< becomes <img src="cid:unique-cid-jpg" /< /// </summary> private void ReplaceImgSrcByCid() { var fileList = new List <string>(); _tagHelper.TagName = "img"; foreach (var element in _tagHelper.StartTags) { var srcAttr = _tagHelper.GetAttributeValue(element, "src"); if (string.IsNullOrEmpty(srcAttr)) { continue; } // this will succeed only with local files (at this time, they don't need to exist yet) var filename = MakeFullPath(MakeLocalPath(srcAttr)); try { if (!fileList.Contains(filename)) { var fileInfo = new FileInfo(filename); var contentType = MimeTypes.GetMimeType(filename); var cid = MimeUtils.GenerateMessageId(); InlineAtt.Add(new FileAttachment(filename, MakeCid(string.Empty, cid, new FileInfo(filename).Extension), contentType)); _tagHelper.ReplaceTag(element, _tagHelper.SetAttributeValue(element, "src", MakeCid("cid:", cid, fileInfo.Extension))); fileList.Add(filename); } } catch { BadInlineFiles.Add(filename); continue; } } }
/// <summary> /// Converts the SRC attribute of IMG tags into embedded content ids (cid). /// Example: <img src="filename.jpg" /< becomes <img src="cid:unique-cid-jpg" /< /// </summary> private void ReplaceImgSrcByCid() { var fileList = new Dictionary <string, string>(); foreach (var element in _htmlDocument.All.Where(m => m is IHtmlImageElement)) { var img = (IHtmlImageElement)element; var currSrc = img.Attributes["src"]?.Value?.Trim(); if (currSrc == null) { continue; } // replace any placeholders with variables currSrc = _mailMergeMessage.SearchAndReplaceVars(currSrc, _dataItem); // Note: if currSrc is a rooted path, _docBaseUrl will be ignored var currSrcUri = new Uri(_docBaseUri, currSrc); // img src is not a local file (e.g. starting with "http" or is embedded base64 image), or manually included cid reference // so we just save the value with placeholders replaced if (string.IsNullOrEmpty(currSrc) || (currSrcUri.Scheme != UriScheme.File) || currSrc.StartsWith("data:image", StringComparison.OrdinalIgnoreCase) || currSrc.StartsWith("cid:", StringComparison.OrdinalIgnoreCase)) { // leave img.Attributes["src"].Value as it is continue; } // this will succeed only with local files (at this time, they don't need to exist yet) var filename = _mailMergeMessage.SearchAndReplaceVarsInFilename(currSrcUri.LocalPath, _dataItem); try { if (!fileList.ContainsKey(filename)) { var fileInfo = new FileInfo(filename); var contentType = MimeTypes.GetMimeType(filename); var cid = MimeUtils.GenerateMessageId(); InlineAtt.Add(new FileAttachment(fileInfo.FullName, MakeCid(string.Empty, cid, fileInfo.Extension), contentType)); img.Attributes["src"].Value = MakeCid("cid:", cid, fileInfo.Extension); fileList.Add(fileInfo.FullName, cid); } else { var cidForExistingFile = fileList[filename]; var fileInfo = new FileInfo(filename); img.Attributes["src"].Value = MakeCid("cid:", cidForExistingFile, fileInfo.Extension); } } catch { BadInlineFiles.Add(filename); continue; } } }
/// <summary> /// Converts the SRC attribute of IMG tags into embedded content ids (cid). /// Example: <img src="filename.jpg" /< becomes <img src="cid:unique-cid-jpg" /< /// </summary> private void ReplaceImgSrcByCid() { var fileList = new HashSet <string>(); foreach (var element in _htmlDocument.All.Where(m => m is IHtmlImageElement)) { var img = (IHtmlImageElement)element; var currSrc = img.Attributes["src"]?.Value; if (currSrc == null) { continue; } // replace any placeholders with variables currSrc = _mailMergeMessage.SearchAndReplaceVars(currSrc, _dataItem); // img scr is not a local file (e.g. starting with "http"), so we just save the value with placeholders replaced if (!string.IsNullOrEmpty(currSrc) && !new Uri(MakeUri(currSrc)).IsFile) { img.Attributes["src"].Value = currSrc; continue; } // this will succeed only with local files (at this time, they don't need to exist yet) var filename = MakeFullPath(MakeLocalPath(currSrc)); try { if (!fileList.Contains(filename)) { var fileInfo = new FileInfo(filename); var contentType = MimeTypes.GetMimeType(filename); var cid = MimeUtils.GenerateMessageId(); InlineAtt.Add(new FileAttachment(fileInfo.FullName, MakeCid(string.Empty, cid, fileInfo.Extension), contentType)); img.Attributes["src"].Value = MakeCid("cid:", cid, fileInfo.Extension); fileList.Add(fileInfo.FullName); } } catch { BadInlineFiles.Add(filename); continue; } } }