【问题标题】:Save outlook sent mail folder in sql在sql中保存outlook发送的邮件文件夹
【发布时间】:2022-02-17 08:18:33
【问题描述】:

我正在尝试在 sql 中保存已发送的邮件文件夹数据,但是我收到类型错误的 cast 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:“无法将“System.__ComObject”类型的 COM 对象转换为接口类型“Microsoft.Office.Interop.Outlook.MailItem”。此操作失败,因为 IID 为“{00063034-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)) .'

【问题讨论】:

标签: c# outlook casting com-interop office-automation


【解决方案1】:

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

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

【讨论】:

  • 感谢您的帮助,我已经解决了,但是现在我正在尝试从 Outlook 中提取附件文件并保存在 sql 中,您知道吗?
  • 遍历附件集合,并为每个附件调用 Attachment.SaveAsFile。然后,您可以将文件插入 SQL。
【解决方案2】:

事实上,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

   }
}

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

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

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 2014-04-14
    • 2020-09-06
    • 1970-01-01
    • 2016-10-02
    • 2014-11-05
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多