1

几天以来,我试图通过 c# 重命名已发送邮件文件夹、已删除元素和收件箱文件夹。

我尝试过这样的事情:

 List<Outlook.MailItem> mailItems = new List<Outlook.MailItem>();
            Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
            // Add PST file (Outlook Data File) to Default Profile
            outlookNs.AddStore(pstFilePath);
            Outlook.MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();

           Outlook.Folders subFolders = rootFolder.Folders;

  foreach (Outlook.Folder folder in subFolders)
            {

              folder.Name =  (folder.Name == "deleted Elements"?"deleted":folder.Name);
}

但没有成功。我总是得到我无权更改名称的例外情况。其他自定义创建的文件夹我可以毫无问题地重命名。

有什么办法可以解锁文件夹吗?或者还有其他访问文件夹的可能性吗?

非常感谢

编辑: Expetion 是:您没有权限。

4

1 回答 1

1
public string RenameFolder(string name, string folderid)
    {
        Outlook.Application app = new Outlook.Application();
        Outlook.NameSpace ns = null;
        Outlook.Folder folder = null;
        string n= null;

        try
        {
            ns = app.GetNamespace("MAPI");
            folder = ns.GetFolderFromID(folderid) as Outlook.Folder;
            n=folder.Name;
            folder.Name = (folder.Name = name) ;
            return n + " has been successfully changed to " + folder.Name;
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (app != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);
            }

            if (folder != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(folder);
            }

            if (ns != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ns);
            }
        }
    }

这段代码对我有用..当我在管理员模式下运行 Visual Studio 时..

于 2016-05-01T05:26:36.550 回答