所以我有以下代码,它从联系人项目中提取所有附件(位于共享文件夹中):
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 的消息框。