Solved

How to access DeletedItems and SentMail folders in Outlook 2007 through MAPI using VB.NET ?

Posted on 2010-08-18
6
917 Views
Last Modified: 2012-05-10
Hello,

Hope somebody can help me on this :)

Below code works fine (If I intend to read the messages in inbox folder). But when I want to read the messages in Deleted Items folder or Sent Items folder, I receive the error message below:

"Could not complete the operation. One or more parameter values are not valid."

I checked the forums and googled it but I didn't be able to find anything on this. Every code sample I looked at is exactly the same as below and the coders claim it works. But it works only when you read inbox folder, not DeletedItems and SentMail folders.

Thanks In Advance

P.S. I change the 4th line like below when I want to read the messages in Sent Items folder:
 Dim olInbox As Outlook.MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)


////////////////// CODE /////////////////////
////////////////// CODE /////////////////////
////////////////// CODE /////////////////////
Dim olApp As Outlook.Application = New Outlook.Application()
        Dim olNS As Outlook.NameSpace = olApp.GetNamespace("MAPI")
        olNS.Logon(null, null, False, True)
        Dim olInbox As Outlook.MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim olItems As Outlook.Items = olInbox.Items
        Dim i As Integer
        Try
            If olItems.Count > 0 Then
                ListBox1.Items.Clear()
                For i = 1 To olItems.Count
                    Dim email As Outlook.MailItem = olItems.Item(i)

                    ListBox1.Items.Add(email.Subject & " >>> FROM: " & email.SenderName)
                Next
                Label1.Text = "Emails: " & (i - 1).ToString
                olNS.Logoff()
            End If
        Catch ex As Exception
            lblMessage.Text = ex.ToString
        Finally
            olApp = Nothing
            olNS = Nothing
            olItems = Nothing
            olInbox = Nothing
        End Try
/////////////////////////// CODE //////////////////////
/////////////////////////// CODE //////////////////////
/////////////////////////// CODE //////////////////////
0
Comment
Question by:VBDotNetCoder
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33465263
i used this post: http://codegain.com/articles/officedev/howto/how-to-access-the-outlook-contacts-using-C-Sharp.aspxconverted it to vb.net.to get SentMail folder replce:Dim contacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)with:Dim contacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)to get DeletedItems folder replce:Dim contacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)with:Dim contacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems)
Private Shared Sub Main(args As String())
	Dim oApp As New Outlook.Application()

	Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
	oNS.Logon("Outlook", Missing.Value, False, True)
	Dim contacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

	Dim oItems As Outlook.Items = contacts.Items
	Try
		For i As Integer = 0 To oItems.Count - 1
			Dim contact As Outlook.ContactItem = DirectCast(oItems(i), Outlook.ContactItem)

			Console.WriteLine(contact.Email1Address)
		Next

		Console.ReadKey()
	Catch ex As Exception
		Throw ex
	Finally
		oItems = Nothing
		oNS.Logoff()
		oNS = Nothing
		oApp = Nothing
	End Try
End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33465753
After replacing
olNS.Logon(null, null, False, True)
with
olNS.Logon(Nothing, Nothing, False, True)
 
 your code works fine for me in VB 2008.
 
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33465812
 After replacing
olNS.Logon(null, null, False, True)
with
olNS.Logon(Nothing, Nothing, False, True)
 
Your code works fine with both DeletedItems and SentMail folders for me in VB 2008.
 
                               
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 42

Expert Comment

by:sedgwick
ID: 33465974
great :)
glad i helped you.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33466045
@sedgwick,

Oooops, I am referring to the asker's code in the question. hehe...
0
 
LVL 3

Accepted Solution

by:
VBDotNetCoder earned 0 total points
ID: 33474264
Hello guyz.
I'm sorry.
I am coding a software which will read all the users' mailboxes recursively and feed the output (attachments, attachment extensions and their sizes) to SQL Server. Since my code is a little complex (hard to read and understand) I found a sample code from the internet and put it here because part of it is almost the same with mine. But I didn't see the most important part : I'm using GetSharedDefaultFolder command and this example is using GetSharedFolder.

After seeing this, I googled it and now I know I cannot use GetSharedDefaultFolder command to access other users' DeletedItems, Outbox and SentMail folders. Hope this helps to somebody. I'll have to code the program from scratch using another method since it seems impossible using MAPI now....

Thank you all for your time.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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