January 27, 2021
Hot Topics:

Interacting with Outlook from a WinForms Application

  • By Rachel Wireman
  • Send Email »
  • More Articles »

I use Email1DisplayName to populate the ListBox on my form. I then display some of the properties in a tool tip in the hover event.

Click here for a larger image.

When the user double-clicks one of the contacts, a new form will open up; it allows the user to modify the contact's information. The following sample code shows how to update the contact with the information from the textboxes.

Click here for a larger image.

oContact.FirstName     = textBoxFirst.Text;
oContact.LastName      = textBoxLast.Text;
oContact.MiddleName    = textBoxMiddle.Text;
oContact.Title         = textBoxTitle.Text;
oContact.JobTitle      = textBoxJobTitle.Text;
oContact.WebPage       = textBoxWebPage.Text;
oContact.Email1Address = textBoxEmail.Text;


Notice that you can write to the Email1Address property without triggering the security warning. The Outlook security model will block read access to the property to prevent applications from stealing your information. You will not receive a security warning when you simply set the property to a new value. Once you have set the properties, the save method will update the contact in Outlook.

Sending an Email to Your Contacts

Just to give an example of the contacts in use, you can add a button to the application's form that will automatically send an email to the selected contact in the ListBox. The button's click event looks like this:

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

if (this.listViewContacts.SelectedItems != null &&
   this.listViewContacts.SelectedItems.Count > 0)
   Outlook.ContactItem oRecip = (Outlook.ContactItem)

   Outlook.MailItem email = (Outlook.MailItem)
   email.Subject = "Just wanted to say...";
   email.Body = "Have a great day!";

   if (MessageBox.Show(
      "Are you sure you want to send a good day message to " +
      oRecip.Email1DisplayName + "?", "Send?",
      == DialogResult.OK)
         MessageBox.Show("Email sent successfully.", "Sent");
      catch (Exception ex)
         MessageBox.Show("Email failed: " + ex.Message, 
            "Failed Send");

   oRecip = null;
   email = null;

Unless you are using Redemption, the above code again will be affected by security warnings.

Page 3 of 4

This article was originally published on September 24, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date