【问题标题】:Interop Outlook - Sending Appointment from another mailbox互操作 Outlook - 从另一个邮箱发送约会
【发布时间】:2015-04-08 16:00:24
【问题描述】:

我在 Outlook 中设置了两个邮箱。

我将它们称为“email1@mail.com”和“email2@mail.com”。

我想使用 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["email2@mail.com"];

                // 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 = "test@gmail.com";
                //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;
        }
    }
}

此代码可以完美地从我的电子邮件“email2@mail.com”自动向“test@gmail.com”发送电子邮件。

但是,我想为特定的电子邮件地址自动创建约会/会议。

这是我目前的尝试:

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["email2@mail.com"];

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

此代码确实成功创建了约会,但它不断为“email1@mail.com”而不是“email2@mail.com”创建约会,这不应该发生,因为我已指定发送帐户为“email2@mail.com”来自以下行:

Outlook.Account account = oApp.Session.Accounts["email2@mail.com"];

然后

appt.SendUsingAccount = account;

这就是我在 Outlook 中设置两个电子邮件地址的方式:http://i.imgur.com/0eopV8A.png

两个电子邮件地址都有不同的用户名,并且来自不同的域/邮件服务器,如该屏幕截图所示。

有没有人能够看到我正在解决的问题,或者是否有其他解决方案?

谢谢。

【问题讨论】:

  • 您是否在单个邮件配置文件中设置了两个帐户?还是每个帐户都有两个单独的个人资料?
  • 我不太清楚你的意思是什么或如何检查。如果有帮助,这就是我在 Outlook 中设置两个电子邮件地址的方式:i.imgur.com/0eopV8A.png 这两个电子邮件地址都有不同的用户名,并且来自不同的域/邮件服务器,如该屏幕截图所示。希望对您有所帮助。
  • 另外,电子邮件地址在控制面板 -> 邮件 (Microsoft Outlook 2013) -> 电子邮件帐户中的显示方式如下:i.imgur.com/mPTq3ll.png
  • 谢谢。您可以找到所需的存储,然后调用 GetDefaultFolder 方法来获取特定帐户的日历文件夹。更多信息请参考我的回答。

标签: c# email outlook interop appointment


【解决方案1】:

不清楚您是在单个邮件配置文件中还是在单独的配置文件中设置了两个帐户。

AppointmentItem 类的SendUsingAccount 属性允许设置一个Account 对象,该对象表示要发送AppointmentItem 的帐户。因此,SendUsingAccount 属性可用于指定在调用 Send 方法时用于发送 AppointmentItem 的帐户。我想这不是你要找的东西。

无论如何,您都可以使用 Store 类的 GetDefaultFolder 方法,该方法返回一个 Folder 对象,该对象表示存储中的默认文件夹,该对象的类型由 FolderType 参数指定。此方法类似于 NameSpace 对象的 GetDefaultFolder 方法。不同之处在于,此方法获取与帐户关联的交付存储上的默认文件夹,而 NameSpace.GetDefaultFolder 返回当前配置文件的默认存储上的默认文件夹。

因此,您可以获取所需帐户的日历文件夹并在其中添加新约会。

您可能会发现 MSDN 中的以下文章很有帮助:

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2014-04-04
    • 1970-01-01
    • 2021-07-07
    • 2018-07-10
    • 2017-10-27
    相关资源
    最近更新 更多