【问题标题】:MailItem.Delete refusing to delete item in Deleted Items folderMailItem.Delete 拒绝删除已删除邮件文件夹中的项目
【发布时间】:2017-07-10 15:10:44
【问题描述】:

不确定为什么下面的代码不会从Deleted Items 文件夹中永久删除MailItem

MailItem 肯定会在Deleted Items 文件夹中结束,但Delete 似乎什么也没做。如您所料,放置第二个Delete 会导致异常,因为它不存在。

我可以通过 Outlook 手动永久删除MailItem

代码:

Dim oOLapp as Outlook.Application
Dim oMapi as Outlook.NameSpace
Dim oFolder as Outlook.Folder
Dim oMailItem as Outlook.MailItem

oOLapp = GetObject([Class]:="Outlook.Application")
oMapi = oOLapp.GetNameSpace("MAPI")
oFolder = oMapi.oFolders(oMapi.DefaultStore.Displayname).Folders("Deleted Items")

oMailItem = oOLapp.Session.OpenSharedItem("C:\sometestmail.msg")
oMailItem = oMailItem.Move(oFolder) ' Now in Deleted items Folder. Verified
' Do other stuff with email
' .
' .
' .
oMailItem.Delete()
oMailItem = nothing

【问题讨论】:

  • 可能是因为删除就是这样做的...将其移至已删除的文件夹。如果您的前景设置为关闭时删除.. 当您关闭它时它们应该会消失。否则,他们会留在那里。
  • 但是您可能想查看此链接vbforums.com/…

标签: vb.net mailitem


【解决方案1】:

要永久删除MailItem,您必须遍历Deleted 文件夹中的Items

为此,请查看以下代码:

Dim oApp As New Outlook.Application

Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")

Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.UserProperties.Add("DeleteMe", Outlook.OlUserPropertyType.olText)
oMailItem.Move(oFolder)
oMailItem = Nothing

For Each item As Outlook.MailItem In oFolder.Items
    Dim oProperty As Outlook.UserProperty = item.UserProperties.Find("DeleteMe")

    If oProperty IsNot Nothing Then
        item.Delete()
    End If
Next

我的代码将与您的代码不同,因为我开启了Option Strict On。我建议你这样做。从长远来看,这将有所帮助。

请注意,我将 UserProperty 设置为 MailItem,然后再将其移动到 Deleted 文件夹。这将有助于识别您要永久删除的单个 MailItem如果您想永久删除Deleted 文件夹中的所有MailItems,那么这就是您需要的代码:

Dim oApp As New Outlook.Application

Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")

Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.Move(oFolder)
oMailItem = Nothing

For i = oFolder.Items.Count To 1 Step -1
    CType(oFolder.Items(i), Outlook.MailItem).Delete()
Next

为了使其工作,您需要遍历Deleted 文件夹向后。查看MSDN 文档,它指出:

Delete 方法删除集合中的单个项目。要删除文件夹的 Items 集合中的所有项目,您必须从文件夹中的最后一个项目开始删除每个项目。例如,在一个文件夹的项目集合AllItems中,如果文件夹中有n个项目,则从AllItems.Item(n)处开始删除该项目,每次递减索引,直到删除AllItems.Item(1) .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2017-11-13
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多