[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-08-18
6
Medium Priority
?
938 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:Meir Rivkin
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 42

Expert Comment

by:Meir Rivkin
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

649 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