How to: Create a Mail Item by Using a Message Template

This example creates a mail item by using the CreateItemFromTemplate method.

This code sample opens the Ivy.oft template file, assigns a subject, and then saves the message to the Drafts folder.

The CreateItemFromTemplate method is useful if you have an Outlook form template file (.oft) stored on disk that you want to use as a message template. The template file can contain preformatted text, stationery, or images that you want to include in the message. However, if the template file contains code behind the form, the form code will not run.

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

using Outlook = Microsoft.Office.Interop.Outlook;
private void CreateItemFromTemplate()
    Outlook.Folder folder =
        Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
    Outlook.MailItem mail =
        @"c:\ivy.oft", folder) as Outlook.MailItem;
    mail.Subject = "Congratulations";

Other Resources