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
 
How do you know that "RetrieveMail" is a child of oFolder[1]?
 
What you mean by "still not working"? You mentioned no error, but what new behavior are you seeing?

When you step through the code what is happening? Are items being found? How exactly are you getting the Inbox? Are you using the GetDefaultFolder or are you making the same assumptions about indexes like in your code in post #1?
 
Yes no parsing of data. I tried to debug and its not passing through the sql connection

I tried this code using GetDefaultFolder

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.MAPIFolder oFolderIn = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

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

                    SqlConnection con = new SqlConnection(@"Data Source=TCLS-DT0052\SQLEXPRESS; initial catalog=EmailReply;Integrated Security=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();
                }
            }
        }
    }
}
 

Attachments

  • RetrieveEmail.gif
    RetrieveEmail.gif
    486 KB · Views: 85
From that animated GIF it looks like the oMailItem.SenderName is not "SenderName", but there were items in the Inbox. If you hover over oMailItem.SenderName, you would see the actual value that was found.
 
How many times I tried, it passed through the Send but doesn't really retrieving the data in sql

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.MAPIFolder oFolderIn = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);


            //Outlook.Application oLk = new Outlook.Application();
            //Outlook._NameSpace olNS = oLk.GetNamespace("MAPI");
            //Outlook.MAPIFolder rootFolder = oLk.Session.DefaultStore.GetRootFolder();
            //Outlook.MAPIFolder folder = rootFolder.Folders["RetrieveMail"];
            //Console.WriteLine(folder.Items.Count);

            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");
                    SqlCommand cmd = new SqlCommand("INSERT INTO Emails (SenderName, Subject, Body, Date) VALUES (@SenderName, @Subject, @Body, @Date)", 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();
                }
            }
        }
    }
}
 
Step through with the debugger. Are you making it to line 41? If not, then the issue is still your filter on line 38. What is the value of oMailItem.SenderName? To get from line 38 to line 41, the condition on line 38 needs to be true.
 
And what is the value? As I said step through the code with a debugger. Hover over the property SenderName when you get to that line and examine the value.
 
Hello @Skydiver thank for the help, I already resolve my problem the inbound outlook email and save on SQL however I'm trying to create an email reply table that retrieve email reply data to SQL, Any ideas?
 
Last edited:
Please start another thread. Describe in detail what you are trying to do. Show us the code that you are using to accomplish your goal.
 
It is still puzzling for me why you want to do this on the client side when your Exchange Admin can get you the same information using their admin console.
 
Back
Top Bottom