如何使用C#插入电子邮件的多个图像内联体

时间:2016-01-05 16:33:08

标签: asp.net regex c#-4.0

我正在尝试在电子邮件的正文(内联)中插入图像,并且我能够插入图像。

但对于多张图片,我在这里面临艰难时期。

emailMessage.Body = "<div style=\"font-family:Arial\">This is an INLINE attachment:<br /><br /><img src=\"\\shared folder\image 1.png" alt=\"\"><br /><br />Thanks for downloading this example.</br><img src=\"\\shared folder\image 2.png" alt=\"\"> </br>

身体将如上所示。要做到这一点,我正在努力做到以下几点。

  1. 扫描文本
  2. 获取src属性中的值 3.在字符串列表中输出每个值 4.浏览列表和循环,并希望下面的代码适用于单个图像。
  3. 任何机构都可以建议如何使用C#语法实现这一功能。 示例电子邮件正文如下:

                        emailMessage.Body = "<div style=\"font-family:Arial\">This is an INLINE attachment:<br /><br /><img src=\"\\shared folder\image 1.png" alt=\"\"><br /><br />Thanks for downloading this example.</br><img src=\"\\shared folder\image 2.png" alt=\"\"> </br>;
                        //string strRegex = "/src=([^\\"]+)/";
                        //Regex srcRegex = new Regex(strRegex);
                        string[] immageArray = Regex.Split(emailMessage.Body, "<img src=");
    
    
                        //string[] immageArray = Regex.Match(emailMessage.Body, srcRegex,RegexOptions.None,Timesp);
                        foreach(string imgLine in immageArray)
                        {
                            attachmentPath = Environment.CurrentDirectory + imgLine;
                            // generate the contentID string using the datetime
                             contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";
                             // create the INLINE attachment
                             Attachment inline = new Attachment(attachmentPath);
                             inline.ContentDisposition.Inline = true;
                             inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
                             inline.ContentId = contentID;
                             inline.ContentType.MediaType = "image/png";
                             inline.ContentType.Name = Path.GetFileName(attachmentPath);
                             emailMessage.Attachments.Add(inline);
    
                             // replace the tag with the correct content ID
                             emailMessage.Body = emailMessage.Body.Replace("@@IMAGE@@", "cid:" + contentID);
                        }
    

    但是我无法从emailMessage.Body中获取图像路径,任何人都能纠正这个吗? 谢谢。

0 个答案:

没有答案