试图以编程方式创建和放大器;打开一个新的Outlook电子邮件 [英] Trying to Programmatically Create & Open a new Outlook Email
问题描述
我有一个WinForms应用程序,我试图创建一个将创建并打开一个新的Outlook电子邮件的方法。到目前为止,我有
私人无效CreateOutlookEmail()
{
试
{
Outlook.MailItem的MailItem =(Outlook.MailItem)
this.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject =这是主题;
mailItem.To =someone@example.com;
mailItem.Body =这是消息。
mailItem.Importance = Outlook.OlImportance.olImportanceLow;
mailItem.Display(假);
}
赶上(异常前)
{
抛出新的异常(CDocument的:出现试图创建Outlook电子邮件错误
+ Environment.NewLine +实施例。信息);
}
}
但createItem中引用强调了与错误消息
不包含createItem中的定义,
我想'createItem中是一种标准方法微软Office项目,但无可否认我没有找到其他网站对上面的代码,并简单地复制它。
我是什么误解吗?
< DIV CLASS =h2_lin>解决方案
想想吧。您正在呼吁的
当前对象。你有没有在这个类中定义的 createItem中
该方法 createItem中
法
而不是你的?
Outlook.MailItem的MailItem =(Outlook.MailItem)this.CreateItem(Outlook.OlItemType.olMailItem);
您需要的行:
Outlook.Application outlookApp =新Outlook.Application();
Outlook.MailItem的MailItem =(Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
您创建Outlook应用程序,可以在其上调用的一个实例createItem中
方法。
修改
有两件事,使这项工作正常。
1)添加到 Microsoft.Office.Interop.Outlook
包引用您的项目
2)
使用确保您有using语句适当的在你的类展望= Microsoft.Office.Interop.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);
}
}
But the 'CreateItem' reference is underlined with the error message
"does not contain a definition for 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.
What am I misunderstanding please?
Think about it. You are calling the CreateItem
method on this
current object. Have you defined the CreateItem
method in this class?
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 CreateItem
method.
Edit
There are two more things to make this work properly.
1) Add a reference to the Microsoft.Office.Interop.Outlook
package to your project
2) Ensure you have the appropriate using statement in your class
using Outlook = Microsoft.Office.Interop.Outlook;
这篇关于试图以编程方式创建和放大器;打开一个新的Outlook电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!