【问题标题】:Is it possible to access redemption method of outlook add in using windows services是否可以使用windows服务访问outlook add in的兑换方法
【发布时间】:2022-04-28 09:03:44
【问题描述】:

我在 C# 中创建了 Outlook 插件,以使用兑换(背景)将邮件主题存储在 SQLite 数据库中。我们可以在 windows 服务中访问兑换方法/outlook 的类吗?

无法从服务中使用 Outlook 对象模型。

【问题讨论】:

    标签: c# windows-services outlook-addin outlook-redemption


    【解决方案1】:

    不要在服务中使用 Outlook 对象模型。其次,您假设文件夹中只有 ContactItem 对象,如果那里有分发列表,您的代码将会中断。

    RDO family of Redemption objects 可以在服务中使用

    【讨论】:

      【解决方案2】:

      您是对的,您不应该使用 Windows 服务中的 Outlook 对象模型。 Considerations for server-side Automation of Office 文章声明如下:

      Microsoft 目前不推荐也不支持从任何无人值守的非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化 Microsoft Office 应用程序,因为 Office在此环境中运行 Office 时可能会出现不稳定的行为和/或死锁。

      如果您要构建在服务器端上下文中运行的解决方案,您应该尝试使用已确保无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。此外,您将在整体解决方案的稳定性方面承担风险。

      Redemption 是对低级 API(扩展 MAPI)的封装。但加载项是 Office 应用程序的一项功能。扩展的 MAPI 对它们一无所知。因此,Extended MAPI 周围的任何包装器(在您的情况下为 Redemption)都不提供对加载项的访问。

      考虑对您的加载项使用任何其他通信方法,例如 - .NET Remoting (WCF)。您可以将托管加载项视为常规 .Net 应用程序。

      【讨论】:

      • 我们可以使用 Microsoft.Office.Interop.Outlook 来执行它;使用 OutLook = Microsoft.Office.Interop.Outlook;
      • 不推荐也不支持从 Windows 服务运行代码的方式。
      • 在服务中使用 OOM 时的“工作”部分往往会在最不合适的时候中断。就像将解决方案部署到生产环境时一样。
      • 可能在你的机器上工作,但它可能在以后任何不方便的时候被打破......
      【解决方案3】:
      using Microsoft.Office.Interop.Outlook;
      using OutLook = Microsoft.Office.Interop.Outlook;
      

      代码将是

       object missing = System.Reflection.Missing.Value;
              try
              {
                  OutLook.MAPIFolder fldContacts = null;;
                  OutLook._Application outlookObj = new OutLook.Application();
                  string folderName = "Default";
      
                  fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);
      
                  //LOOPIN G THROUGH CONTACTS IN THAT FOLDER.
                  foreach (Microsoft.Office.Interop.Outlook._ContactItem contactItem in fldContacts.Items)
                  {
      
                          StringBuilder strb = new StringBuilder();
                          strb.AppendLine((contactItem.FirstName == null) ? string.Empty : contactItem.FirstName);
                          strb.AppendLine((contactItem.LastName == null) ? string.Empty : contactItem.LastName);
                          strb.AppendLine(contactItem.Email1Address);
                          strb.AppendLine(contactItem.Business2TelephoneNumber);
                          strb.AppendLine(contactItem.BusinessAddress);
                          //write to text file
                          StreamWriter sw = null;
                          sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
                          sw.WriteLine(DateTime.Now.ToString() + ": " + strb.ToString());
                          sw.Flush();
                          sw.Close();
      
                  }
              }
              catch (System.Exception ex)
              {
                  throw new ApplicationException(ex.Message);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-11
        • 1970-01-01
        • 2012-01-04
        • 2012-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多