title: Save Outlook email Information to SQL Server
body: <p>I'm trying to save inbound Outlook mail data to sql server however I'm getting in error in System.Runtime.InteropServices.COMException: 'The attempted operation failed.  An object could not be found.' What did I missed</p>
  <p>Note: RetrieveMail is exist in mailbox</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._Folders oFolders;
              oFolders = olNS.Folders;
              Outlook.MAPIFolder oFolder;
              oFolder = oFolders[1];
              Outlook.MAPIFolder oFolderIn = oFolder.Folders[&quot;RetrieveMail&quot;];
  
              Outlook.Items oItems = oFolderIn.Items;
              foreach (Outlook.MailItem oMailItem in oFolderIn.Items)
              {
                  if (oMailItem.SenderName == &quot;sender_name&quot;)
                  {
  
                      SqlConnection con = new SqlConnection(@&quot;Data Source=\SQLEXPRESS; initial catalog=EmailReply;Integrated Security=True;User Instance=True&quot;);
                      SqlCommand cmd = new SqlCommand(&quot;Emails&quot;, con);
                      cmd.CommandType = CommandType.StoredProcedure;
                      cmd.Parameters.AddWithValue(&quot;Subject&quot;, oMailItem.Subject);
                      cmd.Parameters.AddWithValue(&quot;Body&quot;, oMailItem.Body);
  
                      con.Open();
                      int k = cmd.ExecuteNonQuery();
                      if (k != 0)
                      {
                          Console.WriteLine(&quot;Record Inserted Succesfully into the Database&quot;);
  
                      }
                      con.Close();
                  }
              }
          }
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ytSYJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ytSYJ.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/26QXu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/26QXu.png" alt="enter image description here" /></a></p>