2015-02-08 63 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有用下面的文章: