Home > Net >  Save Outlook email Information to SQL Server
Save Outlook email Information to SQL Server

Time:02-10

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

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();
                }
            }
        }
    }
}

enter image description here

enter image description here

CodePudding user response:

Use the NameSpace.GetDefaultFolder method to get the Inbox folder and then try to get the required subfolder instead. For example:

Sub DisplaySubfolderOfInboxFolder()
 Dim myNamespace As Outlook.NameSpace 
 Dim myInbox As Outlook.Folder 
 Dim myFolder As Outlook.Folder 
 
 Set myNamespace = Application.GetNamespace("MAPI")
 Set myInbox = myNamespace.GetDefaultFolder(olFolderInbox)
 Set myFolder = myInbox.Folders(1) 
 
 myFolder.Display 
 
End Sub

So, in your code it should look like that:

Outlook.Application oLk = new Outlook.Application();
Outlook._NameSpace olNS = oLk.GetNamespace("MAPI");
Outlook._Folders oFolders;
Outlook.MAPIFolder oFolder =  olNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);           
Outlook.MAPIFolder oFolderIn = oFolder.Folders["RetrieveMail"];

I've also found a possible pitfall in your code:

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

Be aware, a folder may contain different kind of items. So, I'd suggest using object instead of mail item. Then you may check the item type and cast the object to the underlying type (mail, appointment and etc.).

Another point is that iterating over all items in a folder is not really a good idea. Use the Find/FindNext mehtods of the Items class instead. Read more about these methods in the following articles:

  •  Tags:  
  • Related