c#使用Outlook dll发送电子邮件

本文关键字:电子邮件 dll 使用 Outlook | 更新日期: 2023-09-27 18:07:08

在我的应用中,我需要发送电子邮件。我不能使用smtp,也没有选择以正常方式安装MS Outlook。我试着,

private Microsoft.Office.Interop.Outlook.Application oApp;
private Microsoft.Office.Interop.Outlook._NameSpace oNameSpace;
private Microsoft.Office.Interop.Outlook.MAPIFolder oOutboxFolder;
oApp = new Outlook.Application();
oNameSpace = oApp.GetNamespace("MAPI");
oNameSpace.Logon(null, null, true, true);
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.
    CreateItem(Outlook.OlItemType.olMailItem);
    oMailItem.To = toValue;
    oMailItem.Subject = subjectValue;
    oMailItem.Body = bodyValue;
    oMailItem.Send();

如果在机器上安装并运行Office 2010,则此代码可以正常工作。但我需要知道哪些数据被引用了。有可能只从Outlook中获取所需的dll并使用它们发送电子邮件吗?

Thanks in advance

c#使用Outlook dll发送电子邮件

根据注释,介绍如何使用Exchange Web Services通过Exchange服务器发送电子邮件的示例。大部分信息可从以下链接复制到答案中保存。

创建电子邮件消息并发送的示例(在用户的sent items文件夹中有一份副本)

// Create an email message and identify the Exchange service.
EmailMessage message = new EmailMessage(service);
// Add properties to the email message.
message.Subject = "Interesting";
message.Body = "The merger is finalized.";
message.ToRecipients.Add("user1@contoso.com");
// Send the email message and save a copy.
message.SendAndSaveCopy();

关于创建的更多代码在这里

稍微复杂一点的是上面代码中使用的服务变量的实例化。在这里可以找到

ExchangeService service = new ExchangeService();
service.Credentials = new WebCredentials("user1@contoso.com", "password");
service.AutodiscoverUrl("user1@contoso.com");

将尝试从电子邮件地址自动发现交换服务的url。然而,值得注意的是,除非附加一个回调方法来验证Exchange默认使用的自签名证书,否则对该服务的调用将失败。更多信息在这里

关于如何连接到交换服务、发送电子邮件、创建会议和日历请求有大量的信息。我还没有亲自测试以上所有内容,但可能会给您一个不错的开始。