Click here to Skip to main content
15,850,917 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I am using this code for reading emails but i got an error. Please give me any suggestions.
C#
Microsoft.Office.Interop.Outlook.Application app = null;
               Microsoft.Office.Interop.Outlook._NameSpace ns = null;
               Microsoft.Office.Interop.Outlook.PostItem item = null;
               Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
               Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

               try
               {
                 app = new Microsoft.Office.Interop.Outlook.Application();
                 ns = app.GetNamespace("MAPI");
                 ns.Logon(null,null,false, false);

                 inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                 subFolder = inboxFolder.Folders["Test"]; //folder.Folders[1]; also works
                 Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
                 Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

                 for(int i=1;i<=subFolder.Items.Count;i++)
                 {
                   item = (Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];
                   Console.WriteLine("Item: {0}", i.ToString());
                   Console.WriteLine("Subject: {0}", item.Subject);
                   Console.WriteLine("Categories: {0}", item.Categories);
                   Console.WriteLine("Body: {0}", item.Body);
                   Console.WriteLine("HTMLBody: {0}", item.HTMLBody);
                 }
               }
               catch (System.Runtime.InteropServices.COMException ex)
               {
                 Console.WriteLine(ex.ToString());
               }
               finally
               {
                 ns = null;
                 app = null;
                 inboxFolder = null;
               }





******** error is
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.PostItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063024-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Posted
Updated 20-Apr-16 2:53am
v3

 
Share this answer
 
I never recommend Office.Interop and they are highly complicated and MS doesnt support the way you like to use it. Thats not the purpose of Office Interop Assemblies
Steps
1. Download and install the EWS assemblies from Exchange Web Services Managed API 1.2 and install the target version either 32 or 64. Please remember to note the directory which it gets installed to for it is needed later. Of note is that the install directory contains an excellent document Getting Started which has examples that show the capabilities of the web services.
2. Create your project in Studio, the below example C# code targets a Console Application.
3. Add a reference to Microsoft.Exchange.WebServices by browsing to the directory loaded in step 1 and selecting the Microsoft.Exchange.WebServices.dll. Remember to add using Microsoft.Exchange.WebServices.Data; in your code.
4. Add these lines of code to initialize the web service:

C#
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
 
//service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
 
service.AutodiscoverUrl( "First.Last@MyCompany.com" );



o Line 1 : Create an Exchange Web Service instance specifying that we are targeting a specific version of Exchange.
o Line 3 : You may need to have a service account do your dirty work and this line uncommented out is your key to doing that.
o Line 5 : We don’t specify an Exchange server, but auto discover it. The server may be clustered and this will get the best one for the job. We also specify the account’s mailbox to use.
5. Once the service is up and running we can now get the emails found in the box. Here is how we do it:

C#
FindItemsResults<item> findResults = service.FindItems(
   WellKnownFolderName.Inbox,
   new ItemView( 10 ) );
 
foreach ( Item item in findResults.Items )
   Console.WriteLine( item.Subject );</item>


Line 1 : We call the service to find out about the mailbox.
Line 2 : We are interested in the inbox only, but we could be interested in the calendar. This is where to specify those items.
Line 3 : We only want 10 items…change that number for different results
Line 5: Print out the items, we are only going to show the subject but there are other header items we could show.
Run the program. If everything goes alright then we have gotten the top ten items of the mailbox.
That should get you started. Again check the Getting Started document which the install dropped into the install folder for more examples!
 
Share this answer
 
this is very simple
just replace

PostItem

with

MailItem


wherever you found this and error will gone.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900