使用C#模拟Outlook发送邮件,代码编译报错

添加OutLook API

1 using OutLook = Microsoft.Office.Interop.Outlook;

发送邮件方法

 1 public void SendEmail()
 2 {
 3     OutLook.Application app = new OutLook.Application();
 4     OutLook.NameSpace ns = app.GetNamespace("mapi");
 5     ns.Logon("ServerName\\UserName", "Password", false, true);
 6     OutLook.MailItem message = (OutLook.MailItem)app.CreateItem(OutLook.OlItemType.olMailItem);
 7     message.Subject = "subject";
 8     message.To = "account@XXX.com";
 9     message.Body = "Hello World!";
10     message.Display(true);
11     message.Send();
12     ns.Logoff();
13 }

编译报错如下:

方法“Microsoft.Office.Interop.Outlook._MailItem.Send()”和非方法“Microsoft.Office.Interop.Outlook.ItemEvents_10_Event.Send”之间存在二义性。将使用方法组。

将上述代码片段中的第6行修改成如下:

1 OutLook._MailItem message = (OutLook.MailItem)app.CreateItem(OutLook.OlItemType.olMailItem);

问题解决!

posted @ 2016-12-23 17:36  天琊蓝  阅读(781)  评论(2编辑  收藏  举报