April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Interacting with Outlook from a WinForms Application, Page 3

  • September 24, 2007
  • 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;

oContact.Save();

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)
      (this.listViewContacts.SelectedItems[0].Tag);

   Outlook.MailItem email = (Outlook.MailItem)
      (oApp.CreateItem(Outlook.OlItemType.olMailItem));
   email.Recipients.Add(oRecip.Email1Address);
   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?",
      MessageBoxButtons.OKCancel)
      == DialogResult.OK)
   {
      try
      {
         ((Outlook.MailItem)email).Send();
         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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel