2009-09-18 49 views
0

我正在尋找最基本的完整示例,以通過C#.NET啓動的新消息啓動Outlook 2007(我碰巧使用VS2008 .NET 3.0)從C#.NET開始新的Outlook 2007消息

這裏就是我想:

using System; 
using System.Collections.Generic; 
using System.Text; 
using Microsoft.Office.Interop.Outlook; 

namespace CreateMessage { 

    class Program { 

     static void Main (string[] args) { 

      // Create outlook application object. 
      var outlookApplication = new Microsoft.Office.Interop.Outlook.Application(); 

      // Create mail message. 
      var newMail = (Microsoft.Office.Interop.Outlook.MailItem)outlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 
      newMail.To = "[email protected]"; 
      newMail.Subject = "Example"; 
      newMail.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML; 
      newMail.HTMLBody = "<p>Dear Example,</p><p>Example example.</p>"; 
      newMail.Display(false); 

     } 

    } 
} 

這裏是我的項目refereces:

alt text http://img4.imageshack.us/img4/9350/referencesi.jpg

這裏的間歇例外,我得到(大約一半的時間我運行程序):

 
System.Runtime.InteropServices.COMException was unhandled 
    Message="The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))" 
    Source="Interop.Microsoft.Office.Interop.Outlook" 
    ErrorCode=-2147417846 
    StackTrace: 
     at Microsoft.Office.Interop.Outlook._MailItem.set_To(String To) 
     at CreateMessage.Program.Main(String[] args) in C:\Users\Adam\Projects\GGS\CreateMessage\Program.cs:line 17 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

注:我發現很多答案和實例關於通過互操作與Outlook的工作,但大部分的答案沒有顯示using語句或試圖從我拍攝的東西開始,在下一個階段做事。我只是試圖讓Outlook彈出窗口,開始一條新消息,並且可能會填充一些信息(即Subject,To等)。複製和粘貼到目前爲止我發現的任何示例都不會編譯,因爲它們是片段,而不是編譯完整功能的準系統示例。

謝謝!

回答

1

我不確定你的問題,但是你顯示的錯誤信息表明你還沒有添加程序集。爲此,請展開您的項目,然後右鍵單擊參考,然後選擇「添加參考...」。然後進入'COM'選項卡(在VS2008上)並找到「Microsoft Outlook 12.0 Object Library」(根據安裝的Outlook版本不同,具體版本可能會不同)突出顯示它,然後單擊OK。我認爲這應該清除你的錯誤,並且會讓你走上正確的道路。

+0

謝謝邁克爾。我添加了「Microsoft Outlook 12.0 Object Library」,但仍然收到相同的編譯錯誤消息:「無法找到類型或命名空間名稱」Outlook「(您是否缺少使用指令或程序集引用?)」 – 2009-09-18 20:00:31

+0

我從你的編輯推斷,你解決了編譯問題...至於你的問題,當我用你的代碼創建一個新的C#項目(我所做的只是剪切/粘貼),它工作得很好......應用程序加載和後面它會打開一個包含特定參數的Outlook電子郵件。也許你應該重新開始,以防你在之前的嘗試中搞砸了某些東西。 – 2009-09-18 21:17:01

+0

謝謝你的幫助邁克爾。我確實在那裏有Interop引用,但是使用完全限定名稱空間超過了我的初始錯誤。我現在遇到的問題是我更新的問題中顯示的異常。新消息似乎在Outlook準備就緒之前開始啓動,並引發「應用程序繁忙」異常。 (這隻發生了大約一半的時間,另一半時間,樣本成功執行並生成郵件。) – 2009-09-19 05:18:01