Solved

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

Posted on 2010-08-18
6
908 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

12 Experts available now in Live!

Get 1:1 Help Now