title: Save outlook sent mail folder in sql
body: <p>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.</p>
  <pre><code>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(&quot;MAPI&quot;);
              
              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(@&quot;Data Source=\SQLEXPRESS; initial catalog=EmailReply;Integrated Security=True&quot;);
                   
                      SqlCommand cmd = new SqlCommand(&quot;INSERT INTO Emails (SenderName, Subject, Body) VALUES (@SenderName, @Subject, @Body)&quot;, con);
                      //cmd.CommandType = CommandType.StoredProcedure;
                      cmd.Parameters.AddWithValue(&quot;@SenderName&quot;, oMailItem.SenderName);
                      cmd.Parameters.AddWithValue(&quot;@Subject&quot;, oMailItem.Subject);
                      cmd.Parameters.AddWithValue(&quot;@Body&quot;, oMailItem.Body);
                      //cmd.ExecuteNonQuery();
  
                      con.Open();
                      int k = cmd.ExecuteNonQuery();
                      if (k != 0)
                      {
                          Console.WriteLine(&quot;Record Inserted Succesfully into the Database&quot;);
  
                      }
                      con.Close();
                  }
              }
          }
      }
  }
  </code></pre>
  <p>Error:</p>
  <blockquote>
  <p>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)).'</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/TT33k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TT33k.png" alt="enter image description here" /></a></p>