Outlook with .NET 2.0

Outlook Object Model Overview 

For accessing the outlook and its features you have to add reference of Microsoft Outlook 11.0 object library Version 9.2 (COM component) to your project.This COM component provides various objects through we can access the outlook.

  1. Microsoft.Office.Interop.Outlook.Application
  2. Microsoft.Office.Interop.Outlook.Explorer
  3. Microsoft.Office.Interop.Outlook.Inspector
  4. Microsoft.Office.Interop.Outlook.MAPIFolder
  5. Microsoft.Office.Interop.Outlook.MailItem
  6. Microsoft.Office.Interop.Outlook.AppointmentItem
  7. Microsoft.Office.Interop.Outlook.TaskItem
  8. Microsoft.Office.Interop.Outlook.ContactItem 

1. Microsoft.Office.Interop.Outlook.Application

 

Using this class we can create an object ot the outlook application.

 

Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();

 

2. Microsoft.Office.Interop.Outlook.AppointmentItem

 

You can create an appointment using this class. You can not directly create an instance of this class. Because the AppointmentItemClass has not constructors defined so; you have to use the application object for create an instance of the appointment object. 

For create an instance of the AppointmentItem object You have to use the Application object's CreateItem() method. This method takes OlItemType enumuration parameter which has eight values.

 

(olAppointmentItem, olContactItem, olDistributionListItem, olJournalItem, olMailItem, olNoteItem, olPostItem, olTaskItem) and cast it to AppintmenItem type.

Microsoft.Office.Interop.Outlook.AppointmentItem oAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApp.CreateItem

(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);

 

AppointmentItem object provides various properties.

 

Properties

Property

Description

Type

Subject

Set the subject of the appointment

String

Location

Location of the appointment

String

Body

Body of the appointment

String

Start

Set start date time of appointment

DateTime

End

Set end date time of appointment

DateTime

ReminderSet

Set whether reminder is on or off

Boolean

ReminderMinuteBeforeStart

Set the time period before reminder message is popup

Integer

ReminderPlaySound

Set the sound file path which play sound when reminder is active

String (Path)

Importance

Set the importance priority for appointment

OlImportance

BusyStatus

Set the status of appointment

OlBusyStatus

OlImportance enumeration takes three values

 

olImportanceHigh     ->   High importance

olImportanceLow     ->   Low importance

olImportanceNormal ->  Normal importance

 

OlBusyStatus enumeration takes four values

 

olBusy                  ->  Busy status

olFree                  ->  Free status

olOutOfOffice        ->  Out of office status

olTentative           ->  Tentative status (under terms not fully final) 

 

Methods:

 

Save()

 

This method save your appointment to your system. When you call the save() method an appointment is save to outlook.

 

ForwardAsVcal()

 

This method can be used to the Vcs file via email.

 

Microsoft.Office.Interop.Outlook.MailItem oMailItem = oAppointment.ForwardAsVcal();

oMailItem.To = "Destination mail id";

oMailItem.Send() ;

The send method send the appointment via email.

The receipient receive the mail and press the save button the appointment is automatically add to his outlook.

 

//Create an instance of the Appilcation object

Microsoft.Office.Interop.Outlook.Application outlookApp = new Application();       

 

//Create and instance of the Appointment class 

Microsoft.Office.Interop.Outlook.AppointmentItem oAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);

 

//Set the various property of the appointmentitem object

oAppointment.Subject = "This is appointment Subject";

oAppointment.Body = "This is appointment body";

oAppointment.Location = "This is appointment location";

oAppointment.Start= Convert.ToDateTime("06/29/2006 10:00:00 AM");

oAppointment.End= Convert.ToDateTime("06/29/2006 10:00:00 AM");

oAppointment.ReminderSet = true;

oAppointment.ReminderMinutesBeforeStart = 15;

oAppointment.ReminderPlaySound = false;

oAppointment.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;

oAppointment.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;    
   

//This method save the appointment to the outlook

oAppointment.Save();

       

//send a mail and appointment as an attachment to Ajay Rohilla

Microsoft.Office.Interop.Outlook.MailItem oMailItem = oAppointment.ForwardAsVcal();

oMailItem.To = "FirstName.LastName @t-systems.com";

oMailItem.Send();

 

3. Microsoft.Office.Interop.Outlook.ContactItem

 

Using this class you can add contacts to outlook. You have to create an instance of the application object and then set the parameter value  olContactItem to OlItemType to the CreateItem() mehod of the Appication object.

 

Microsoft.Office.Interop.Outlook.Application outlookApp = new Application();

Microsoft.Office.Interop.Outlook.ContactItem newContatItem = (Microsoft.Office.Interop.Outlook.ContactItem)outlookApp.CreateItem(OlItemType.olContactItem); 

 

Properies:  

Property

Description

Type

FirstName

First name of the user

String

LastName

Last name of the user

String

Email1Address

First email address of the user

String

PrimaryTelephoneNumber

Telephone number

String

MailAddressCity

Name of the city

String

JobTitle

Job title of the user

String

Birthday

Birth date of the user

DateTime

Methods:

 

AddPicture(String): Add the picture to the contact item. Pass the string which is the path of the image fie as value to this method.

Save() : Save the contact item to the outlook.

Display(Boolean): This method takes a Boolean value in paramether. If you pass the true value it will popup the contact item window otherwise it will not display.

ForwardAsVcard(): Using this method you can send the Vcard via email.

 

Microsoft.Office.Interop.Outlook.Application outlookApp = new Application();

Microsoft.Office.Interop.Outlook.ContactItem newContatItem = (Microsoft.Office.Interop.Outlook.ContactItem)outlookApp.CreateItem(OlItemType.olContactItem);

newContatItem.FirstName = "FistName";

newContatItem.LastName = "LastName";

newContatItem.Email1Address = "[email protected]";        newContatItem.PrimaryTelephoneNumber = "98********";

newContatItem.MailingAddressCity = "Pune";

newContatItem.MailingAddressState = "Maharashtra";

newContatItem.JobTitle = "Consultant";

newContatItem.CompanyName = "T-Systems India Pvt. Ltd";

newContatItem.BusinessFaxNumber = "022******";

newContatItem.BusinessTelephoneNumber = "020******";

newContatItem.MobileTelephoneNumber = "098********";

newContatItem.BusinessAddress = @"7th Floor,S B Road";

newContatItem.Department = "B Solution";

newContatItem.OfficeLocation = "Office Location";

newContatItem.Profession = "Software Development";

newContatItem.ManagerName = "ManagerName";

newContatItem.AssistantName = "Assistant's Name";

newContatItem.NickName = "Nick Name";

newContatItem.Spouse = "Spouse Name";

newContatItem.Birthday = Convert.ToDateTime("mm/dd/yyyy");

string imagePath = @"path…… \Sree Sai2.bmp";

newContatItem.AddPicture(imagePath);

newContatItem.Save();

newContatItem.Display(true);

Microsoft.Office.Interop.Outlook.MailItem oMailItem = newContatItem.ForwardAsVcard();

oMailItem.To = "[email protected]";

oMailItem.Send();

 

4. Microsoft.Office.Interop.Outlook.TaskItem

 

Using this class you can add tasks to outlook. You have to create an instance of the application object and then set the parameter value  olTaskItem to OlItemType to the CreateItem() mehod of the Appication object.

 

Microsoft.Office.Interop.Outlook.Application outlookApp = new Application();

Microsoft.Office.Interop.Outlook.TaskItem oTask = (Microsoft.Office.Interop.Outlook.TaskItem)outlookApp.CreateItem(OlItemType.olTaskItem);

 

Properties

Property

Description

Type

Subject

Subject of the task

String

DueDate

Due date of the task

DateTime

StartDate

Start date of the task

DateTime

ReminderSet

Whether reminder is on or off

Boolean

ReminderTime

Time of the reminder

DateTime

Body

Body of the task

String

Status

Status of the task

OlTaskStatus

OlTaskStatus enumeration contains five values

 

olTaskComplete        -> Task is complete

olTaskDeferred         -> Task is deferred

olTaskInProgress      -> Task is in progress (Continue)

olTaskNotStarted     -> Task is not started

olTaskWaiting          -> Task is in wait

 

Methods:
 

Save(): This method saves the task to the outlook.

 

Send the task via email:

 

If you want to send the task then you have to create an instance of the Receipient class.

Microsoft.Office.Interop.Outlook.Recipients oRecipients = oTask.Recipients;

 

//Code of the task
Microsoft.Office.Interop.Outlook.Application outlookApp = new Application();

Microsoft.Office.Interop.Outlook.TaskItem oTask = (Microsoft.Office.Interop.Outlook.TaskItem)outlookApp.CreateItem(OlItemType.olTaskItem);

oTask.Subject = "This is my task subject";

oTask.DueDate = Convert.ToDateTime("06/29/2006");

oTask.StartDate = Convert.ToDateTime("06/28/2006");

oTask.ReminderSet = true;

oTask.ReminderTime = Convert.ToDateTime("06/28/2006 02:40:00 PM");

oTask.Body = "This is the task body";

oTask.SchedulePlusPriority = "High";

oTask.Status = OlTaskStatus.olTaskInProgress;

oTask.Save();

 

//Send task via email
Microsoft.Office.Interop.Outlook.Recipients oRecipients = oTask.Recipients;

Microsoft.Office.Interop.Outlook.Recipient oReceipient;

oReceipient = oRecipients.Add("[email protected]");

oReceipient = oRecipients.Add("FirstName2. [email protected]");

oReceipient = oRecipients.Add("FirstName3. [email protected]");

oReceipient.Type = 1;

oRecipients.ResolveAll();

oTask.Assign();

oTask.Send();

Up Next
    Ebook Download
    View all
    Learn
    View all