所以我有以下代码,它从联系人项目(位于共享文件夹中)中提取所有附件:
Outlook._Application objOutlook; //declare Outlook application
objOutlook = new Outlook.Application(); //create it
Outlook._NameSpace objNS = objOutlook.Session; //create new session
Outlook.MAPIFolder oAllPublicFolders; //what it says on the tin
Outlook.MAPIFolder oPublicFolders; // as above
Outlook.MAPIFolder objContacts; //as above
Outlook.Items itmsFiltered; //the filtered items list
oPublicFolders = objNS.Folders["Public Folders"];
oAllPublicFolders = oPublicFolders.Folders["All Public Folders"];
objContacts = oAllPublicFolders.Folders["Global Contacts"];
itmsFiltered = objContacts.Items.Restrict(strFilter);//restrict the search to our filter terms
for (int i = 1; i <= itmsFiltered.Count; i++) //loop through filtered items
{
var item = itmsFiltered[i];
Contact ctctNew = new Contact(); //create new contact
foreach (Outlook.Attachment oa in item.Attachments)
{ ctctNew.ImportedAttachments.Add(oa); }
lstContacts.Add(ctctNew); // add to the list that will be displayed in the OLV
}
return lstContacts;
这似乎很有效。
然后我试着把这些保存到一个文件中,这样:
if (!System.IO.Directory.Exists(strPath))
{ System.IO.Directory.CreateDirectory(strPath); }
foreach (Outlook.Attachment o in ctLoaded.ImportedAttachments)
{
string strFilePath = strPath + @"" + o.FileName;
if (!File.Exists(strFilePath))
{
o.SaveAsFile(strPath + @"" + o.FileName); //exception here
}
}
最后一位适用于非.msg文件类型,即适用于.pdf文件等;如果它是一个.msg文件,那么我会得到以下异常:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Event Management System.exe
Additional information: Cannot save the attachment. Could not complete the operation. One or more parameter values are not valid.
有人能发光吗?
感谢
更新:我发现只有一些.msg文件无法保存;其中一些工作得很好。似乎某些用户附加的文件似乎有效,而其他用户的文件则无效;我猜这可能与他们如何连接有关?
此外,代码似乎确实保存了文件(尽管引发了异常),但它似乎已损坏——相关文件夹中出现了一个3Kb.msg文件。Outlook不会打开它,如果我试图打开文件,我只会收到Outlook的"无法读取项目"消息框。
作为猜测,一个消息附件的o.FileName的值是多少?它可能不是有效的文件名。