Outlook 2k7集成(通过P / Invoke) - 阻止不安全的附件

时间:2011-03-01 20:08:44

标签: c# outlook pinvoke outlook-2007

我们目前正在为C#中的一个软件开发一个插件。此插件提取PST文件的内容,并将其中的所有项目存储为文本文件(附件除外,附件存储为与电子邮件相同的文件夹中的类型)。

在我们使用Windows 7 w / Outlook 2K7进行测试之前,它一直没有问题。在使用Outlook 2000的计算机上运行相同的上一个作业后,我们注意到有超过12,000个文件丢失。这些文件被证明是附件(主要是URL)

我们发现问题是Outlook 2K7会阻止具有特定扩展名的附件。如果您在Outlook中打开电子邮件,则会在顶部看到一个蓝色栏,指出“Outlook阻止访问以下可能不安全的附件”以及电子邮件中的所有附件。

有没有办法以编程方式获取这些附件而不阻止Outlook?

我们用来保存附件的代码是:

private void saveAttachment(ref object oEmail, StoreInfo currentStoreInfo, string sEmailID, string sExportPath)
{

   int iAttachCount = 0;
   object oAttach = null;
   oAttach = getNextAttachment(oEmail, ref iAttachCount);

   while (oAttach != null)
   {
      saveAttachment(sEmailID, sExportPath, oAttach);
      oAttach = getNextAttachment(oEmail, ref iAttachCount);
   }

} 

private object getNextAttachment(object oEmail, ref  int iAttachCount)
{
   object oAttach = null;

   try
   {
      iAttachCount++;
      oAttach = GetProperty(oEmail, "Attachments", new object[] { iAttachCount });
   }
   catch //(Exception ex)
   {
      // There was no attachment to be gotten
      oAttach = null;
   }
   return oAttach;
}

1 个答案:

答案 0 :(得分:0)

如果其他人遇到同样的问题,请将此放在此处。 Outlook 2K7具有Level1文件类型功能,可阻止对具有特定扩展名的附件的访问。

但是,您可以更改注册表以允许访问这些文件。请记住,为了安全起见,请将其重新设置为修改之前的方式。

private void SetLevel1RemoveValues()

    {
        // Get the base key for the current user HKEY_CURRENT_USER
        Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, "");
        // Get the security key from the registry
        Microsoft.Win32.RegistryKey subKey = regKey.OpenSubKey("Software\\Microsoft\\Office\\" + sCurrentOutlookVersion + ".0\\Outlook\\Security", true);

        // If the Outlook\Security key doesn't exit, create one
        if (subKey == null)
            subKey = regKey.CreateSubKey("Software\\Microsoft\\Office\\" + sCurrentOutlookVersion + ".0\\Outlook\\Security");

        // Loop through each Value in the registry to see if the Level1Remove key already exists.
        string[] sValues = subKey.GetValueNames();

        bool bHasLevel1RemoveKey = false;
        foreach (string sValue in sValues)
            if (sValue == "Level1Remove")
                bHasLevel1RemoveKey = true;

        // If the key already exists, store the data so we can reset it later
        if (bHasLevel1RemoveKey)
            sPrevLevel1RemoveValues = subKey.GetValue("Level1Remove").ToString();
        else
            sPrevLevel1RemoveValues = "";

        // Create an array of all Level 1 Extensions
        string[] level1Extensions = new string[] { ".ade", ".adp", ".app", ".asp", ".bas", ".bat", 
            ".cer", ".chm", ".cmd", ".com", ".cpl", ".crt", ".csh", ".exe", ".fxp", ".gadget", 
            ".hlp", ".hta", ".inf", ".ins", ".isp", ".its", ".js", ".jse", ".ksh", ".lnk", 
            ".mad", ".maf", ".mag", ".mam", ".maq", ".mar", ".mas", ".mat", ".mau", ".mav", ".maw", 
            ".mda", ".mdb", ".mde", ".mdt", ".mdw", ".mdz", ".msc", ".msi", ".msp", ".mst", ".ops", 
            ".pcd", ".pif", ".pfr", ".prg", ".pst", ".reg", ".scf", ".scr", ".sct", ".shb", ".shs", 
            ".tmp", ".url", ".vb", ".vbe", ".vbs", ".vsmacros", ".vss", ".vst", ".vsw", 
            ".ws", ".wsc", ".wsf", ".wsh" };

        // Set the value in the registry to the list of all Level 1 extensions so we can extract them
        subKey.SetValue("Level1Remove", string.Join(";", level1Extensions));

        // Close (and save) the values
        subKey.Close();
        regKey.Close();
    }