Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

February 27, 2006 12:00 AM

Using a Script to Delete All Messages in a Set of Mailboxes

Windows IT Pro
InstantDoc ID #49238
Rating: (1)
Downloads
49238.zip

I need to create a script that I can use to connect to several mailboxes and delete all messages within them. I could manually connect to them, of course. But I have 10 accounts that aren't associated with users but are being used by other services running on our network, and I need to clean all of them up periodically. What would such a script look like?

This type of cleanup activity calls for the use of the Collaboration Data Objects (CDO) 1.21 programming library, an optional component in Outlook that supports dynamic logons. As long as you know the server name and mailbox alias and have appropriate permissions, you can write CDO code to log on to a specific mailbox and perform operations on it. It doesn't matter whether Outlook is running or not.

The code in Web Listing 1 consists of three procedures:

  • PurgeMailbox—the main procedure your script will call
  • ProcessCDOFolder—a recursive procedure that processes all items and subfolders in a given folder
  • DeleteCDOFolderItems—the procedure that actually deletes the items from the folder

Put all three procedures in your script, then use one or more statements to call PurgeMailbox with specific server and mailbox alias names:

Call PurgeMailbox("myserver", _ 
  "mymailbox") 

The PurgeMailbox procedure uses those parameters, separated by a line feed, to build a profile information string that the Session.Logon method can use to log on to that specific mailbox, as the code at callout A in Web Listing 1 shows.

The ProcessCDOFolder procedure is a recursive procedure because it calls itself. Recursion enables the code to handle any number of folders, not just the Inbox and other folders on the same level.

Both the PurgeMailbox and Process-CDOFolder procedures skip contacts folders so that the code can run unattended—from the Windows scheduler, for example. One quirk of Outlook security is that touching a contacts folder with CDO code puts up a security prompt, even if you don't access properties of any individual items.

To download the code for Web Listing 1 click the 49238.zip hotlink. As with any code that acts on a mailbox—especially code that deletes items—you should test, test, and test again before implementing this code in a production environment.

Related Content:

ARTICLE TOOLS

Comments
  • toecker
    6 years ago
    Jun 22, 2006

    Why not simly use the mailbox manager (recipient policies) to automatically delete messages?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.