Question on Sending calendar requests

bdinnocenzo

Active member
Joined
Oct 22, 2009
Messages
38
Location
Massachusetts
Programming Experience
5-10
I cobbled together the code below in an attempt to send a calendar request via email. Even though I've set the Content-Type to "text/calendar" and formed the VCalendar "body" correctly (I think) the email shows up as text with my header for Content-Type as well as another set to "text/plain". I've searched this forum as well as the web and have found some stuff, but it still has not been able to lead me to my error. I'm thinking this should not be as hard as it is...am I wrong?

Here's the code:

VB.NET:
        Dim SMTP As New SmtpClient
        Dim m As New MailMessage
        Dim Invite As New VCalendar
        Dim Body As String = ""


        With Invite
            .summary = txtBody.Text
            .location = txtLocation.Text
            .start_day = CInt(dtpStartDate.Value.ToString("dd"))
            .start_month = CInt(dtpStartDate.Value.ToString("MM"))
            .start_year = CInt(dtpStartDate.Value.ToString("yyyy"))
            .end_day = CInt(dtpEndDate.Value.ToString("dd"))
            .end_month = CInt(dtpEndDate.Value.ToString("MM"))
            .end_year = CInt(dtpEndDate.Value.ToString("yyyy"))
            .start_hour = CInt(dtpStartTime.Value.ToString("hh"))
            .start_minute = CInt(dtpStartTime.Value.ToString("mm"))
            .start_second = 0
            .end_hour = CInt(dtpEndTime.Value.ToString("hh"))
            .end_minute = CInt(dtpEndTime.Value.ToString("mm"))
            .end_second = 0
            .method = "REQUEST"
        End With

        FromMail = "info@somesite.com"
        Body = CreateCalendarRecord(Invite)
        SMTP.Host = "mail.somesite.com"
        SMTP.Port = 25
        m.From = New MailAddress(FromMail, "Invite")
        m.To.Add(txtRequired.Text)
        If txtOptional.Text.Length > 0 Then
            m.To.Add(txtOptional.Text)
        End If
        m.Subject = "Invite: " & txtSubject.Text
        m.IsBodyHtml = False
        m.Priority = MailPriority.Normal
        m.Body = Body ' & vbNewLine & "*~*~*~*~*~*~*~*~*~*" & _
                             vbNewLine & txtBody.Text

        [B]m.Headers.Add("Content-Type", "text/calendar; method=REQUEST;charset=""UTF-8""")[/B]
        m.Headers.Add("Content-Transfer-Encoding", "7bit")

        For i = 0 To m.Headers.Count - 1
            Debug.Print(i.ToString & " - " & m.Headers.Keys(i).ToString & ": " & _
                            m.Headers.Item(i).ToString)
        Next

        SMTP.DeliveryMethod = SmtpDeliveryMethod.Network
        Try
            SMTP.Send(m)        ' Send it
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Public Function CreateCalendarRecord(ByVal calEvent As VCalendar) As String
        Dim calRecord As String = ""
        calRecord = "BEGIN:VCALENDAR" & vbNewLine
        calRecord &= "PRODID:-//CalendarThing//EN" + vbNewLine
        calRecord &= "METHOD:" & calEvent.method & vbNewLine
        calRecord &= "VERSION:2.0" & vbNewLine
        calRecord &= "BEGIN:VEVENT" & vbNewLine
        calRecord &= "ORGANIZER:mailto:" & FromMail & vbNewLine
        calRecord &= "ATTENDEE;RSVP=YES:mailto:" & txtRequired.Text & vbNewLine
        calRecord &= "DTSTAMP:" & Now.Year.ToString("0000")  _
                  & Now.Month.ToString("00") & Now.Day.ToString("00") _
                  & "T" & Now.Hour.ToString("00") & Now.Minute.ToString("00") _
                  & Now.Second.ToString("00") & "Z" & vbNewLine
        calRecord &= "DTSTART:" & calEvent.start_year.ToString("0000") & _
                  calEvent.start_month.ToString("00") & _
                  calEvent.start_day.ToString("00") & _
                  "T" & calEvent.start_hour.ToString("00") & _
                  calEvent.start_minute.ToString("00") & _
                  calEvent.start_second.ToString("00") & "Z" & vbNewLine
        calRecord &= "DTEND:" & calEvent.end_year.ToString("0000") & _
                  calEvent.end_month.ToString("00") & _
                  calEvent.end_day.ToString("00") & _
                  "T" & calEvent.end_hour.ToString("00") & _
                  calEvent.end_minute.ToString("00") & _
                  calEvent.end_second.ToString("00") & "Z" & vbNewLine

        calRecord &= "LOCATION:" & calEvent.location & vbNewLine
        calRecord &= "SUMMARY:" & calEvent.summary & vbNewLine
        calRecord &= "DESCRIPTION:" & "Just a test for now" & vbNewLine

        ' Calculate unique ID based on current DateTime and its MD5 hash
        Dim strHash As String
        strHash = ""
        For Each b As Byte In (New MD5CryptoServiceProvider()). _
            ComputeHash(System.Text.Encoding.Default.GetBytes( _
            DateTime.Now.ToString()))
            strHash &= b.ToString("X2")
        Next

        calRecord &= "UID:" & strHash & vbNewLine
        calRecord &= "STATUS:CONFIRMED" & vbNewLine
        calRecord &= "END:VEVENT" & vbNewLine
        calRecord &= "END:VCALENDAR" ' & vbNewLine ' DO I NEED THIS LAST vbNewLine?
        Debug.Print(calRecord)

        Return calRecord
    End Function

When this shows up in my mail box, either Outlook 2007 or my same account accessed through the web, it is just a text message. Here's what comes through:

BEGIN:VCALENDAR
PRODID:-//CalendarThing//EN
METHOD:REQUEST
VERSION:2.0
BEGIN:VEVENT
ORGANIZER:mailto:info@somesite.com
ATTENDEE;RSVP=YES:mailto:bill@somesite.com
DTSTAMP:20091119T103615Z
DTSTART:20091119T103600Z
DTEND:20091119T113600Z
LOCATION:My Cyberspace Office
SUMMARY:This is the body of the invite...
DESCRIPTION:Just a test for now
UID:C86FD1CD55136D79A5C884071008D650
STATUS:CONFIRMED
END:VEVENT
END:VCALENDAR​

Any clues? Thanks,

-Bill
 
Last edited:
Wow...this is much more difficult than I ever imagined. I have spent the better part of this week scouring the web and anywhere else I might find info on sending calendar requests. There's lots of information, but when I follow all the instructions I find the emails do get through however the "Context-Type" gets duplicated and converted to "text\plain" from "text\calendar".

When I send an email with no body, just the header info and a subject line, the "Content-Type" does not get changed. As soon as the body contains anything, even one character...even "chr(0)" the problem of the additional "Content-Type" shows up. I suspect that mail clients (Outlook and web-based like Hord) read the body and set the "Content-Type" based on what it reads.

This is SOOOOOOO frustrating! :mad: Does anyone have any suggestions????

-Bill
 
Thanks for the input. I can in fact create a file and attach it to the email as a calendar request. The format for iCalendar and vCalendar are about the same. When I do this the email content type is changed to "multipart/mixed". It gets me closer, but not where i want to go.

I really want the email to show up as an invite and allow the user to accept or decline the invite without having to click on an attached file. This method works for Lotus Notes, but doesn't seem to fly with Outlook (I'm running Outlook 2007).

The trek continues...
 
Tried earlier with my Outlook 2002, and it gave me an attached file with ICS extension. On searching I found this page - not sure if it helps, but it seems to suggest that you have to format it as an attachment anyway.
 
Thanks. I'm having some luck. I did create an .ics file an attach it...seems to help somewhat. I need to "play around" with this a bit more. I have a couple of issues to deal with. First I want (have to) to send the invite using Lotus Notes to Lotus Notes users -- it's what we use at work :confused: (don't ask me why!)

My plan is to make it simple, get it to work sending an invite using System.Net.Mail to Outlook. Once that works then try sending from Lotus Notes to Outlook, then finally Notes to Notes.

Lotus Notes adds a whole new layer of complexity since they have extended the vCalendar in their own environment, but they seem to be compliant to the standard when sending and receiving invites to outside mailers.

Thanks for the link, it will be helpful. When I make any progress I'll be sure to post it back here.

-Bill
 
Did your vcalendar class come from this page? If so, I'll try it again with my Outlook for you :)
 
I did not get my vCalendar class from this site...it looks interesting. I basically rolled my own and for the most part is shown in my original post (though it's changed a million times since then). I will look at this class when I get a chance in the next few days. What does the "{0}" at the end of string literals represent? Is that a null terminator, and if so what's with the System.Environment.Newline? I guess I need to look into StringBuilder a bit more.

UPDATE: Never mind...I looked into StringBuilder...now I get it. I'll dig through the code when I get time in the next few days.
 
Last edited:
Back
Top