Save outlook sent mail folder in sql

ika palad 86 Reputation points

I'm trying to save out sent mail folder data in sql however I'm getting cast com object of type error/ I tried to online repair the Microsoft app and nothing happens.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Reflection; // to use Missing.Value  
//using Microsoft.Office.Interop.Outlook;  
using System.Data.SqlClient;  
using System.Data;  
using Outlook = Microsoft.Office.Interop.Outlook;  
namespace RetrieveEmail  
    public class Program  
         static void Main(string[] args)  
            Outlook.Application oLk = new Outlook.Application();  
            Outlook._NameSpace olNS = oLk.GetNamespace("MAPI");  
            Outlook.MAPIFolder oFolderIn = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);  
            Outlook.Items oItems = oFolderIn.Items;  
            foreach (Outlook.MailItem oMailItem in oFolderIn.Items)  
                if (oMailItem.SenderName != null)  
                    SqlConnection con = new SqlConnection(@"Data Source=TCLS-DT0052\SQLEXPRESS; initial catalog=EmailReply;Integrated Security=True");  
                    SqlCommand cmd = new SqlCommand("INSERT INTO Emails (SenderName, Subject, Body) VALUES (@SenderName, @Subject, @Body)", con);  
                    //cmd.CommandType = CommandType.StoredProcedure;  
                    cmd.Parameters.AddWithValue("@SenderName", oMailItem.SenderName);  
                    cmd.Parameters.AddWithValue("@Subject", oMailItem.Subject);  
                    cmd.Parameters.AddWithValue("@Body", oMailItem.Body);  
                    int k = cmd.ExecuteNonQuery();  
                    if (k != 0)  
                        Console.WriteLine("Record Inserted Succesfully into the Database");  


System.InvalidCastException: '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: 0x80004002 (E_NOINTERFACE)).'


An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,493 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
3,730 questions
No comments
{count} votes

Accepted answer
  1. Eugene Astafiev 801 Reputation points

    The fact is that Outlook allows placing different kind if items into a folder. So, each time you iterate over all items you need to check the underlying type by using the is operator in C#:

    foreach (object item in oFolderIn.Items)
       if (item is Outlook.MailItem)
           // cast item to MailItem.
           Outlook.MailItem mailItem = item as Outlook.MailItem;
           // your code here for the mailItem object
       if (item is Outlook.MeetingItem)
           // cast item to MeetingItem.
           Outlook.MeetingItem meetingItem = item as Outlook.MeetingItem;
           // your code here for the meetingItem object

    If you need to process other item types in Outlook you need to add similar checks for each item type.

0 additional answers

Sort by: Most helpful