?
Solved

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

Posted on 2010-08-18
6
Medium Priority
?
931 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

801 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