简体   繁体   English

PDF Outlook电子邮件附件有时没有权限保存

[英]PDF Outlook Email attachment sometimes saved with no permissions

Looping through messages (getItem object) in an Outlook message store and saving attachments as files using the code below: 循环浏览Outlook邮件存储中的邮件(getItem对象),并使用以下代码将附件另存为文件:

try 
{
      foreach (Outlook.Attachment attach in getItem.Attachments)
      {
           if (attach.FileName == sItemName)
           {
                string sSaveFile = Path.GetTempPath() + "Attachment" + sItemType;
                if (System.IO.File.Exists(sSaveFile)) System.IO.File.Delete(sSaveFile);
                attach.SaveAsFile(sSaveFile);
                sContent = Common.GetFileContent2(sSaveFile);
           }
      }
 }
 catch (Exception Ex)
 {
      Common.LogError("GetUpdated", Ex, "Get text from Email attachment", "Error", false);
 }
 System.Runtime.InteropServices.Marshal.ReleaseComObject(getItem.Attachments);

I find that the extracted PDF file is sometimes inaccessible for deletion when another PDF file appears. 我发现,当另一个PDF文件出现时,提取的PDF文件有时无法删除。 The file permissions of the extracted PDF file cannot be viewed in Explorer - the Security tab of properties says "You must have read permissions to view the properties of this object". 无法在资源管理器中查看提取的PDF文件的文件权限-属性的“安全性”选项卡显示“您必须具有读取权限才能查看该对象的属性”。 The General properties tab can be read OK. 常规属性选项卡可以阅读确定。

I am using Outlook 2007 in Windows 8.1 我在Windows 8.1中使用Outlook 2007

I'd recommend starting from reviewing the code and releasing all underlying COM objects instantly. 我建议从检查代码开始并立即释放所有基础COM对象。 Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. 使用完后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。 This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. 如果您的加载项尝试枚举Microsoft Exchange Server上存储的集合中的256个以上Outlook项目,则这尤其重要。 If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. 如果不及时释放这些对象,则可以达到Exchange对任何一次打开的最大项目数施加的限制。 Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. 然后在Visual Basic中将变量设置为Nothing(在C#中为null)以释放对该对象的引用。 You can read more about that in the Systematically Releasing Objects article in MSDN. 您可以在MSDN中的“ 系统释放对象”文章中了解有关此内容的更多信息。

For example: 例如:

foreach (Outlook.Attachment attach in getItem.Attachments)

The Attachments property returns an instance of the Attachments class which should be released after. Attachments属性返回Attachments类的实例,该实例应在之后释放。 Also each instance of the Attachment class stays alive until the method ends and GC runs. 另外,Attachment类的每个实例都保持活动状态,直到方法结束并运行GC。

Finally, I'd recommend using the for loop instead. 最后,我建议改为使用for循环。 Thus, you will be able to release every object instantly. 因此,您将能够立即释放每个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM