Solved

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

Posted on 2011-03-01
3
275 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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