【发布时间】: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)) .'
【问题讨论】:
-
不,我没有回答我需要 MailItem 保存在 SQL 服务器中
标签: c# outlook casting com-interop office-automation