Share via


Howto: Add a body part to a message using CDOSYS.

The sample below shows how to add an additional body part to a message using cdosys.  This example demonstrates adding a "text/calendar" (vcalendar) body part, however it should be possible to use this to add "text/plain", "text/html" and other types.  In the sample, "this.VCalendarText" holds the text to be written to the body part (in this case its a VCalendar meeting request.

//============================ VCalendar ==============================
if (this.AddVCalendar == true)
{
    oMsg.BodyPart.ContentClass = this.ContentClass;
    oMsg.BodyPart.ContentMediaType = this.ContentType;

    CDO.IBodyPart iBpVCalendar = oMsg.BodyPart.AddBodyPart(-1);

    //oMsg.BodyPart.Fields["urn:schemas:mailheader:content-type"].Value = "multipart/alternative";
    iBpVCalendar.ContentClass = "urn:content-classes:calendarmessage";
    iBpVCalendar.Fields["urn:schemas:mailheader:content-type"].Value = "text/calendar;method=REQUEST;name=\"meeting.ics\"";
    iBpVCalendar.Fields["urn:schemas:mailheader:content-transfer-encoding"].Value = "8bit";
    iBpVCalendar.Fields.Update();

    // ----------  Write VCALENDAR -------------------------
    ADODB.Stream stm = null;
    stm = iBpVCalendar.GetDecodedContentStream();
    //stm.Write(this.VCalendarText);
    stm.WriteText(this.VCalendarText, ADODB.StreamWriteEnum.stWriteLine);
    stm.Flush();
    stm.Close();
    oMsg.BodyPart.Fields.Update();

    // ----------  Clean-up -------------------------
    System.Runtime.InteropServices.Marshal.ReleaseComObject(iBpVCalendar);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(stm);
    iBpVCalendar = null;
    stm = null;
}

Here is an example of what might be written to the body part. Please note that sending meeting requests with a custom created VCALENDAR is not supported, however I'm including this for completeness.
 
Content-class: urn:content-classes:calendarmessage
Content-Type: text/calendar;
 method=REQUEST;
 name="meeting.ics"
Content-Transfer-Encoding: 8bit

BEGIN:VCALENDAR
METHOD:REQUEST
BEGIN:VEVENT
DTSTAMP:20080325T202857Z
DTSTART:20080325T200000Z
DTEND:20080325T220000Z
SUMMARY:Test meeting request
UID:040000008200E00074C5B7101A82E00800000000B2BB07349575C80100000000000000001000000019BF8D0149C50643A81325C54140C093
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="Myf":MAIL
 TO:myfriend@myserver.mycompany.com
ORGANIZER;CN="Me Myself":MAILTO:memyself@myserver.mycompany.com
LOCATION: Here
DESCRIPTION:Test Request\N
SEQUENCE:0
PRIORITY:5
CLASS:
CREATED:20080321T190958Z
STATUS:CONFIRMED
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR