Solved

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

Posted on 2010-08-18
6
919 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
Technology Partners: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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