Solved

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

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 Shortcut (VB) 4 35
Syntax Error 2 47
Visual Studio 2015 Source Code Control 10 38
Help with AsEnumerable(), LINQ 4 22
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

26 Experts available now in Live!

Get 1:1 Help Now