Send a mail item by using a Hotmail account

This example uses the SendUsingAccount property to send a mail item by using a Windows Live Hotmail account.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

A profile defines one or more email accounts, and each email account is associated with a server of a specific type, such as Microsoft Exchange Server or Post Office Protocol 3 (POP3). Because you may have multiple accounts in your profile, you must specify which email account you want to use to send the item, and then obtain an Account object to represent it.

In the following code example, a message is created with an attached itinerary and then sent by using a Windows Live Hotmail account. The Hotmail email account is used as the Account object in the user’s profile. The code example then sets the SendUsingAccount property to that Account and calls the Send() method from the MailItem object.

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;
private void SendUsingAccountExample()
{
    Outlook.MailItem mail = Application.CreateItem(
        Outlook.OlItemType.olMailItem) as Outlook.MailItem;
    mail.Subject = "Our itinerary";
    mail.Attachments.Add(@"c:\travel\itinerary.doc",
        Outlook.OlAttachmentType.olByValue,
        Type.Missing, Type.Missing);
    Outlook.Account account =
        Application.Session.Accounts["Hotmail"];
    mail.SendUsingAccount = account;
    mail.Send();
}

See also