【问题标题】:Trying to Programmatically Create & Open a new Outlook Email尝试以编程方式创建和打开新的 Outlook 电子邮件
【发布时间】:2014-07-04 19:56:31
【问题描述】:

我有一个 winforms 应用程序,我正在尝试创建一个方法来创建和打开一个新的 Outlook 电子邮件。到目前为止我有

private void CreateOutlookEmail()
    {
        try
        {                
            Outlook.MailItem mailItem = (Outlook.MailItem)
                this.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = "This is the subject";
            mailItem.To = "someone@example.com";
            mailItem.Body = "This is the message.";
            mailItem.Importance = Outlook.OlImportance.olImportanceLow;
            mailItem.Display(false);
        }
        catch (Exception eX)
        {
            throw new Exception("cDocument: Error occurred trying to Create an Outlook Email"
                                + Environment.NewLine + eX.Message);
        }
    }

但“CreateItem”引用带有错误消息下划线

"不包含 CreateItem 的定义"

我认为“CreateItem”是 MS Office 项目的标准方法,但不可否认,我确实在另一个网站上找到了上述代码并简单地复制了它。

请问我误会了什么?

【问题讨论】:

  • 你把这个方法放在哪里? IE。对this 的引用是否位于 VSTO Outlook 插件项目中?我怀疑您需要创建一个新的 Outlook 插件项目,然后将此代码放入该项目中。

标签: c# winforms outlook


【解决方案1】:

考虑一下。您正在对 this 当前对象调用 CreateItem 方法。你在这个类中定义了CreateItem方法吗?

代替你的:

Outlook.MailItem mailItem = (Outlook.MailItem) this.CreateItem(Outlook.OlItemType.olMailItem);

你需要这些行:

Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem) outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

您创建一个 Outlook 应用程序的实例,您可以在该实例上调用 CreateItem 方法。

编辑

还有两件事可以使它正常工作。

1) 将Microsoft.Office.Interop.Outlook 包的引用添加到您的项目中

2) 确保您的班级中有适当的 using 语句

using Outlook = Microsoft.Office.Interop.Outlook;

【讨论】:

  • 我已建议对 MSDN 页面进行此编辑,您可能从该页面复制了代码。
【解决方案2】:

试试这个

string subject = "My subject";
string emailTag = string.Format("mailto:someone@test.com?subject={0}", subject);
System.Diagnostics.Process.Start(emailTag);

【讨论】:

  • 嗨,这在我的本地 PC 上很有效,但我假设这需要在服务器上运行的 Office 应用程序/实例才能工作,因为当从基于远程服务器运行时它不会打开 Outlook 2007网站。我是正确的还是我需要做些什么才能让它在 Live 网站上运行,比如在我的 WebConfig 中引用它等?谢谢
  • 永远不要在远程服务器上使用 Office Interop。根据 Microsoft (support.microsoft.com/en-us/help/257757/…):“Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为当 Office 在此环境中运行时,Office 可能会表现出不稳定的行为和/或死锁。”
【解决方案3】:

根据我的经验,Office.Interop 可能会很麻烦,只需使用适当的参数启动 Outlook 可能是一种更简单、更便携的选择:

System.Diagnostics.Process.Start("C:\\Program Files (x86)\\Microsoft Office\\Office12\\OUTLOOK.EXE", "/c ipm.note /m name@address.com"));

Outlook 命令行开关为您提供更多选项以及众多信息来源(试试http://www.outlook-tips.net/how-to/using-outlook-command-lines

【讨论】:

  • 有一个原因,正如我最近在我自己的 winforms 应用程序中发现的那样,这是一个坏主意:事实证明,有一个限制,基于字符,你可以放入多少以这种方式发送电子邮件。从 Winforms 世界的角度来看,Interop 实际上是最顺畅的方式。
猜你喜欢
  • 2012-03-27
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
相关资源
最近更新 更多