Sending Meeting Requests to Outlook via ASP.NET Mail Message

3 01 2009

Numerous programs on my company deal with event registration. Naturally, that involves sending our employees meeting requests. Here’s the best way I’ve found so far for doing that. The long function at the end is from a web-service already in use for a few months.

Meeting Request

Meeting Request

To make these ICS files dynamically, I use the .Net library DDay.iCal. It’s aewsome.
It took some trial and error, but at the end I was able to create events that work well for all versions of Outlook.
Unlike the solutions I’ve found on the web, this one doesn’t use the office interop thing, so I don’t need outlook installed on the server. I’m using a regular SMTP mail message and add the ICS as attachment.

Anyway, assuming you know how to send mails and how to generate the right ICS for your event (we’ll get to that later), this is the way to send it to Outlook:
First the variables:

//init the message with your defaults (from, to, subject, etc)
MailMessage message = initMailMessage();
string iCal = initICal(parameters ...);

And the code that adds the attachment:

//Add the attachment, specify it is a calendar file.
System.Net.Mail.Attachment attachment =
System.Net.Mail.Attachment.CreateAttachmentFromString(
iCal, new ContentType("text/calendar"));
attachment.TransferEncoding = TransferEncoding.Base64;
attachment.Name = "EventDetails.ics"; //not visible in outlook

message.Attachments.Add(attachment);

sendMailMessage(message);

Ok. That’s probably disappointing, since you can’t just copy-paste this code and make it work. I’m including more code you may want to use, but first some rules for the ICS files you’re about to create:

Rules of Thumb

  • Make sure to specify the Method property – should be Publish or Request. Outlook won’t accept the file without it.
  • Add an Organizer – or Outlook 2007 won’t let people save the event.
  • Add the meeting’s subject and description to the ICS file, but also as the mail’s subject and body. Outlook may display either one.

Now for that extra code.
This one is pretty simple – sends an SMTP message:

private static void sendMailMessage(MailMessage mailMessage)
{
string mailHost = "Ask.Someone.com";
SmtpClient smtpClient = new SmtpClient(mailHost, 25);
smtpClient.DeliveryMethod =
   SmtpDeliveryMethod.PickupDirectoryFromIis;
smtpClient.Send(mailMessage);
}

This fine function creates the ICS:

[WebMethod(Description =
   "Send an appointment with much details.")]
public void SendAppointment(string from, string to,
   string title, string body, DateTime startDate,
   double duration, string location, string organizer,
   bool updatePreviousEvent, string eventId,
   bool allDayEvent,
   int recurrenceDaysInterval, int recurrenceCount)
{
  iCalendar iCal = new iCalendar();

  // outlook 2003 needs this property,
  //  or we'll get an error (a Lunar error!)
  iCal.Method = "PUBLISH";

  // Create the event
  Event evt = iCal.Create();

  evt.Summary = title;

  evt.Start = new iCalDateTime(startDate.Year,
    startDate.Month, startDate.Day, startDate.Hour,
    startDate.Minute, startDate.Second);
  evt.Duration = TimeSpan.FromHours(duration);
  evt.Description = body;
  evt.Location = location;

  if (recurrenceDaysInterval > 0)
  {
    RecurrencePattern rp = new RecurrencePattern();
    rp.Frequency = FrequencyType.Daily;
    rp.Interval = recurrenceDaysInterval; // interval of days

    rp.Count = recurrenceCount;
    evt.AddRecurrencePattern(rp);
  }
  evt.IsAllDay = allDayEvent;

  //organizer is mandatory for outlook 2007 - think about
  // trowing an exception here.
  if (!String.IsNullOrEmpty(organizer))
    evt.Organizer = organizer;

  if (!String.IsNullOrEmpty(eventId))
    evt.UID = eventId;

  //"REQUEST" will update an existing event with the same
  // UID (Unique ID) and a newer time stamp.
  if (updatePreviousEvent)
    iCal.Method = "REQUEST";

  // Save into calendar file.
  iCalendarSerializer serializer =
    new iCalendarSerializer(iCal);
  //serializer.Serialize(@"iCalendar.ics");

  string icalData = serializer.SerializeToString();

  //send the iCal data. Also sends the subject and body
  //on the mail.
  SendAppointmentFromICalWithMailTitle(from, to,
    icalData, title, body);
}

See Also:


Actions

Information

26 responses

4 03 2009
Shahar Rubin

is there an option to send an outlook message via iCal ?

4 03 2009
Kobi

The meeting request is sent as an email message, so you don’t need iCal for that.
What exactly do you mean by ‘Outlook message’?

4 03 2009
Shahar Rubin

A regular message. I want to build a system that gets the message details and sends it on a set time (like 20:00) ;)

5 03 2009
Kobi

If all you need is a regular mail, I have a small function here that can do that, sendMailMessage. You can find many examples for that on the net.
If you’re in the army though, they’ve probably closed all mail servers and ports (I tried that a while back, Mor looked for them too). Mamram have a web service that sends mails, maybe you’ll have better luck getting a user than I did. (I found a name and password at a code example on z-net, and made a web service that encapsulates that on Eli’s server).

8 04 2009
Chris G

Hi Kobi,

I use this method but only can get a message with meeting request attachment. How can I get meeting request directly in outlook 2k3?
Thanks.

8 04 2009
Kobi

Hello Chris,
The key here is this line:
System.Net.Mail.Attachment.CreateAttachmentFromString(iCal, new ContentType(“text/calendar”));
This should turn the message into a meeting request. Also, make sure to include Method and Organizer.
A good tip here is to send there requests to gmail. You can than view the source and see exactly what’s gong on, by decoding the attachment from base 64.

9 04 2009
Chris G

Hi Kobi,
Your keys are already included in my codes, but still mails with attachment either received in outlook 2k3 or in gmail.
Kindly check my codes? Few changes from your codes.Thanks a lot!!

//Using DDay.iCal 0.7.0
//parameters
string title = “Test”;
string body = “Test body”;
DateTime startDate = DateTime.Now;
double duration = 1;
string location = “B4F1 Meeting Room”;
string organizer = “Chris G”;
bool updatePreviousEvent = false;
string eventId = “000832″;
bool allDayEvent = false;
int recurrenceDaysInterval = 0;
int recurrenceCount = 0;

iCalendar iCal = new iCalendar();

// outlook 2003 needs this property,
// or we’ll get an error (a Lunar error!)
iCal.Method = “PUBLISH”;

// Create the event
Event evt = iCal.Create();

evt.Summary = title;
evt.Start = new iCalDateTime(startDate.Year,startDate.Month, startDate.Day, startDate.Hour,startDate.Minute, startDate.Second);
evt.Duration = TimeSpan.FromHours(duration);
evt.Description = body;
evt.Location = location;

if (recurrenceDaysInterval> 0)
{
RecurrencePattern rp = new RecurrencePattern();
rp.Frequency = FrequencyType.Daily;
rp.Interval = recurrenceDaysInterval; // interval of days

rp.Count = recurrenceCount;
evt.AddRecurrencePattern(rp);
}
evt.IsAllDay = allDayEvent;

//organizer is mandatory for outlook 2007 – think about
// trowing an exception here.
if (!String.IsNullOrEmpty(organizer)) evt.Organizer = organizer;

if (!String.IsNullOrEmpty(eventId)) evt.UID = eventId;

//”REQUEST” will update an existing event with the same
// UID (Unique ID) and a newer time stamp.
if (updatePreviousEvent) iCal.Method = “REQUEST”;

// Save into calendar file.
iCalendarSerializer serializer =
new iCalendarSerializer(iCal);
//serializer.Serialize(@”iCalendar.ics”);

MailMessage msg = new MailMessage();
msg.From = new MailAddress(“AAA@BBB.com”);
msg.To.Add(“CCC@BBB.com”);
msg.Subject = title;
msg.Body = body;

Attachment att = Attachment.CreateAttachmentFromString(serializer.SerializeToString(), new ContentType(“text/calendar”));
att.TransferEncoding = TransferEncoding.Base64;
att.Name = eventId + “.ics”;

msg.Attachments.Add(att);

SmtpClient clt = new SmtpClient(“mailhost.BBB.com”);
try
{
clt.Send(msg);
}
catch { }

9 04 2009
Kobi

Hello Chris,
Could you try changing Organizer to an Email address?
(I would like making some tests, but we’re on holiday for the next two weeks!)

9 04 2009
Chris G

Kobi,

I have tried for Organizer = an Email address before but still a mail with attachment.

Have a good holiday!

3 05 2009
Kobi

Hello Chris,
I’ve tested your code, and it’s working well for me. I get a meeting request for both outlook and gmail.
Have you been able to solve this issue?
Thanks.

8 07 2009
shyamala

hi Kobi,

what dll should i add for icalendar class?

8 07 2009
Kobi

DDay.iCal.dll , here’s the link again: http://sourceforge.net/projects/dday-ical
They have a new version which I haven’t tested yet, but it should be ok.

9 07 2009
shyamala

Hi Kobi,

Thanks for your reply, really it helped me a lot.
But my problem is, if i send meeting request from my application, it is getting delivered as attachment in mail, but if i run the same application from other local machine it is delivered as meeting request. could you suggest me the reason for this ?

Thank you

11 07 2009
Kobi

Hello Shyam Mala,
I haven’t experienced this, my code worked for all the computers I’ve tested (though, all of the servers where Windows 2003. I’ll make another test tomorrow, on XP).
If you do figure the cause, which could be a configuration, a network permission, or whatnot, could you please update me? I’d like to share this with more people, as some have experience what you describe.
Thanks.

22 07 2009
shyamala

hi Kobi,

Yet I didn’t get solution for my previous post, if I find surely I will share it. Can we overwrite reminder from the application. i.e., when the meeting request is sent through the application, the default reminder is 15 min . I need to overwrite this 15 min as as 30 min or 1 hour through application. Is it possible?

regards,
shyamala

22 07 2009
shyamala

Hi Kobi,

How can we do recurrent meeting request which is similar to outlook Recurrence functionality?

22 07 2009
Kobi

Hello Again Shyan,
The code above has some basic recurrent
I’d like to kindly forward you to DDay.iCal’s Support Forums, they have answers to most questions on the ICS format.
Also, the code above has basic support for recurrence. For more complex patterns you may have to add more events to the same file (again, please check the documentations of DDay.iCal). Keep in mind you should test them with Outlook 2003 and 2007, they behave differently.
Thanks.

5 08 2009
PB

For anyone having trouble with outlook not detecting it as a meeting request you might want to try alternative views, they worked for me;

Some sample code..

MailMessage m = new MailMessage(new MailAddress(someone@email.com), new MailAddress(someoneelse@email.com));

ContentType calType = new ContentType(“text/calendar”);
calType.Parameters.Add(“method”, “REQUEST”);
calType.Parameters.Add(“name”, “meeting.ics”);

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, new ContentType(“text/html”);
AlternateView calendarView = AlternateView.CreateAlternateViewFromString(s.SerializeToString(), calType);
calendarView.TransferEncoding = TransferEncoding.SevenBit;

m.AlternateViews.Add(calendarView);
m.AlternateViews.Add(htmlView);
m.Body = body;
sendMailMessage(m);

where s is the iCalSerializer.

Also note that the value you use for calType.Parameters.Add(“method”, “REQUEST”) should be the same as that inside the event you have created. i.e. iCal.Method = “REQUEST”;

Hope that helps someone, you have no idea how many different permutations I’ve tried.

26 09 2009
Ron

Kobi,

Is there an easy way – for a non-programmer to send an appointment and invite attendees in the body of the message instead of an attachment?

Thank You,

Ron

26 09 2009
Kobi

Hi Ron,
Well, there are probably dozens of services that offer that – obviously Outlook if you’re working on a corporation, or Google Calendar.

30 09 2009
Ron

Thanks for the reply Kobi.

Would you be able to tell me how to send an invitation to users in the body of the message instead of as an attachment?

Thank You,

Ron

30 09 2009
Kobi

I’m not sure that’s even possible – but either way, it depends on your client.

30 09 2009
Arasi

How to send a meeting request in a C# web application?

30 09 2009
Kobi

Hello Arasi,
There is no difference. In fact, this code is from a web service, which is definitely a web application :)
It’s possible you already have an SmtpClient defined in your web.config, so you may be able to use that. Other than that, it should be good to go.

1 10 2009
Arasi

Hi Kobi,
SMTP client is not defined in web.config.

I am new to C# and unaware of mailing procedure
Please help me from the beginning how do i go about?
I used your code defined web service – and it gave me some errors:

  • “SendAppointmentFromICalWithMailTitle”.
  • Unable to create an event “Event evt”
  • also I need to provide meeting end date for the request

Please help me in this regard also

Thanks

3 10 2009
Kobi

Hi Arasi: Make sure you add a reference to the dday.ical dll, and add the right “using” statements. The rest or your questioned are mentioned in the post.
SendAppointmentFromICalWithMailTitle isn’t defined here – it’s a helper function left for implementation. I can’t really give you a step-by-step instruction, I think the tutorial id detailed enough, and it assumes you have experience with C#.
For starters, make sure you’re able to send regular Mail Messages, then add reference to dday.ical, and it should be a few easy steps later.

Leave a comment