1
votes

I have two mailboxes setup in Outlook.

I'll refer to them as "[email protected]" and "[email protected]".

I would like to use Interop to create and send an appointment to a specific email address calender, not just to the default outlook account.

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

This code works flawlessly in sending an email automatically to "[email protected]" from my email "[email protected]".

However, I would like to automatically create an appointment/meeting for a specific email address.

This is my current attempt:

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

This code does create an appointment successfully, but it keeps creating an appointment for "[email protected]" instead of "[email protected]", which shouldn't happen as I've specified the sending account to be "[email protected]" from the lines:

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

and then

appt.SendUsingAccount = account;

This is how my two email addresses are set up in Outlook: http://i.imgur.com/0eopV8A.png

Both the email addresses have different user names and are from different domains/mail servers, as shown in that screenshot.

Would anyone be able to see the problem I'm making or if there's a different solution?

Thank you.

1
Do you have two accounts set up in the single Mail profile? Or do you have two separate prfoiles for each account?Eugene Astafiev
I'm not too sure what you mean by that or how to check. If it helps, this is how my two email addresses are set up in Outlook: i.imgur.com/0eopV8A.png Both the email addresses have different user names and are from different domains/mail servers, as shown in that screenshot. Hope that helps.Matthew
Also, here's how the email address are displayed in Control Panel -> Mail (Microsoft Outlook 2013) -> E-mail accounts... : i.imgur.com/mPTq3ll.pngMatthew
Thank you. You can find the required store and then call the GetDefaultFolder method to get a Calendar folder for a specific account. Please refer to my answer for more information.Eugene Astafiev

1 Answers

0
votes

It is not clear whether you have got two accounts set up in the single Mail profile or separate profiles.

The SendUsingAccount property of the AppointmentItem class allows to set an Account object that represents the account under which the AppointmentItem is to be sent. So, The SendUsingAccount property can be used to specify the account that should be used to send the AppointmentItem when the Send method is called. It is not what you are looking for I suppose.

Anyway, you can use the GetDefaultFolder method of the Store class which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

Thus, you can get the Calendar folder for the required account and add a new appointment there.

You may find the following articles in MSDN helpful: