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