Export (0) Print
Expand All

How to: Create a Sendable Item for a Specific Account Based on the Current Folder

This topic contains two code examples that show how to create a sendable e-mail item and meeting request, and then how to send them by using a specific account that is based on the current folder.

When you use the CreateItem(OlItemType) method of the Application object to create an Outlook item, the item is created for the primary account for that session. In a session where multiple accounts are defined in the profile, you can create an item for a specific IMAP, POP, or Exchange account. If there are multiple accounts in the current profile and you create a sendable item in the user interface, for example, by clicking New E-mail or New Meeting, an inspector displays a new mail item or meeting request in compose mode, and then you can select the account from which to send the item. This topic shows how to programmatically create a sendable item and send it by using a specific sending account. The topic has two code examples that show how to create a MailItem and an AppointmentItem for a specific account that is determined by the current folder in the active explorer.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;

In the first method below, CreateMailItemFromAccount first identifies the appropriate account by matching the store of the current folder (obtained from the Store property) with the default delivery store of each account (obtained with the DeliveryStore property) that is defined in the Accounts collection for the session. CreateMailItemFromAccount then creates the MailItem. To associate the item with the account, CreateMailItemFromAccount assigns the user of the account as the sender of the item by setting the account.CurrentUser.AddressEntry property to the Sender property of the MailItem. Assigning the Sender property is the important step; if you do not specify the sender, the MailItem is created for the primary account by default. At the end of the method, CreateMailItemFromAccount displays the MailItem. Note that if the current folder is not on a delivery store, CreateMailItemFromAccount creates the MailItem for the primary account for the session.

private void CreateMailItemFromAccount()
{
    Outlook.AddressEntry addrEntry = null;
    // Get the Store for CurrentFolder.
    Outlook.Folder folder =
        Application.ActiveExplorer().CurrentFolder 
        as Outlook.Folder;
    Outlook.Store store = folder.Store;
    Outlook.Accounts accounts =
        Application.Session.Accounts;
    // Enumerate accounts to find
    // account.DeliveryStore for store.
    foreach (Outlook.Account account in accounts)
    {
        if (account.DeliveryStore.StoreID == 
            store.StoreID)
        {
            addrEntry =
                account.CurrentUser.AddressEntry;
            break;
        }
    }
    // Create MailItem.
    Outlook.MailItem mail =
        Application.CreateItem(
        Outlook.OlItemType.olMailItem)
        as Outlook.MailItem;
    if (addrEntry != null)
    {
        // Set Sender property.
        mail.Sender = addrEntry;
        mail.Display(false);
    }
}

The next method, CreateMeetingRequestFromAccount, is similar to CreateMailItemFromAccount except that it creates an AppointmentItem instead of a MailItem. CreateMeetingRequestFromAccount first identifies the appropriate account by matching the store of the current folder (obtained from the Store property) with the default delivery store of each account (obtained from the DeliveryStore property) that is defined in the Accounts collection for the session. CreateMeetingRequestFromAccount then creates the AppointmentItem. To associate the item with the account, CreateMeetingRequestFromAccount assigns that account as the item's sending account by setting the Account object to the SendUsingAccount property of the AppointmentItem. Assigning the SendUsingAccount property is the important step; if you do not specify the account, the AppointmentItem is created for the primary account by default. At the end of the method, CreateMeetingRequestFromAccount displays the AppointmentItem. Note that if the current folder is not on a delivery store, CreateMeetingRequestFromAccount creates the AppointmentItem for the primary account for the session.

private void CreateMeetingRequestFromAccount()
{
    Outlook.Account acct = null;
    Outlook.Folder folder =
        Application.ActiveExplorer().CurrentFolder
        as Outlook.Folder;
    Outlook.Store store = folder.Store;
    Outlook.Accounts accounts =
        Application.Session.Accounts;
    foreach (Outlook.Account account in accounts)
    {
        if (account.DeliveryStore.StoreID ==
            store.StoreID)
        {
            acct = account;
            break;
        }
    }
    Outlook.AppointmentItem appt =
        Application.CreateItem(
        Outlook.OlItemType.olAppointmentItem)
        as Outlook.AppointmentItem;
    appt.MeetingStatus = 
        Outlook.OlMeetingStatus.olMeeting;
    if (acct != null)
    {
        // Set SendUsingAccount property.
        appt.SendUsingAccount=acct;
        appt.Display(false);
    }
}

Other resources

Show:
© 2014 Microsoft