2015-02-08 62 views
0

我在Outlook中設置了兩個郵箱。Interop Outlook - 從另一個郵箱發送約會

我將其稱爲「[email protected]」和「[email protected]」。

我想使用Interop創建併發送約會到特定的電子郵件地址日曆,而不僅僅是默認的Outlook帳戶。

using System; 
using System.Diagnostics; 
using System.Reflection; 
using Outlook = Microsoft.Office.Interop.Outlook; 

namespace Program 
{ 
    class Program 
    { 
     public static void Main(string[] args) 
     { 
       // Create the Outlook application. 
       Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application(); 

       Outlook.Account account = oApp.Session.Accounts["[email protected]"]; 

       // Get the NameSpace and Logon information. 
       Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi"); 

       // Log on by using a dialog box to choose the profile. 
       oNS.Logon(Missing.Value, Missing.Value, true, true); 

       // Create a new mail item. 
       Microsoft.Office.Interop.Outlook.MailItem oMsg =(Microsoft.Office.Interop.Outlook.MailItem) oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 

       // Set the subject. 
       oMsg.Subject = "test"; 

       // Set HTMLBody. 
       oMsg.HTMLBody = "test"; 

       oMsg.To = "[email protected]"; 
       //oMsg.CC = _cc; 
       //oMsg.BCC = _bcc; 

       oMsg.Save(); 
       oMsg.SendUsingAccount = account; 

       // Add a recipient. 
       //Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients; 

       // TODO: Change the recipient in the next line if necessary. 
       //Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(_recipient); 
       //oRecip.Resolve(); 

       // Send. 
       (oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send(); 

       // Log off. 
       oNS.Logoff(); 

       // Clean up. 
       //oRecip = null; 
       //oRecips = null; 
       oMsg = null; 
       oNS = null; 
       oApp = null; 
     } 
    } 
} 

此代碼完美的作品從我的電子郵件「[email protected]」自動發送電子郵件至「[email protected]」。

但是,我想自動創建特定電子郵件地址的預約/會議。

這是我目前的嘗試:

using System; 
using System.Diagnostics; 
using System.Reflection; 
using Outlook = Microsoft.Office.Interop.Outlook; 

namespace SendEventToOutlook 
{ 
    class Program 
    { 
     public static void Main(string[] args) 
     { 
      try 
      { 
       // Create the Outlook application. 
       Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application(); 

       Outlook.Account account = oApp.Session.Accounts["[email protected]"]; 

       // Get the nameSpace and logon information. 
       Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi"); 

       // Log on by using a dialog box to choose the profile. 
       oNS.Logon(Missing.Value, Missing.Value, true, true); 

       // Create a new Appointment item. 
       Microsoft.Office.Interop.Outlook.AppointmentItem appt = 
        (Microsoft.Office.Interop.Outlook.AppointmentItem) 
         oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem); 

       appt.Start = DateTime.Now; 
       appt.End = DateTime.Now.AddDays(7); 
       appt.Location = "Test"; 
       appt.Body = "Test"; 
       appt.AllDayEvent = false; 
       appt.Subject = "Test"; 

       appt.Save(); 
       appt.SendUsingAccount = account; 

       // Log off. 
       oNS.Logoff(); 

       appt = null; 
       oNS = null; 
       oApp = null; 
      } 
      catch (Exception ex) 
      { 
       Debug.WriteLine("The following error occurred: " + ex.Message); 
      } 
     } 
    } 
} 

此代碼成功地創建約會,但它一直對「[email protected]」創建一個約會,而不是「[email protected]」,這

Outlook.Account account = oApp.Session.Accounts["[email protected]"]; 

然後

appt.SendUsingAccount = account; 
:正如我所指定的發送賬戶是「 [email protected]」從線不應該發生

這是我在Outlook中設置的兩個電子郵件地址:http://i.imgur.com/0eopV8A.png

這兩個電子郵件地址具有不同的用戶名,並且來自不同的域/郵件服務器,如截圖所示。

任何人都可以看到我正在做的問題,或者如果有不同的解決方案?

謝謝。

+0

你有兩個帳戶設立在單個郵件配置文件?或者每個帳戶有兩個單獨的文件夾? – 2015-02-09 11:09:10

+0

我不太確定你的意思或如何檢查。如果有幫助,這是我在Outlook中設置的兩個電子郵件地址:http://i.imgur.com/0eopV8A.png 這兩個電子郵件地址具有不同的用戶名,並且來自不同的域/郵件服務器,如顯示在該屏幕截圖中。 希望有所幫助。 – Matthew 2015-02-09 23:39:50

+0

此外,這裏是如何在控制面板 - >郵件(Microsoft Outlook 2013) - >電子郵件帳戶...中顯示電子郵件地址:http://i.imgur.com/mPTq3ll.png – Matthew 2015-02-09 23:48:35

回答

0

目前還不清楚您是否在單個郵件配置文件或單獨的配置文件中設置了兩個帳戶。

AppointmentItem類的SendUsingAccount屬性允許設置一個Account對象,該對象表示AppointmentItem要發送到的帳戶。因此,SendUsingAccount屬性可用於指定在調用Send方法時應用於發送AppointmentItem的帳戶。這不是你想要的,我想。

無論如何,您可以使用Store類的GetDefaultFolder方法,它返回一個Folder對象,該對象表示存儲中的默認文件夾,並且該Folder對象的類型由FolderType參數指定。此方法與NameSpace對象的GetDefaultFolder方法類似。區別在於此方法獲取與帳戶關聯的交付存儲的默認文件夾,而NameSpace.GetDefaultFolder返回當前配置的默認存儲的默認文件夾。

因此,您可以獲取所需帳戶的日曆文件夾並在其中添加新的約會。

您可能會發現在MSDN有用下面的文章: