Click here to Skip to main content
13,260,330 members (44,122 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 24 Jul 2005

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 ;)). 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...

You may also be interested in...

Comments and Discussions

QuestionYou can also do this with EWS. Pin
ayseff2-Dec-14 17:14
memberayseff2-Dec-14 17:14 
GeneralNice Article Pin
Rajin Sayeed S Alam15-May-11 20:07
memberRajin Sayeed S Alam15-May-11 20:07 
GeneralMy vote of 3 Pin
fertansa2-Mar-11 12:21
memberfertansa2-Mar-11 12:21 
Newsbroken code - do not use as-is - please read the RFC and validate Pin
rlively6-Apr-10 9:16
memberrlively6-Apr-10 9:16 
To create a proper iCalendar file, read up on the Internet Calendaring and Scheduling Core Object Specification (iCalendar). To help validate that you are correctly creating calendar file, use the iCalendar Validator, which "is used to validate calendaring data against the iCalendar (RFC 5545) standard." Also see to read and collaborate on creating calendar files.

This code produces an invalid calendar file for various reasons (missing required properties such as DTSTAMP and UID, not escaping special characters, etc). Also, there is an update to the vCalendar spec called iCalendar, which was put forth in 1998.

One of the biggest problems is "MIMEDIR" beginning on a line, which makes it appear as a property - there is no "MIMEDIR" property.

Thumbs Down | :thumbsdown:
sbvCalendar.Append("PRODID:-//Microsoft Corporation//Outlook ");

If you save a calendar event from Outlook, you can see that MIMEDIR//EN is part of the previous line containing the PRODID property that identifies the program that created the iCalendar data (Outlook in this case). The end of the PRODID line and the beginning of the next property VERSION have been jammed together in this code to create MIMEDIR//ENVERSION:1.0.

VERSION:2.0 property should be included on a line by itself to identify the version.

Good example exported from Outlook:

Thumbs Up | :thumbsup:
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN

This code appears to be based off an example such as this posting that, due to the formatting of the post, makes lines appear to merge together. There's even a note stating that it should be one line and not multiple lines to try to eliminate confusion.

PRODID:-//Microsoft Corporation//Outlook 11.0
(these previous 3 lines are all one line)

Read the spec and make sure you understand what the code does and what it produces.


Here are some relevant bits from the RFC that will help in creating a proper iCalendar file and will help with many of the issues people are facing such as failure to import into Outlook, being unable to get the "all day event" checkbox to be checked in Outlook, etc:
The VERSION property is required by the RFC: Conformance: This property MUST be specified once in an iCalendar object.
<128> Section Exchange 2003, Exchange 2007, and Exchange 2010 require that X-MICROSOFT-CDO-ALLDAYEVENT be set to 'TRUE' if the Calendar object is to be imported as all day.
Outlook 2003 requires that a VALUE parameter be set to 'DATE' for both DTSTART and DTEND if the Calendar object is to be imported as all day.
<129> Section Outlook 2003 does not import or export the X-MICROSOFT-CDO-ALLDAYEVENT or X-MICROSOFT-MSNCALENDAR-ALLDAYEVENT properties.
Value Type: The default value type is DATE-TIME. The time value MUST be one of the forms defined for the DATE-TIME value type.
The value type can be set to a DATE value type. (";" "VALUE" "=" ("DATE-TIME" / "DATE")) / (";" tzidparam) /
The following is an example of the "VEVENT" calendar component used to represent a multi-day event scheduled from June 28th, 2007 to July 8th, 2007 inclusively. Note that the "DTEND" property is set to July 9th, 2007, since the "DTEND" property specifies the non-inclusive end of the event.

SUMMARY:Festival International de Jazz de Montreal
The "DTEND" property for a "VEVENT" calendar component specifies the non-inclusive end of the event. For cases where a "VEVENT" calendar component specifies a "DTSTART" property with a DATE value type but no "DTEND" nor "DURATION" property, the event's duration is taken to be one day.
Exporting an day all day event from Outlook creates DTEND;VALUE=DATE: for the day after the appointment ends, even for 1 day events.
Purpose: This property defines whether or not an event is transparent to busy time searches.
Description: Time Transparency is the characteristic of an event that determines whether it appears to consume time on a calendar.
Events that consume actual time for the individual or resource associated with the calendar SHOULD be recorded as OPAQUE, allowing them to be detected by free/busy time searches. Other events, which do not take up the individual's (or resource's) time SHOULD be recorded as TRANSPARENT, making them invisible to free/ busy time searches.
The following are REQUIRED, but MUST NOT occur more than once. dtstamp / uid / Unique Identifier
Property Name: UID
Purpose: This property defines the persistent, globally unique identifier for the calendar component.
Description: The "UID" itself MUST be a globally unique identifier. The generator of the identifier MUST guarantee that the identifier is unique. There are several algorithms that can be used to accomplish this. A good method to assure uniqueness is to put the domain name or a domain literal IP address of the host on which the identifier was created on the right-hand side of an "@", and on the left-hand side, put a combination of the current calendar date and time of day (i.e., formatted in as a DATE-TIME value) along with some other currently unique (perhaps sequential) identifier available on the system (for example, a process id number). Using a DATE-TIME value on the left-hand side and a domain name or domain literal on the right-hand side makes it possible to guarantee uniqueness since no two hosts should be using the same domain name or IP address at the same time.
Example: The following is an example of this property with formatted line breaks in the property value:
DESCRIPTION:Meeting to provide technical review for "Phoenix"
design.\nHappy Face Conference Room. Phoenix design team
MUST attend this meeting.\nRSVP to team leader.
Value Name: TEXT
ESCAPED-CHAR = ("\\" / "\;" / "\," / "\N" / "\n")
\\ encodes \, \N or \n encodes newline, \; encodes ;, \, encodes ,

An intentional formatted text line break MUST only be included in a "TEXT" property value by representing the line break with the character sequence of BACKSLASH, followed by a LATIN SMALL LETTER N or a LATIN CAPITAL LETTER N, that is "\n" or "\N".

The "TEXT" property values may also contain special characters that are used to signify delimiters, such as a COMMA character for lists of values or a SEMICOLON character for structured values. In order to support the inclusion of these special characters in "TEXT" property values, they MUST be escaped with a BACKSLASH character. A BACKSLASH character in a "TEXT" property value MUST be escaped with another BACKSLASH character. A COMMA character in a "TEXT" property value MUST be escaped with a BACKSLASH character. A SEMICOLON character in a "TEXT" property value MUST be escaped with a BACKSLASH character. However, a COLON character in a "TEXT" property value SHALL NOT be escaped with a BACKSLASH character.
GeneralRe: broken code - do not use as-is - please read the RFC and validate Pin
Member 1028103925-Feb-14 0:22
memberMember 1028103925-Feb-14 0:22 
Generalmsg.To cannot be assigned Pin
James_Lin11-May-09 11:12
memberJames_Lin11-May-09 11:12 
GeneralAppending strings to get vCalendar: wrong, Using MS API: right Pin
rlively10-Oct-08 6:40
memberrlively10-Oct-08 6:40 
GeneralReminder & Busy Status Pin
Member 44677752-Oct-08 4:56
memberMember 44677752-Oct-08 4:56 
QuestionAppointment vs Task Pin
centric5-Mar-07 8:57
membercentric5-Mar-07 8:57 
GeneralHave a little problem Pin
uddesh14-Feb-07 19:37
memberuddesh14-Feb-07 19:37 

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

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

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