C# 使用Windows窗体创建新的outlook电子邮件
这可能是一个非常初级的问题。我正在尝试创建我的第一个Windows窗体应用程序,并希望通过单击窗体上的按钮来创建outlook电子邮件 问题是有13个错误,主要是: 严重性代码说明项目文件行抑制状态 错误CS0246找不到类型或命名空间名称“Outlook” (是否缺少使用指令或程序集引用?) 机器v.0.0.1 C:\Users\PC\source\repos\Offer机器v.0.0.1\Offer 机器v.0.0.1\Form1.cs 29激活 我已添加对我的项目的引用: 代码如下:C# 使用Windows窗体创建新的outlook电子邮件,c#,outlook,C#,Outlook,这可能是一个非常初级的问题。我正在尝试创建我的第一个Windows窗体应用程序,并希望通过单击窗体上的按钮来创建outlook电子邮件 问题是有13个错误,主要是: 严重性代码说明项目文件行抑制状态 错误CS0246找不到类型或命名空间名称“Outlook” (是否缺少使用指令或程序集引用?) 机器v.0.0.1 C:\Users\PC\source\repos\Offer机器v.0.0.1\Offer 机器v.0.0.1\Form1.cs 29激活 我已添加对我的项目的引用: 代码如下: u
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Offer_machine_v._0._0._1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
try
{
List<string> lstAllRecipients = new List<string>();
//Below is hardcoded - can be replaced with db data
lstAllRecipients.Add("sanjeev.kumar@testmail.com");
lstAllRecipients.Add("chandan.kumarpanda@testmail.com");
Outlook.Application outlookApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector = oMailItem.GetInspector;
// Thread.Sleep(10000);
// Recipient
Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients;
foreach (String recipient in lstAllRecipients)
{
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient);
oRecip.Resolve();
}
//Add CC
Outlook.Recipient oCCRecip = oRecips.Add("THIYAGARAJAN.DURAIRAJAN@testmail.com");
oCCRecip.Type = (int)Outlook.OlMailRecipientType.olCC;
oCCRecip.Resolve();
//Add Subject
oMailItem.Subject = "Test Mail";
// body, bcc etc...
//Display the mailbox
oMailItem.Display(true);
}
catch (Exception objEx)
{
Response.Write(objEx.ToString());
}
}
private void Label1_Click(object sender, EventArgs e)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间提供\u机器\u v.\u 0.\u 0.\u 1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
List lstAllRecipients=新列表();
//以下为硬编码-可替换为db数据
lstAllRecipients.Add(“sanjeev。kumar@testmail.com");
lstAllRecipients.Add(“chandan。kumarpanda@testmail.com");
Outlook.Application outlookApp=新建Outlook.Application();
Outlook.\u MailItem oMailItem=(Outlook.\u MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector=oMailItem.GetInspector;
//睡眠(10000);
//接受者
Outlook.Recipients oRecips=(Outlook.Recipients)oMailItem.Recipients;
foreach(lstAllRecipients中的字符串收件人)
{
Outlook.Recipient oRecip=(Outlook.Recipient)oRecips.Add(Recipient);
oRecip.Resolve();
}
//添加抄送
Outlook.Recipient oCCRecip=oRecips.Add(“THIYAGARAJAN。DURAIRAJAN@testmail.com");
oCCRecip.Type=(int)Outlook.OlMailRecipientType.olCC;
oCCRecip.Resolve();
//添加主题
omalitem.Subject=“测试邮件”;
//正文、密件抄送等。。。
//显示邮箱
oMailItem.Display(真);
}
捕获(异常对象)
{
Write(objEx.ToString());
}
}
私有无效标签1_单击(对象发送方,事件参数e)
{
}
}
}
您没有在代码中添加正确的用法。您需要添加:
using Microsoft.Office.Interop.Outlook;
如果没有这一行,您应该在互操作库中的每个对象之前键入完整的命名空间。使用该选项后,您可以在来自互操作的对象之前删除所有Outlook。
。但是,创建主应用程序对象的应用程序需要完整的命名空间,以避免与Winforms中定义的应用程序类发生冲突
Microsoft.Office.Interop.Outlook.Application outlookApp =
new Microsoft.Office.Interop.Outlook.Application();
_MailItem oMailItem = (_MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
Inspector oInspector = oMailItem.GetInspector;
..... and so on ....
您没有向代码中添加正确的using。您需要添加:
using Microsoft.Office.Interop.Outlook;
如果没有这一行,您应该在互操作库中的每个对象之前键入完整的命名空间。使用该选项后,您可以在来自互操作的对象之前删除所有Outlook。
。但是,创建主应用程序对象的应用程序需要完整的命名空间,以避免与Winforms中定义的应用程序类发生冲突
Microsoft.Office.Interop.Outlook.Application outlookApp =
new Microsoft.Office.Interop.Outlook.Application();
_MailItem oMailItem = (_MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
Inspector oInspector = oMailItem.GetInspector;
..... and so on ....
您似乎已将Outlook互操作添加到项目引用中两次 对于错误消息,您只需要向Outlook命名空间添加一个别名:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
此外,您可能会发现示例项目很有帮助 您似乎已经两次将Outlook互操作添加到项目引用中 对于错误消息,您只需要向Outlook命名空间添加一个别名:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
此外,您可能会发现示例项目很有帮助