我尝试获取您可以在EmailMessage附件中找到的图像。但是当我运行代码时,我得到这个错误: object reference没有设置为一个对象的实例。
代码如下:
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;发布于 2011-03-23 20:49:32
既然你是在吹嘘
string sType = item.Attachments[i].ContentType.ToLower();并且您已经检查了附件的长度,则不能设置Attachment对象的内容类型。由于您正在使用它来执行代码的其余部分,因此看起来这是您所必需的。这条消息是如何生成的?
我会添加一些代码来查看附件,可能是AttachmentType或FileName属性,以确保您拥有附件的详细信息,并确定操作过程。可能是您自己添加附件,只需要设置contenttype即可。祝好运!
发布于 2011-03-23 20:33:12
而不是
FileAttachment[] attachments = null;您应该为它创建一个实例:
FileAttachment[] attachments = new FileAttachment[10];例如
https://stackoverflow.com/questions/5405237
复制相似问题