Tag Archive | Meeting

Sending Meeting Requests to Outlook via ASP.NET Mail Message

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:

Follow

Get every new post delivered to your Inbox.