在 sql 中保存 outlook 已发送邮件文件夹

Save outlook sent mail folder in sql

我试图在 sql 中保存已发送的邮件文件夹数据,但是我得到了错误类型的 com 对象/我尝试在线修复 Microsoft 应用程序,但没有任何反应。

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

错误:

System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

您可以在“已发送邮件”文件夹中包含 MailItem 以外的项目类型 - 很可能您还有 MeetingItem 个对象。

foreach (object item in oFolderIn.Items)
{
  if (item is Outlook.MailItem oMailItem)
  {
      ...
  }
}

事实是 Outlook 允许将不同类型的项目放入文件夹中。因此,每次迭代所有项目时,您都需要使用 C# 中的 is 运算符检查基础类型:

foreach (object item in oFolderIn.Items)
{
   if (item is Outlook.MailItem)
   {
       // cast item to MailItem.
       Outlook.MailItem mailItem = item as Outlook.MailItem;
       // your code here for the mailItem object
   }
   if (item is Outlook.MeetingItem)
   {
       // cast item to MeetingItem.
       Outlook.MeetingItem meetingItem = item as Outlook.MeetingItem;
       // your code here for the meetingItem object

   }
}

因此,首先,您需要定义一个使用 as 对象而不是邮件项目的循环,这样您就可以遍历文件夹中的所有项目。然后你需要检查对象的底层类型。

请注意,如果您需要在 Outlook 中处理其他项目类型,您需要为每个项目类型添加类似的检查。