How do I handle sending and receiving Appointments? I know for sending, the eml data should be stored in an AlternateView with the content type set to text/calendar, but for some reason it's not working for me.

Can someone shine some light toward helping me with this??? Thanks!

asked 02 Feb '11, 07:17

P-Money's gravatar image

P-Money
182
accept rate: 0%


To construct a calendar appointment, the following C# code can be used:

// plaintext and HTML descriptions (HTML version is required by Outlook)
string bodyText = "Please come to project XYZ review meeting.";
string bodyHtml = "<p>Please come to project XYZ review meeting.</p>";

// appointment data - see http://tools.ietf.org/html/rfc2445 for details
string appointmentData =
    "BEGIN:VCALENDAR\n" +
    "PRODID:Rebex Mail\n" +
    "VERSION:2.0\n" +
    "BEGIN:VTIMEZONE\n" +
    "TZID:US-Eastern\n" +
    "BEGIN:STANDARD\n" +
    "DTSTART:19981025T020000\n" +
    "RDATE:19981025T020000\n" +
    "TZOFFSETFROM:-0400\n" +
    "TZOFFSETTO:-0500\n" +
    "TZNAME:EST\n" +
    "END:STANDARD\n" +
    "BEGIN:DAYLIGHT\n" +
    "DTSTART:19990404T020000\n" +
    "RDATE:19990404T020000\n" +
    "TZOFFSETFROM:-0500\n" +
    "TZOFFSETTO:-0400\n" +
    "TZNAME:EDT\n" +
    "END:DAYLIGHT\n" +
    "END:VTIMEZONE\n" +
    "BEGIN:VEVENT\n" +
    "DTSTAMP:19980309T231000Z\n" +
    "UID:guid-1.host1.com\n" +
    "ORGANIZER;ROLE=CHAIR:MAILTO:mrbig@host.com\n" +
    "ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:MAILTO:employee-A@host.com\n" +
    "DESCRIPTION:Project XYZ Review Meeting\n" +
    "CATEGORIES:MEETING\n" +
    "CLASS:PUBLIC\n" +
    "CREATED:19980309T130000Z\n" +
    "SUMMARY:XYZ Project Review\n" +
    "DTSTART;TZID=US-Eastern:19980312T083000\n" +
    "DTEND;TZID=US-Eastern:19980312T093000\n" +
    "LOCATION:1CP Conference Room 4350\n" +
    "END:VEVENT\n" +
    "END:VCALENDAR\n";  

// construct the calendar view
AlternateView appointment = new AlternateView();
appointment.SetContent(appointmentData, "text/calendar");
appointment.ContentType.Parameters.Add("method", "REQUEST");

// construct the message
MailMessage message = new MailMessage();
message.BodyText = bodyText;
message.BodyHtml = bodyHtml;
message.AlternateViews.Add(appointment);

// save the message
message.Save(...);

To load and a calendar appointment and display its data, this should work:

// load the appointment
MailMessage appointment = new MailMessage();
appointment.Load(...);

// display text parts
Console.WriteLine("Plain text: {0}", appointment.BodyText);
Console.WriteLine("HTML text: {0}", appointment.BodyHtml);

// find and display the text/calendar part
foreach (AlternateView view in message.AlternateViews)
{
    if (view.ContentType.MediaType == "text/calendar")
    {
        Console.WriteLine("Appointment data: {0}", view.ContentString);
        break;
    }
}

If it still doesn't work for you, please let us know.

link

answered 02 Feb '11, 12:28

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28
accept rate: 31%

Thanks, but instead of saving the message can I email it to someone? For example if I email the message to someone and they receive in outlook it will add the appointment to their calendar.

(04 Feb '11, 02:47) P-Money

Yes, you can do that easily. Instead of calling the Save method, just send the message using Smtp object, as described in the following tutorials: http://www.rebex.net/secure-mail.net/tutorial-smtp.aspx#sending-mail

(04 Feb '11, 12:12) Lukas Pokorny ♦♦

Hey I finally got chance to try your code and it works for the most part.

I can send the appointments to outlook, but the content type of the email is being sent as "multipart/alternative"...

How do I change this to get it to show up as text/calendar???

Please help!

(14 Feb '11, 14:54) P-Money

Only the content type of the root MIME entity of the email is "multipart/alternative" - the last of its sub-entities is actually "text/calendar". This structure corresponds to calendar appointments generated by third-party software. However, in case you only need the text/calendar entity, I have added a separate answer for that.

(14 Feb '11, 22:16) Lukas Pokorny ♦♦

In case you prefer a message with a single text/calendar entity, you can use the following code to construct it:

string appointmentData =
    "BEGIN:VCALENDAR\n" +
    ... same as before ...
    "END:VCALENDAR\n";

// construct the calendar entity
MimeMessage appointment = new MimeMessage();
appointment.SetContent(appointmentData, "text/calendar");

// save the entity
appointment.Save(...);

The MimeMessage class is available in Rebex.Mime namespace. However, it looks like the other variant (text/calendar embedded in multipart/alternative) is slightly more compatible with third-party software (such as MS Outlook).

Again, instead of saving the MimeMessage, you can send it using the Smtp object.

link

answered 14 Feb '11, 22:15

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28
accept rate: 31%

Okay, thanks that worked.

But my issue now is that when I send the appointment to outlook, outlook doesn't display it the same (displaying an accept, decline and etc). Nor does outlook add the appointment to the calendar.

Do you have any insight on this?

And I do appreciate all your help, thanks!

(15 Feb '11, 16:54) P-Money

Is there a way to convert the string appointmentData above to an Appointment?

(17 Feb '11, 14:12) P-Money

The problem was the METHOD was not specified within the appointemt data and entity's Content-Type. This code works with my Outlook 2010. Please give it a try.

static string ConstructVCalendar(string from, string to, DateTime start, DateTime end, string summary, string description, string location)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("BEGIN:VCALENDAR\n");
    sb.Append("METHOD:REQUEST\n");
    sb.Append("VERSION:2.0\n");
    sb.Append("PRODID:Rebex Mail\n");
    sb.Append("BEGIN:VEVENT\n");
    sb.AppendFormat("DTSTAMP:{0}\n", DateTime.Now.ToUniversalTime().ToString(@"yyyyMMdd\THHmmss\Z"));
    sb.AppendFormat("UID:{0}\n", Guid.NewGuid());
    sb.AppendFormat("ORGANIZER:MAILTO:{0}\n", from);
    sb.AppendFormat("ATTENDEE;RSVP=TRUE:MAILTO:{0}\n", to);
    sb.AppendFormat("DTSTART:{0}\n", start.ToUniversalTime().ToString(@"yyyyMMdd\THHmmss\Z"));
    sb.AppendFormat("DTEND:{0}\n", end.ToUniversalTime().ToString(@"yyyyMMdd\THHmmss\Z"));
    sb.Append("CATEGORIES:MEETING,PROJECT\n");
    sb.Append("CLASS:PUBLIC\n");
    sb.AppendFormat("SUMMARY:{0}\n", summary);
    sb.AppendFormat("DESCRIPTION:{0}\n", description);
    sb.AppendFormat("LOCATION:{0}\n", location);
    sb.Append("END:VEVENT\n");
    sb.Append("END:VCALENDAR\n");
    return sb.ToString();
}

// ...
string from = "organizer@host.com";
string to = "attendee@host.cz";
DateTime start = new DateTime(2011, 2, 28, 10, 30, 0, DateTimeKind.Local);
DateTime end = start.AddHours(2); // duration is 2 hours

// appointment data - see http://tools.ietf.org/html/rfc2445 for details
string appointmentData = ConstructVCalendar(from, to, start, end, "My summary", "My description", "My location");

// construct the calendar entity
MimeMessage appointment = new MimeMessage();
appointment.SetContent(appointmentData, "text/calendar");
appointment.ContentType.Parameters.Add("method", "REQUEST");
appointment.From = from;
appointment.To = to;

Smtp.Send(appointment, "SMTP.server.name");

Please note the line sb.AppendFormat("UID:{0}\n", Guid.NewGuid()); This UID can be used lately to change this appointemnt.

link

answered 18 Feb '11, 14:46

Lukas%20Matyska's gravatar image

Lukas Matyska ♦♦
90117
accept rate: 28%

I need the code too but I realize it's just not as easy as the VCalendar. Look into the AppointmentItemClass in the interop library, what a pain, need to deal with COM+

link

answered 10 Mar '11, 23:22

Stanley's gravatar image

Stanley
16
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×53
×12
×4

Asked: 02 Feb '11, 07:17

Seen: 1,107 times

Last updated: 10 Mar '11, 23:22