发送后如何从发送的文件夹中删除邮件?

时间:2014-06-04 06:31:43

标签: email vbscript

我目前正在使用vbscript编写代码来自动发送电子邮件。 如何删除我在发送文件夹中发送的同一封电子邮件?

以下是我的代码:

Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MyTime
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail

ToAddress = "site.net"
MessageSubject = "stuff"
MessageBody = "SEND" 

MessageAttachment = "C:\Users\Bellere\Desktop\numbers.csv"

Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

任何帮助都是有效的! 谢谢!

2 个答案:

答案 0 :(得分:1)

本节介绍如何使用Microsoft Outlook 11.0对象库从Visual Basic .NET中的Outlook收件箱中删除邮件。

Dim tempApp As Outlook.Application
    Dim tempSent As Outlook.MAPIFolder
    Dim SentItems As Outlook.Items
    Dim tempMail As Object
    tempApp = CreateObject("Outlook.Application")
    tempSent = tempApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
    SentItems = tempSent.Items
    Dim DeleteMail As Outlook.MailItem
    For Each newMail In SentItems
        DeleteMail.Delete()
    Next

注意此处执行所有任务最重要的一点是添加对&#34; Microsoft Outlook对象库&#34;的引用,如果 < / p>

 Microsoft Outlook 2000, Add "Microsoft Outlook 9.0 object library" 
 Microsoft Outlook 2002, Add "Microsoft Outlook 10.0 object library" 
 Microsoft Outlook 2003, Add "Microsoft Outlook 11.0 object library" 
 Microsoft Outlook 2007, Add "Microsoft Outlook 12.0 object library"

答案 1 :(得分:0)

添加此内容,这应该适用于脚本

中首次出现的已发送项目
Const olMailItem = 0
Const olFolderSentMail = 5
Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MyTime
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
Dim oMail ' <- added

ToAddress = "site.net"
MessageSubject = "stuff"
MessageBody = "SEND" 

MessageAttachment = "C:\Users\Bellere\Desktop\numbers.csv"

Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

' Search for the first occurrence of the sent item (Subject and first recipient address)
Set newMail = Nothing
For Each oMail In ns.GetDefaultFolder(olFolderSentMail).Items
    If oMail.Subject = MessageSubject And oMail.Recipients(1).Address = ToAddress Then
        Set newMail = oMail
        Exit For
    End If
Next
If Not newMail Is Nothing Then newMail.Delete