Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to send the mail using below code:
private void btn_Send_Click(object sender, EventArgs e)
       {
           try
           {
               Outlook._Application _app = new Outlook.Application();
               Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
               mail.To = txt_To.Text;
               mail.Subject = txt_Subject.Text;
               mail.Body = txt_Message.Text;
               mail.Importance = Outlook.OlImportance.olImportanceNormal;
               ((Outlook._MailItem)mail).Send();
               MessageBox.Show("Your Message has been successfully sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }


But i am facing an error while reading the mails from outlook, below is my code to read the mail:
private void btn_Receive_Click(object sender, EventArgs e)
       {
           try
           {
               Outlook._Application _app = new Outlook.Application();
               Outlook._NameSpace _ns = _app.GetNamespace("MAPI");
               Outlook.MAPIFolder inbox = _ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
               _ns.SendAndReceive(true);
               dt = new DataTable("Inbox");
               dt.Columns.Add("Subject", typeof(string));
               dt.Columns.Add("Sender", typeof(string));
               dt.Columns.Add("Body", typeof(string));
               dt.Columns.Add("Date", typeof(string));
               dataGrid.DataSource = dt;
               foreach (Outlook.MailItem item in inbox.Items)
               {
                   dt.Rows.Add(new object[] { item.Subject, item.SenderName, item.HTMLBody, item.SentOn.ToLongDateString() + "" + item.SentOn.ToLongDateString() });
               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }


Error is: Unable to cast COM object of type 'System._ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0*80004002 (E_NOINTERFACE)).

What I have tried:

After googling i found that we have to check If Item.Class=43, but when I try putting that If like: (if(item.Class=43)) condition, it shows a error stating 

Property or indexer '_MailItem.Class' cannot be assigned to -- it is read only 


Kindly help me to fix this.
Posted
Updated 14-Mar-18 6:28am
Comments
F-ES Sitecore 14-Mar-18 12:28pm    
It's two equals to compare

if(item.Class==43)

the code you copied from was vb.net which only uses one.

1 solution

Simple:
C#
if(item.Class=43)

Property or indexer '_MailItem.Class' cannot be assigned to -- it is read only
One equals is an assignment. two is a comparison. Try:
C#
if(item.Class == 43)
 
Share this answer
 
Comments
Member 13628387 15-Mar-18 2:31am    
There is a different error, It says:
Operator '==' cannot be applied to operands of type 'OlObjectClass' and 'int'
Richard Deeming 15-Mar-18 15:04pm    
if (item.Class == OlObjectClass.olMail)

OlObjectClass enumeration (Microsoft.Office.Interop.Outlook)[^]
OriginalGriff 15-Mar-18 15:15pm    
I must be losing my mind: I'm sure I replied to that this morning ... :confused:
Member 13628387 18-Mar-18 23:01pm    
Sorry but i am still getting this below error whenever i am trying to fetch all my mails from outlook imbox
Error is: Unable to cast COM object of type 'System._ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0*80004002 (E_NOINTERFACE)).
Member 13628387 18-Mar-18 23:19pm    
And there are 6037 emails in my outlook inbox but when I am debugging with the code it shows 1915 as the count in that for loop (foreach (Outlook.MailItem item in inbox.Items)). I really want to crack this code, I don't know how to fix this). Mainly I want to show all the emails from the outlook inbox in windows application(Grid) using c#

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