Solved

need vb.net code to delete e-mails from custom outlook folder

Posted on 2011-03-01
3
250 Views
Last Modified: 2012-05-11
have hundreds of thousands of e-mail messages that need to be deleted
doing it manually would take a long long time

trying to automate using vb.net
folder containing e-mails is located under

Deleted Items /  Notifications

-Alex
0
Comment
Question by:AlexF777
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
here's the code for you:
Dim OutlookApp As New Application()
Dim exp As _Explorer = OutlookApp.ActiveExplorer()

For Each st As Store In exp.Session.Stores
	If st.DisplayName.ToLower().StartsWith("mailbox") Then
		Dim rootFolder As MAPIFolder = st.GetRootFolder()
		Dim parentFolder As MAPIFolder = rootFolder.Folders.Cast(Of MAPIFolder)().Where(Function(n) n.Name = "Deleted Items").FirstOrDefault()
		Dim folder As MAPIFolder = parentFolder.Folders.Cast(Of MAPIFolder)().Where(Function(n) n.Name = "Notifications").FirstOrDefault()
		folder.Delete()
		Try
			parentFolder.Folders.Add("Notifications", OlDefaultFolders.olFolderInbox)
		Catch ex As System.Exception
			Console.WriteLine(ex.Message)
		End Try
		Exit For
	End If
Next

exp.Close()

Open in new window

0
 

Author Comment

by:AlexF777
Comment Utility
getting 'NullReferenceException was unhandled'
Object reference not set to an instance of an object
 
after reaching

 For Each st As Store In exp.Session.Stores
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now