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.
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)
app.CreateItem(Outlook.OlItemType.olTaskItem);
Set the properties of task as needed:
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:
string CreateTask(DateTime start, DateTime end, string sub, string msgBody)
{
StringBuilder sbvCalendar = new StringBuilder();
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");
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");
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");
sbvCalendar.Append("LOCATION;ENCODING=QUOTED-PRINTABLE: "
+ String.Empty);
sbvCalendar.Append("\n");
sbvCalendar.Append("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:"
+ msgBody);
sbvCalendar.Append("\n");
sbvCalendar.Append("SUMMARY;ENCODING=QUOTED-PRINTABLE:"
+ sub);
sbvCalendar.Append("\n");
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:
string sub = "Test";
string body = "Testing Task";
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;
DateTime start = DateTime.Parse("Jan 1, 2005");
DateTime end = DateTime.Parse("Jan 2, 2005");
DateTime ex = DateTime.Now;
string attachUrl =
"C:\Inetpub\wwwroot\WebApplication3\bin\ Test.vcs";
using(StreamWriter sw = new StreamWriter(attachUrl)
{
sw.Write(CreateTask (start, end, sub, body));
}
MailAttachment mAttachment = new MailAttachment(attachUrl);
msg.Attachments.Add(mAttachment);
SmtpMail.SmtpServer = "some-smtp-mail-server.com";
SmtpMail.Send(msg);
That�s it. Happy coding!