图像显示在附件而不是邮件正文

时间:2013-08-10 17:33:57

标签: asp.net-mvc

我在电子邮件中嵌入图片时遇到问题。接收邮件时我在附件中显示图像而不是邮件正文,我在邮件正文中动态添加图像。我也设置了“cid”,但我没有成功。我设置了ishtmlbody = true但没有在正文中显示图像。请解决我的问题。

我的代码在这里:

这是我的正文消息:

 const string to = "test@gmail.com";
        msg.To.Add(to);
        msg.From = new MailAddress("test@gmail.com");
        msg.Subject = "test";
        int count = 1;
        int stratindex = 0;
        //Create altenative view
        AlternateView alternative = AlternateView.CreateAlternateViewFromString(strMailContent, null, MediaTypeNames.Text.Html);
        while ((lastIndex = strMailContent.IndexOf(findStr, stratindex, StringComparison.Ordinal)) != -1)
        {
            int srcStartIndex =Convert.ToInt32(strMailContent.IndexOf("src", lastIndex, StringComparison.Ordinal)) + 5;
            int srcEndIndex = strMailContent.IndexOf(@"'", srcStartIndex, StringComparison.Ordinal);
            string imgSrc = strMailContent.Substring(srcStartIndex, srcEndIndex - srcStartIndex);
            string path = imgSrc;
            // Atteched resource
            // set cid 
            var resource = new LinkedResource(path, "image/jpg");
            string cid = "companylogo" + count;
            //now add the AlternateView

            resource.ContentId = cid;
            alternative.LinkedResources.Add(resource);
            msg.AlternateViews.Add(alternative);
            //now append it to the body of the mail


            strMailContent = strMailContent.Replace(strMailContent.Substring(srcStartIndex, srcEndIndex - srcStartIndex), "cid:" + cid);
            stratindex = strMailContent.IndexOf("<br/>", lastIndex, StringComparison.Ordinal) + 5;
            strMailContent = strMailContent.Remove(stratindex - 5, 5);

            stratindex = stratindex - 5;
            count++;
        }

在屏幕短红色区域显示附件,黄色区域显示空白体。

如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我没有看到您的代码有任何问题,您必须确保路径文件正确