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.
-
Microsoft.Office.Interop.Outlook.Application
-
Microsoft.Office.Interop.Outlook.Explorer
-
Microsoft.Office.Interop.Outlook.Inspector
-
Microsoft.Office.Interop.Outlook.MAPIFolder
-
Microsoft.Office.Interop.Outlook.MailItem
-
Microsoft.Office.Interop.Outlook.AppointmentItem
-
Microsoft.Office.Interop.Outlook.TaskItem
-
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();
KasunPosted May 26, 2010, 10:49 PM
Hi, I wrote C# application for import unread e-mails from outlook 2007, I could import sender name, sender mail address,subject and body to data grid view as following UnreadEmails mail = new UnreadEmails(); mail.SenderName = (mailItem.UnRead == false) ? string.Empty : mailItem.SenderName; mail.SenderAddress = (mailItem.UnRead == false) ? string.Empty : mailItem.SenderEmailAddress; mail.Subject = (mailItem.UnRead == false) ? string.Empty : mailItem.Subject; mail.BodyContent = (mailItem.UnRead == false) ? string.Empty : mailItem.Body; // mail.AttachmentContent = (mailItem.UnRead == false) ? string.Empty : mailItem.Attachments.Session.OpenSharedItem; emails.Add(mail); UnreadEmails is a separte class. but couldn't find a way to import attachments (word pdf ppt excel) because i need it for my filter pls help me about it i need to import those attachments to my program for filtering purpose ..
varsha paliwalPosted Aug 28, 2009, 9:19 AM
I tried your solution to add new contacts but whenever i try the same it says "an error in the outlook encountered an error and needs to be closed".please guide for the same.
Vamsi KrishnaPosted Oct 14, 2008, 2:51 AM
Hi whether the above code can be used in ASP.NET application ? Operation failed error coming while executing the line Microsoft.Office.Interop.Outlook.TaskItem oTask = (Microsoft.Office.Interop.Outlook.TaskItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olTaskItem); please help us.
Megan TippsPosted Dec 10, 2007, 4:32 AM
Love the article!! But I'm creating a program where the user enters the date and time themselves and put it in 2 seperate textboxes. oAppointment.Start= Convert.ToDateTime("06/29/2006 10:00:00 AM"); That part is where I'm stuck, I'm struggling to convert the textbox strings into the correct format. Please help!!
Megan TippsPosted Dec 10, 2007, 4:08 AM
Love the article!! But I'm creating a program where the user enters the date and time themselves and put it in 2 seperate textboxes. oAppointment.Start= Convert.ToDateTime("06/29/2006 10:00:00 AM"); That part is where I'm stuck, I'm struggling to convert the textbox strings into the correct format. Please help!!
Sawan GuptaPosted Nov 7, 2007, 3:22 AM
Outlook.Application obj = new Outlook.Application(); Outlook.MailItem mail = (Outlook.MailItem)obj.CreateItem(Outlook.OlItemType.olMailItem); mail.To="[email protected]"; mail.Subject=" thank you"; mail.Body= " this is test mail"; Outlook.Attachment attch = (Outlook.Attachment)"c:\\abc.txt"; mail.Attachments.Add(attch) ; mail.Display(obj);
Sawan GuptaPosted Nov 7, 2007, 3:22 AM
Outlook.Application obj = new Outlook.Application(); Outlook.MailItem mail = (Outlook.MailItem)obj.CreateItem(Outlook.OlItemType.olMailItem); mail.To="[email protected]"; mail.Subject=" thank you"; mail.Body= " this is test mail"; Outlook.Attachment attch = (Outlook.Attachment)"c:\\abc.txt"; mail.Attachments.Add(attch) ; mail.Display(obj);
Sawan GuptaPosted Nov 7, 2007, 3:21 AM
Outlook.Application obj = new Outlook.Application(); Outlook.MailItem mail = (Outlook.MailItem)obj.CreateItem(Outlook.OlItemType.olMailItem); mail.To="[email protected]"; mail.Subject=" thank you"; mail.Body= " this is test mail"; Outlook.Attachment attch = (Outlook.Attachment)"c:\\abc.txt"; mail.Attachments.Add(attch) ; mail.Display(obj);
Thulasi RamanPosted Aug 10, 2007, 8:20 AM
Hi I have a form which contains lot of user entry fields(textbox) and a button in the end.Now what i want is i need to send this form to mail message body by caputuring the user values.please give me a solution for this. thanks in advance Thulasiram
Sabin SankarPosted Aug 7, 2007, 9:21 AM
I have an html body string for the email bodi.I need to ensure that the text is displayed in the email in lanscape format.Can ya help me!
Sabin SankarPosted Aug 7, 2007, 9:10 AM
I have an html body string for the email bodi.I need to ensure that the text is displayed in the email in lanscape format.Can ya help me!
rahim ataeiPosted Jul 16, 2007, 4:43 AM
how can i save as selection item in outlook to specify folder by vb.net
Anshu BhatiaPosted Jun 28, 2007, 12:12 PM
Good inoformation
Mahesh ChandPosted Jun 27, 2007, 12:01 PM
Great article Nimesh. I've seen many questions on this topic. Keep up the good work.