Click here to Skip to main content
Click here to Skip to main content

Sending Tasks Programmatically

, 24 Jul 2005
Rate this:
Please Sign up or sign in to vote.
This article shows two different ways of sending tasks programmatically. One is using the Microsoft Outlook 11.0 Object Library and the other is using vCalendar.


I’m going to show two different ways of sending tasks programmatically. One is using the Microsoft Outlook 11.0 Object Library and the other is using vCalendar.

Using Microsoft Outlook 11.0 Object Library

The simplest of these is using Microsoft 11.0 object library. This has a class called TaskItem. TaskItem represents a task (an assigned, delegated, or self-imposed task to be performed within a specified time frame) in a Tasks folder. Like appointments or meetings, tasks can be delegated. You can get this library from Add Reference and click on COM tab.

Create an alias as follows:

using Outlook = Microsoft.Office.Interop.Outlook;

Create objects of ApplicationClass and TaskItem as follows:

Outlook.ApplicationClass app = new Outlook.ApplicationClass();
Outlook.TaskItem tsk = (Outlook.TaskItem) 

Set the properties of task as needed:

tsk.StartDate = DateTime.Now;
tsk.DueDate = DateTime.Now;
tsk.Subject = "Test";
tsk.Body = "Testing Task";

That’s it. This is the simplest way for creating and sending a task programmatically. One disadvantage of this is we get to click away the annoying security popups.

Using vCalendar

The next way is through the vCalendar object. vCalendar is the industry standard format for sending/receiving scheduling information electronically. vCalendar was developed by a consortium formed by IBM, AT&T, Siemens and Apple. Later, this specification was given to Internet Mail Consortium. Now, most of the Personal Information Manager (PIM) programs on all software support vCalendar as the standard exchange format.

Microsoft Outlook supports vCalendar (what more do we want Wink | ;) ). vCalendar files are not just used to exchange appointments and schedules within one organization. They can be used to schedule appointments with others, who use scheduling software incompatible with yours (all the more we want, right?). Let’s get on with how to create a vCalendar object programmatically.

vCalendar file would look something like the following:

BEGIN:VCALENDAR PRODID:-//Microsoft Corporation//Outlook MIMEDIR//EN 
     VERSION:1.0 BEGIN:VEVENT DTSTART:19980114T210000Z DTEND:19980114T230000Z 
     LOCATION:Team Room CATEGORIES:Business 

To create the vCalendar object, we have the method given below:

string CreateTask(DateTime start, DateTime end, string sub, string msgBody)
    StringBuilder sbvCalendar = new StringBuilder();

    sbvCalendar.Append("METHOD: REQUEST");
    sbvCalendar.Append("PRODID:-//Microsoft Corporation//Outlook ");
    string hour = start.Hour.ToString();
    if(hour.Length<2){hour ="0"+ hour;}
    string min = start.Minute.ToString();
    if(min.Length<2){min = "0" + min;}
    string sec = start.Second.ToString();
    if(sec.Length<2){sec = "0" + sec;}

    string mon = start.Month.ToString();
    if(mon.Length<2){mon ="0" + mon;}
    string day = start.Day.ToString();
    if(day.Length<2){day ="0" + day;}

    sbvCalendar.Append(start.Year.ToString()+ mon + day 
                           +  "T" + hour + min + sec );

    hour = end.Hour.ToString();
    if(hour.Length<2){hour ="0"+ hour;}

    min = end.Minute.ToString();
    if(min.Length<2){min = "0" + min;}
    sec = end.Second.ToString();
    if(sec.Length<2){sec = "0" + sec;}

    mon = end.Month.ToString();
    if(mon.Length<2){mon ="0" + mon;}
    day = end.Day.ToString();
    if(day.Length<2){day ="0" + day;}

    sbvCalendar.Append(end.Year.ToString()+ mon + 
                 day +  "T" + hour + min + sec );

                                             + String.Empty);
    //Message body
                                                    + msgBody);

                                                    + sub);

    return sbvCalendar.ToString();

We can use this method as:

string sub = "Test";
string body = "Testing Task";

//Create a message object
MailMessage msg = new MailMessage();
msg.From = "";
msg.To = "";
msg.Subject = sub;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Body = body;
//Set the date/time
DateTime start = DateTime.Parse("Jan 1, 2005");
DateTime end = DateTime.Parse("Jan 2, 2005");
DateTime ex = DateTime.Now;

//Location where you want to save the vCalendar file
string attachUrl = 
   "C:\Inetpub\wwwroot\WebApplication3\bin\ Test.vcs";

//Create task
using(StreamWriter sw = new StreamWriter(attachUrl)
  sw.Write(CreateTask (start, end, sub, body));

//Attach the task
MailAttachment mAttachment = new MailAttachment(attachUrl);
SmtpMail.SmtpServer = "";

That’s it. Happy coding!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mercy Ponnupandy
United States United States
Architect / Consultant on Content Management and Cloud Computing... Artist and Author by the weekends...
Follow on   Twitter

Comments and Discussions

QuestionYou can also do this with EWS. Pinmemberayseff2-Dec-14 17:14 
GeneralNice Article PinmemberRajin Sayeed S Alam15-May-11 20:07 
GeneralMy vote of 3 Pinmemberfertansa2-Mar-11 12:21 
Newsbroken code - do not use as-is - please read the RFC and validate Pinmemberrlively6-Apr-10 9:16 
GeneralRe: broken code - do not use as-is - please read the RFC and validate PinmemberMember 1028103925-Feb-14 0:22 
Generalmsg.To cannot be assigned PinmemberJames_Lin11-May-09 11:12 
any ideas?
GeneralAppending strings to get vCalendar: wrong, Using MS API: right Pinmemberrlively10-Oct-08 6:40 
GeneralReminder & Busy Status PinmemberMember 44677752-Oct-08 4:56 
QuestionAppointment vs Task Pinmembercentric5-Mar-07 8:57 
GeneralHave a little problem Pinmemberuddesh14-Feb-07 19:37 
GeneralRe: Have a little problem PinmemberFilip C2-Apr-09 2:43 
GeneralOther people's tasks PinmemberKokas1-Nov-05 3:10 
GeneralRe: Other people's tasks Pinmemberstehlewm21-Jul-06 5:43 
QuestionAnd with no attachment? Pinsussjoujoukinder1-Sep-05 21:51 
QuestionOutlook neets to run in background ? Pinmembermvanschie27-Jul-05 23:12 
AnswerRe: Outlook neets to run in background ? Pinmemberstehlewm21-Jul-06 5:37 
GeneralNice, But... PinmemberFZelle25-Jul-05 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 25 Jul 2005
Article Copyright 2005 by Mercy Ponnupandy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid