Solved

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

Posted on 2011-03-01
3
267 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
ID: 35009037
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 35009410
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
ID: 35010979
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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