Delete Email Messages


Below is the code to delete messages using VSTO Outlook.

  // Create Application class and get namespace

 Outlook.Application outlook = new Outlook.Application();

 Outlook.NameSpace ns = outlook.GetNamespace("Mapi");

 object _missing = Type.Missing;

 ns.Logon(_missing, _missing, false, true);

 // Get Inbox information in objec of type MAPIFolder

 Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

 Outlook.MailItem item = inbox.Items[0];



Below is the code to delete messages using for .NET.

  string MailBoxURI = "http://MachineName/exchange/Username";

 string UserName = "username";

 string Password = "password";

 string Domain = "domain";

 // Create instance of ExchangeClient class by giving credentials

 ExchangeClient client = new ExchangeClient(MailBoxURI, UserName, Password, Domain);

 // Call ListMessages method to list messages info from Inbox

 ExchangeMessageInfoCollection msgCollection = client.ListMessages(client.MailboxInfo.InboxUri);

 // Get URI of Message to Delete

 string MessageURI= msgCollection[0].UniqueUri;

 // Delete the message


Download Source Code

Download Running Example