簡體   English   中英

C#使用Outlook dll發送電子郵件

[英]C# send email using Outlook dlls

在我的應用程序中,我需要發送電子郵件。 我不能使用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,則此代碼可以很好地工作。 但是我需要找出正在引用的dll。 是否可以僅從Outlook獲取必需的dll並使用它們發送電子郵件?

提前致謝

根據評論,提供有關如何使用Exchange Web服務通過Exchange服務器發送電子郵件的示例。 可從以下鏈接復制到答案中的大多數信息,以進行保存。

創建並發送電子郵件的示例(在用戶的“已發送郵件”文件夾中帶有副本)

// 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");

它將嘗試從電子郵件地址自動發現交換服務的網址。 但是,值得注意的是,除非您附加一個回調方法來驗證Exchange默認使用的自簽名證書,否則對該服務的調用將失敗。 更多信息在這里

有關如何連接到交換服務,發送電子郵件,創建會議和日歷請求的大量信息。 我還沒有親自測試以上所有內容,但可能為您提供了不錯的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM