How do I get a MailItem from Outlook?


T

Tom P.

I am trying to search an Outlook folder for certain e-mail items but I
am running into a problem getting items from the MAPIFolder object. I
use the following code but I can't find anything to cast the item to
that doesn't get an exception.


private List<SearchItem> SearchFolder(Outlook.MAPIFolder
CurrentFolder, string SearchString)
{
for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
iLoop++)
{
//This is where I get the exception
CurrentItem =
(Outlook.MailItemClass)CurrentFolder.Items[iLoop];
}
}

I've tried casting to MailItem, PostItem, Object, I've tried using
Folder.Items.GetFirst()... Nothing works. How do I get the item from
the MAPIFolder? Am I even supposed to be using the MAPIFolder?

I always get an exception: "Unable to cast COM object of type
'System.__ComObject' ........ failed due to the following error: No
such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."

I've looked up several code examples and none of them address this.
I'm using Microsoft.Office.Interop.Outlook v 11.0

Any help would be appreciated.
Tom P.
 
Ad

Advertisements

K

Karl Mitschke

Hello Tom P.,
I am trying to search an Outlook folder for certain e-mail items but I
am running into a problem getting items from the MAPIFolder object. I
use the following code but I can't find anything to cast the item to
that doesn't get an exception.

private List<SearchItem> SearchFolder(Outlook.MAPIFolder
CurrentFolder, string SearchString)
{
for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
iLoop++)
{
//This is where I get the exception
CurrentItem =
(Outlook.MailItemClass)CurrentFolder.Items[iLoop];
}
}
I've tried casting to MailItem, PostItem, Object, I've tried using
Folder.Items.GetFirst()... Nothing works. How do I get the item from
the MAPIFolder? Am I even supposed to be using the MAPIFolder?

I always get an exception: "Unable to cast COM object of type
'System.__ComObject' ........ failed due to the following error: No
such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."

I've looked up several code examples and none of them address this.
I'm using Microsoft.Office.Interop.Outlook v 11.0

Any help would be appreciated.
Tom P.

Is your mailbox on Exchnage?

If so, is it Exchange 2007 or 2010?

If so, Have you looked in to the Exchange Web Services Managed API?

If so, can I stop saying if so ?;)

If you are using a POP3 mailbox, see here:
http://support.microsoft.com/kb/310244

Also, for the Exchange Web Services, and other development issues, see here:
http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/threads

Karl
http://unlockpowershell.wordpress.com/
 
T

Tom P.

Hello Tom P.,


I am trying to search an Outlook folder for certain e-mail items but I
am running into a problem getting items from the MAPIFolder object. I
use the following code but I can't find anything to cast the item to
that doesn't get an exception.
private List<SearchItem> SearchFolder(Outlook.MAPIFolder
CurrentFolder, string SearchString)
{
for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
iLoop++)
{
//This is where I get the exception
CurrentItem =
(Outlook.MailItemClass)CurrentFolder.Items[iLoop];
}
}
I've tried casting to MailItem, PostItem, Object, I've tried using
Folder.Items.GetFirst()... Nothing works. How do I get the item from
the MAPIFolder? Am I even supposed to be using the MAPIFolder?
I always get an exception: "Unable to cast COM object of type
'System.__ComObject' ........  failed due to the following error: No
such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."
I've looked up several code examples and none of them address this.
I'm using Microsoft.Office.Interop.Outlook v 11.0
Any help would be appreciated.
Tom P.

Is your mailbox on Exchnage?

I was not aware this was a factor. I have no idea.
If so, is it Exchange 2007 or 2010?

If so, Have you looked in to the Exchange Web Services Managed API?

No, I have not. I was under the impression that Outlook and it's
objects would provide the interface to Exchange. I thought that's what
they were.
If so, can I stop saying if so ?;)

If you are using a POP3 mailbox, see here:http://support.microsoft.com/kb/310244

This is a perfect example of my issue. I go to this link and look-up
the samples. One says to use the following snippet to loop through
items:

Outlook.MAPIFolder oContacts =
olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = oContacts.Items;
for (int i = 1; i <= oItems.Count; i++)
{
Outlook._ContactItem oContact = (Outlook._ContactItem)
oItems.Item(i);
Console.WriteLine(oContact.FullName);
oContact = null;
}


BUT when I declare an Outlook.Items variable (FolderItems) and then
try to use FolderItems.Item(iLoop) the compiler says there is no such
method.
Does it matter that I'm trying to get folders that are not Default
Folders? What if I want to look through the Personal Folders?
Also, for the Exchange Web Services, and other development issues, see here:http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopme...
I have not looked here yet, but I will.

Tom P.
 
T

Tom P.

Hello Tom P.,
I am trying to search an Outlook folder for certain e-mail items but I
am running into a problem getting items from the MAPIFolder object. I
use the following code but I can't find anything to cast the item to
that doesn't get an exception.
private List<SearchItem> SearchFolder(Outlook.MAPIFolder
CurrentFolder, string SearchString)
{
for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
iLoop++)
{
//This is where I get the exception
CurrentItem =
(Outlook.MailItemClass)CurrentFolder.Items[iLoop];
}
}
I've tried casting to MailItem, PostItem, Object, I've tried using
Folder.Items.GetFirst()... Nothing works. How do I get the item from
the MAPIFolder? Am I even supposed to be using the MAPIFolder?
I always get an exception: "Unable to cast COM object of type
'System.__ComObject' ........  failed due to the following error: No
such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."
I've looked up several code examples and none of them address this.
I'm using Microsoft.Office.Interop.Outlook v 11.0
Any help would be appreciated.
Tom P.
Is your mailbox on Exchnage?

I was not aware this was a factor. I have no idea.


If so, is it Exchange 2007 or 2010?
If so, Have you looked in to the Exchange Web Services Managed API?

No, I have not. I was under the impression that Outlook and it's
objects would provide the interface to Exchange. I thought that's what
they were.


If so, can I stop saying if so ?;)
If you are using a POP3 mailbox, see here:http://support.microsoft.com/kb/310244

This is a perfect example of my issue. I go to this link and look-up
the samples. One says to use the following snippet to loop through
items:

Outlook.MAPIFolder oContacts =
olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = oContacts.Items;
for (int i = 1; i <= oItems.Count; i++)
{
     Outlook._ContactItem oContact = (Outlook._ContactItem)
oItems.Item(i);
     Console.WriteLine(oContact.FullName);
     oContact = null;

}

BUT when I declare an Outlook.Items variable (FolderItems) and then
try to use FolderItems.Item(iLoop) the compiler says there is no such
method.
Does it matter that I'm trying to get folders that are not Default
Folders? What if I want to look through the Personal Folders?

The problem is with Personal Folders. If I don't use
GetDefaultFolders() then none of the items will cast to valid
MailItems.

So, how do I loop through Non-default folders?

Tom P.
 
M

Mel Weaver

Try

Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon(Missing.Value, Missing.Value, false, true);

Outlook.MAPIFolder oBox = inbox == true ?
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) :
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);

Outlook.Items oItems = oBox.Items;
Outlook.MailItem oMsg;
Object outlookObject;

for (int i = 1; i <= oItems.Count; i++)
{
outlookObject = i == 1 ? oItems.GetFirst() : oItems.GetNext();

oMsg = outlookObject as Outlook.MailItem;
if (oMsg != null)
{
// do whatever you need with oMsg
}

}




Tom P. said:
Hello Tom P.,


I am trying to search an Outlook folder for certain e-mail items but I
am running into a problem getting items from the MAPIFolder object. I
use the following code but I can't find anything to cast the item to
that doesn't get an exception.
private List<SearchItem> SearchFolder(Outlook.MAPIFolder
CurrentFolder, string SearchString)
{
for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
iLoop++)
{
//This is where I get the exception
CurrentItem =
(Outlook.MailItemClass)CurrentFolder.Items[iLoop];
}
}
I've tried casting to MailItem, PostItem, Object, I've tried using
Folder.Items.GetFirst()... Nothing works. How do I get the item from
the MAPIFolder? Am I even supposed to be using the MAPIFolder?
I always get an exception: "Unable to cast COM object of type
'System.__ComObject' ........ failed due to the following error: No
such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."
I've looked up several code examples and none of them address this.
I'm using Microsoft.Office.Interop.Outlook v 11.0
Any help would be appreciated.
Tom P.

Is your mailbox on Exchnage?

I was not aware this was a factor. I have no idea.
If so, is it Exchange 2007 or 2010?

If so, Have you looked in to the Exchange Web Services Managed API?

No, I have not. I was under the impression that Outlook and it's
objects would provide the interface to Exchange. I thought that's what
they were.
If so, can I stop saying if so ?;)

If you are using a POP3 mailbox, see
here:http://support.microsoft.com/kb/310244

This is a perfect example of my issue. I go to this link and look-up
the samples. One says to use the following snippet to loop through
items:

Outlook.MAPIFolder oContacts =
olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = oContacts.Items;
for (int i = 1; i <= oItems.Count; i++)
{
Outlook._ContactItem oContact = (Outlook._ContactItem)
oItems.Item(i);
Console.WriteLine(oContact.FullName);
oContact = null;
}


BUT when I declare an Outlook.Items variable (FolderItems) and then
try to use FolderItems.Item(iLoop) the compiler says there is no such
method.
Does it matter that I'm trying to get folders that are not Default
Folders? What if I want to look through the Personal Folders?
Also, for the Exchange Web Services, and other development issues, see
here:http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopme...
I have not looked here yet, but I will.

Tom P.
 
Ad

Advertisements

T

Tom P.

I don't know where I found it but I saw somebody suggest a
GC.Collect() to clean up the RPC handles (or something).

Believe it or not - It Worked!

I think it had to do with filling the ComboBox without releasing the
resources, but one little GC.Collect() and it's casting it MailItem
just like it's supposed to.

Thanks for the help guys.

Tom P.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top