【发布时间】:2011-07-21 06:51:57
【问题描述】:
我尝试获取可以在 EmailMessage 的附件中找到的图像。但是当我运行代码时,我收到了这个错误:对象引用未设置为对象的实例。
这是代码:
string sHTMLCOntent = item.Body;
FileAttachment[] attachments = null;
if (item.Attachments.Count != 0)
{
attachments = new FileAttachment[item.Attachments.Count];
for (int i = 0; i < item.Attachments.Count; i++)
{
string sType = item.Attachments[i].ContentType.ToLower();
if (sType.Contains("image"))
{
attachments[i] = (FileAttachment)item.Attachments[i];
string sID = attachments[i].ContentId;
sType = sType.Replace("image/", "");
string sFilename = sID + "." + sType;
string sPathPlusFilename = Directory.GetCurrentDirectory() + "\\" + sFilename;
attachments[i].Load(sFilename);
string oldString = "cid:" + sID;
sHTMLCOntent = sHTMLCOntent.Replace(oldString, sPathPlusFilename);
}
}
}
//string s = System.Text.Encoding.UTF8.GetString(item.MimeContent.Content);
//FreeTextBox1.Text += s;
【问题讨论】:
-
错误也应该给你一个行号。
-
第 88 行:字符串 sType = item.Attachments[i].ContentType.ToLower();
-
那么
item、Attachments[i]或ContentType为空,可能是Attachments[i]。 -
你引用的 EmailMessage 对象的类型是什么。它在什么命名空间中?
-
使用 Microsoft.Exchange.WebServices.Data; item 是一个 EmailMessage 对象
标签: c# asp.net email exchange-server