MailItem object

The MailItem object allows you to work with a mail message. An Items collection is associated with each MAPIFolder object. The collection contains all of the mail messages in the folder.

Creating a new mail message

Use the CreateItem method from the Application object to create a new mail message. You must specify the item type with the olMailItem constant from the olItemType enumeration. The mail message is automatically added to the Items collection.

{Create a new mail message.}
mailItem = app.CreateItem(Outlook.olMailItem);

You can also use the Add method from the Items collection to create a new mail message. You must specify the item type with the olMailItem constant.

{Create a new mail message.}
mailItem = folder.Items.Add(Outlook.olMailItem);

Retrieving an existing mail message

You can retrieve a mail message from the Items collection with the Item method by the number associated with the mail message.

{Retrieve the third mail message from the Items collection.}
mailItem = folder.Items.Item(3);

You can also access a mail message by the mail message’s subject.

{Retrieve the mail message named "Houses Available".}
mailItem = folder.Items.Item("Houses Available");

Displaying a mail message

Use the Display method to display a mail message in a window.

{Display the mail message.}
mailItem.Display();

Adding a recipient

To add a new recipient to a mail message, create a new Recipient object or add a new recipient to the Recipients collection. For more information, refer to Recipient object.

Adding an attachment

To add a new attachment to a mail message, you must add a new Attachment object to the Attachments collection. For more information, refer to Attachment object.

Specifying the subject of a mail message

Use the Subject property to specify a mail message’s subject.

{Specify the subject of the mail message.}
mailItem.Subject = "Houses Available";

Specifying the body of a mail message

Use the Body property to create the body of a mail message.

{Create the body of a mail message.}
mailItem.Body = "We have found several houses that match your profile.";

Specifying the importance of a mail message

Use the Importance property to specify a mail message’s importance. Set the level of importance with a constant from the olImportance enumeration.

{Set the mail message’s importance to high.}
mailItem.Importance = Outlook.olImportanceHigh;

Sending a mail message

Use the Send method to send a mail message. The Send method also saves the mail message to the Sent Items folder.

{Send the mail message.}
mailItem.Send();

You can use the Sent property to find out if a mail message has been sent. The Sent property returns true if the mail message has been sent. Otherwise, it returns false. You cannot set the value of the Sent property.

{If a mail message has not been sent, send it.}
if(not mailItem.Sent) then
	mailItem.Send();
end if;

Saving a mail message as a file

If you have previously saved a mail message, use the Save method to save the mail message.

{Save an existing mail message.}
mailItem.Save();

To save a new mail message, use the SaveAs method. You must specify the filename for the mail message. You can also specify the saved message’s file type with a constant from the olSaveTypeAs enumeration. If you do not specify a constant, the mail message is saved as the message file type.

{Save the new mail message as a a text file.}
mailItem.SaveAs("C:\RESM\HOUSES.TXT", Outlook.olTXT);

You can use the Saved property to find out if a mail message has been saved. The Saved property returns true if the mail message has been saved. Otherwise, it returns false. You cannot set the value of the Saved property.

{If a mail message has not been saved, save it.}
if(not mailItem.Saved) then
	mailItem.Save();
end if;

Deleting a mail message

To delete a mail message, use the Delete method.

{Delete the mailItem.}
mailItem.Delete();

Closing a mail message

Use the Close method to close a mail message. You can use the constants contained in the OlInspectorClose enumeration to specify whether a mail message is saved when it is closed. To save a draft of a mail message, use the Close method and specify the olSave constant.

{Close the mail message and save the message in the Drafts folder.}
mailItem.Close(Outlook.olSave);


Documentation Feedback