C# 尝试以编程方式创建和打开新的 Outlook 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11242745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:06:24  来源:igfitidea点击:

Trying to Programmatically Create & Open a new Outlook Email

c#winformsoutlook

提问by PJW

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

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

private void CreateOutlookEmail()
    {
        try
        {                
            Outlook.MailItem mailItem = (Outlook.MailItem)
                this.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = "This is the subject";
            mailItem.To = "[email protected]";
            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);
        }
    }

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

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

"does not contain a definition for CreateItem"

“不包含 CreateItem 的定义”

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.

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

What am I misunderstanding please?

请问我有什么误解?

采纳答案by luketorjussen

Think about it. You are calling the CreateItemmethod on thiscurrent object. Have you defined the CreateItemmethod in this class?

想想看。您正在调用当前对象CreateItem上的方法this。你CreateItem在这个类中定义了方法吗?

Instead of your:

而不是你的:

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

You need the lines:

您需要以下几行:

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

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

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

Edit

编辑

There are two more things to make this work properly.

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

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

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

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

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

using Outlook = Microsoft.Office.Interop.Outlook;

回答by JohnnBlade

Try this

尝试这个

string subject = "My subject";
string emailTag = string.Format("mailto:[email protected]?subject={0}", subject);
System.Diagnostics.Process.Start(emailTag);

回答by user3805384

In my experience Office.Interop can be troublesome and simply kicking off Outlook with the appropriate arguments may represent a simpler and more portable option:

根据我的经验,Office.Interop 可能很麻烦,简单地使用适当的参数启动 Outlook 可能代表一个更简单、更便携的选项:

System.Diagnostics.Process.Start("C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE", "/c ipm.note /m [email protected]"));

Outlook command line switches give you many more options with numerous sources of info (try http://www.outlook-tips.net/how-to/using-outlook-command-lines)

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