Save Outlook email Information to SQL Server

Joined
Feb 2, 2022
Messages
15
Programming Experience
Beginner
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

Note: RetrieveMail is exist in mailbox

C#:
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._Folders oFolders;
            oFolders = olNS.Folders;
            Outlook.MAPIFolder oFolder;
            oFolder = oFolders[1];
            Outlook.MAPIFolder oFolderIn = oFolder.Folders["RetrieveMail"];

            Outlook.Items oItems = oFolderIn.Items;
            foreach (Outlook.MailItem oMailItem in oFolderIn.Items)
            {
                if (oMailItem.SenderName == "sender_name")
                {

                    SqlConnection con = new SqlConnection(@"Data Source=TCLS-DT0052\SQLEXPRESS; initial catalog=EmailReply;Integrated Security=True;User Instance=True");
                    SqlCommand cmd = new SqlCommand("Emails", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Subject", oMailItem.Subject);
                    cmd.Parameters.AddWithValue("Body", oMailItem.Body);

                    con.Open();
                    int k = cmd.ExecuteNonQuery();
                    if (k != 0)
                    {
                        Console.WriteLine("Record Inserted Succesfully into the Database");

                    }
                    con.Close();
                }
            }
        }
    }
}

1644439307688.png


1644439334289.png
 
I see. And so if the person is on vacation for 2 weeks, you'll be missing 2 weeks worth of data until that person logs back on and starts up Outlook. When they log on, then the SQL data will suddenly get populated. Additionally, what happens if the user creates a message handling rule that automatically moves those Power BI messages to another folder before your code has a chance to find them in the Inbox?

Anyway, it's your code and your user's or client's requirements. If that's what they want even after you let the know that there's a better way, c'est la vie.
 
I actually solved it I parse outlook inbox data to sql however what I'm working is to get the sent mail data but I'm getting this error.


C#:
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);
                     //cmd.ExecuteNonQuery();
    
                     con.Open();
                     int k = cmd.ExecuteNonQuery();
                     if (k != 0)
                     {
                         Console.WriteLine("Record Inserted Succesfully into the Database");
    
                     }
                     con.Close();
                 }
             }
         }
     }
 }



Note: I tried to online repair the Microsoft app and nothing happens still getting this error
1644887958366.png
 
Not all items in a folder will be a MailItem. You need to handle those cases (or manually scrub the folders so that all items will be MailItems).
 

Latest posts

Back
Top Bottom