Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Program to Delete mail older than x days

Status
Not open for further replies.

Tegwin15

IS-IT--Management
Mar 4, 2003
3
GB
hello,
I have a problem whereby we have people who download mail from a mail server to a Handheld device, and for some reason it does not delete the mail off the server once the person has received it, so I would like to write a program that will delete any mail off the server that is older than x number of days. Can someone give me some idea on how to do this. It is for outlook and Exchange
 
PLEASE !!! can someone help me here, I really quite desperate to get this done.
 
I presume these e-mails are in each user's Inbox. AFAIK the only way to access these mailboxes to delete items is through the Outlook client.

You need to create/designate a mailbox to which the procedure can log on. Each of the users will have to grant this mailbox delegate permissions both to the mailbox root and the Inbox.

Then you need to run the app on a client PC with Outlook installed and preferably with default profile same as designated mailbox. The app can automate Outlook and use the GetSharedDefaultFolder method (NameSpace object) to return each user's mailbox in a loop.

In this code snippet, the Mailbox parameter contains the display name in the GAL.

Code:
Create a dummy e-mail to the mailbox owner
Set objDummyItem = objOLApp.CreateItem(olMailItem)
Set objRecip = objDummyItem.Recipients.Add("=" & Mailbox)
'Attempt to resolve the recipient
objRecip.Resolve
If Not objRecip.Resolved Then
 Set objDummyItem = Nothing
 Set objRecip = Nothing
 Err.Raise oerrAmbiguousRecipient, , _
 "Cannot resolve '" _
 & Mailbox & "' in the Global Address List."
End If
'Use the recipient object to get the shared folder
Set objSharedFolder = Nothing
On Error Resume Next
Set objSharedFolder = objOLNS.GetSharedDefaultFolder(objRecip, olFolderInbox)
On Error GoTo 0
If objSharedFolder Is Nothing Then
 Err.Raise oerrMailboxOpenFail, , _
 "Cannot open the shared folder in Mailbox: '" _
 & Mailbox & "'."
End If
'Use Restrict to return the items collection
Set objSharedItems = objSharedFolder.Items.Restrict( _
&quot;[Received] < &quot; & Chr$(34) & _
Format$(Date - intDays, &quot;short date&quot;) & Chr$(34))
'Delete the items
With objSharedItems
 If .Count > 0 Then
  For lngC1 = 1 To .Count
   .Item(1).Delete
  Next
 End If
End With

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top