尝试以编程方式创建 &打开新的 Outlook 电子邮件 [英] Trying to Programmatically Create & Open a new Outlook Email

查看:26
本文介绍了尝试以编程方式创建 &打开新的 Outlook 电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a winforms application and I am trying to create a method that will create and open a new Outlook Email. So far I have

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"引用带有错误消息下划线

But the 'CreateItem' reference is underlined with the error message

不包含 CreateItem 的定义"

"does not contain a definition for CreateItem"

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

I thought 'CreateItem' was a standard method for MS Office items, but admittedly I did find the above code on another website and simply copied it.

请问我有什么误解?

推荐答案

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

Think about it. You are calling the CreateItem method on this current object. Have you defined the CreateItem method in this class?

而不是你的:

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 方法.

You create an instance of the outlook application, on which you can call the CreateItem method.

还有两件事可以使这项工作正常进行.

There are two more things to make this work properly.

1) 向您的项目添加对 Microsoft.Office.Interop.Outlook 包的引用

1) Add a reference to the Microsoft.Office.Interop.Outlook package to your project

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

2) Ensure you have the appropriate using statement in your class

using Outlook = Microsoft.Office.Interop.Outlook;

这篇关于尝试以编程方式创建 &打开新的 Outlook 电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆