我对电子邮件中的嵌入图像有疑问。接收邮件时,我在附件而不是邮件正文中显示图像,并且我在邮件正文中动态添加图像。而且我已经设置了“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++;
}
在屏幕中显示红色区域显示附件,黄色区域显示空白正文。
如何解决这个问题呢。