Click here to Skip to main content
15,867,765 members
Articles / Programming Languages / C#
Article

Sending Tasks Programmatically

Rate me:
Please Sign up or sign in to vote.
4.44/5 (13 votes)
24 Jul 20051 min read 110.2K   41   17
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.

Introduction

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.

Image 1

Create an alias as follows:

C#
using Outlook = Microsoft.Office.Interop.Outlook;

Create objects of ApplicationClass and TaskItem as follows:

C#
Outlook.ApplicationClass app = new Outlook.ApplicationClass();
Outlook.TaskItem tsk = (Outlook.TaskItem) 
             app.CreateItem(Outlook.OlItemType.olTaskItem);

Set the properties of task as needed:

C#
tsk.StartDate = DateTime.Now;
tsk.DueDate = DateTime.Now;
tsk.Subject = "Test";
tsk.Body = "Testing Task";
tsk.Recipients.Add("abc@xyz.com");
tsk.Assign();
tsk.Send();

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 
     DESCRIPTION;ENCODING=QUOTED-PRINTABLE:Testing 
     Task=0D=0A SUMMARY:Test PRIORITY:3 END:VEVENT END:VCALENDAR

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

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

    //Header
    sbvCalendar.Append("METHOD: REQUEST");
    sbvCalendar.Append("\n");
    sbvCalendar.Append("BEGIN:VCALENDAR");
    sbvCalendar.Append("\n");
    sbvCalendar.Append("PRODID:-//Microsoft Corporation//Outlook ");
    sbvCalendar.Append("\n");
    sbvCalendar.Append("MIMEDIR//ENVERSION:1.0");
    sbvCalendar.Append("\n");
    sbvCalendar.Append("BEGIN:VEVENT");
    sbvCalendar.Append("\n");
                
    //DTSTART 
    sbvCalendar.Append("DTSTART:");
    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 );
    sbvCalendar.Append("\n");

    //DTEND
    sbvCalendar.Append("DTEND:");
    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 );
    sbvCalendar.Append("\n");

    //Location
    sbvCalendar.Append("LOCATION;ENCODING=QUOTED-PRINTABLE: " 
                                             + String.Empty);
    sbvCalendar.Append("\n");
        
    //Message body
    sbvCalendar.Append("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" 
                                                    + msgBody);
    sbvCalendar.Append("\n");

    //Subject
    sbvCalendar.Append("SUMMARY;ENCODING=QUOTED-PRINTABLE:" 
                                                    + sub);
    sbvCalendar.Append("\n");

    //Priority
    sbvCalendar.Append("PRIORITY:3");
    sbvCalendar.Append("\n");
    sbvCalendar.Append("END:VEVENT");
    sbvCalendar.Append("\n");
    sbvCalendar.Append("END:VCALENDAR");
    sbvCalendar.Append("\n");
            
    return sbvCalendar.ToString();
}

We can use this method as:

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

//Create a message object
MailMessage msg = new MailMessage();
msg.From = "abc@xyz.com";
msg.To = "def@xyz.com";
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);
msg.Attachments.Add(mAttachment);
            
//Send
SmtpMail.SmtpServer = "some-smtp-mail-server.com";
SmtpMail.Send(msg);

That’s it. Happy coding!

License

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


Written By
Architect
United States United States
Architect / Consultant on Content Management and Cloud Computing... Artist and Author by the weekends...

Comments and Discussions

 
QuestionYou can also do this with EWS. Pin
ayseff2-Dec-14 16:14
ayseff2-Dec-14 16:14 
GeneralNice Article Pin
Rajin Sayeed S Alam15-May-11 19:07
Rajin Sayeed S Alam15-May-11 19:07 
GeneralMy vote of 3 Pin
fertansa2-Mar-11 11:21
fertansa2-Mar-11 11:21 
Newsbroken code - do not use as-is - please read the RFC and validate Pin
rlively6-Apr-10 8:16
rlively6-Apr-10 8:16 
GeneralRe: broken code - do not use as-is - please read the RFC and validate Pin
Member 1028103924-Feb-14 23:22
Member 1028103924-Feb-14 23:22 
Generalmsg.To cannot be assigned Pin
James_Lin11-May-09 10:12
James_Lin11-May-09 10:12 
GeneralAppending strings to get vCalendar: wrong, Using MS API: right Pin
rlively10-Oct-08 5:40
rlively10-Oct-08 5:40 
GeneralReminder & Busy Status Pin
Member 44677752-Oct-08 3:56
Member 44677752-Oct-08 3:56 
QuestionAppointment vs Task Pin
centric5-Mar-07 7:57
centric5-Mar-07 7:57 
GeneralHave a little problem Pin
uddesh14-Feb-07 18:37
uddesh14-Feb-07 18:37 
GeneralRe: Have a little problem Pin
Filip C2-Apr-09 1:43
Filip C2-Apr-09 1:43 
GeneralOther people's tasks Pin
Kokas1-Nov-05 2:10
Kokas1-Nov-05 2:10 
GeneralRe: Other people's tasks Pin
stehlewm21-Jul-06 4:43
stehlewm21-Jul-06 4:43 
QuestionAnd with no attachment? Pin
joujoukinder1-Sep-05 20:51
joujoukinder1-Sep-05 20:51 
QuestionOutlook neets to run in background ? Pin
mvanschie27-Jul-05 22:12
mvanschie27-Jul-05 22:12 
Nice code.
I've tried this to create a small taskrequest utility, but found that outlook still needs to run in the background.

I get an error message that the task has been saved as a file and can not be send.

Local test are fine, but testing from a remote computer gives me this error.

Is there a way to use this code without outlook running.

<br />
		private void button1_Click(object sender, System.EventArgs e)<br />
		{<br />
			// Create an Outlook Application object. <br />
			Outlook.ApplicationClass outLookApp = new Outlook.ApplicationClass();<br />
			// Create a new TaskItem.<br />
			Outlook.TaskItem newTask = <br />
				(Outlook.TaskItem)outLookApp.CreateItem(Outlook.OlItemType.olTaskItem);<br />
			// Configure the task at hand and save it.<br />
			newTask.Body = "Name : " + txtName.Text + "\r\n";<br />
			newTask.Body += "Product : " + txtProduct.Text + "\r\n";<br />
			newTask.Body += "Company : " + txtCompany.Text + "\r\n";<br />
			newTask.Body += "Amount : " + txtCatalogNr.Text + "\r\n";<br />
			newTask.Body += "Price : " + txtPrice.Text + "\r\n";<br />
			newTask.Body += "Amount : " + txtAmount.Text + "\r\n";<br />
<br />
			newTask.Recipients.Add("xxxxxxxx@xxxx.nl");<br />
			newTask.DueDate = DateTime.Now;<br />
			newTask.Importance = Outlook.OlImportance.olImportanceHigh;<br />
			newTask.Subject = "Order: " + txtProduct.Text;<br />
			newTask.Assign();<br />
			newTask.Send();<br />
			//newTask.Save();<br />
<br />
		}<br />

AnswerRe: Outlook neets to run in background ? Pin
stehlewm21-Jul-06 4:37
stehlewm21-Jul-06 4:37 
GeneralNice, But... Pin
FZelle25-Jul-05 4:52
FZelle25-Jul-05 4:52 

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.