The sample below shows how to add an additional body part to a message using System.Net.Mail. 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.
//=======================================================================// Set_SSNM_BodyPart_VCalendar// // oMsg - the message to modify.//// sBody - can be any body - text, html, vcalendar.//=======================================================================public bool Set_SSNM_BodyPart_VCalendar(ref System.Net.Mail.MailMessage oMsg, string sVCalendar){ bool bRet = false; bRet = AddMessageAlternateView(ref oMsg, sVCalendar, "text/calendar"); return bRet;}
//============================================================================// AddMessageAlternateView// // oMsg - the message to modify.//// sBody - can be any body - text, html, vcalendar.//// Content Types:// Text: "text/plain"// HTML: "text/html"// VCalendar: "text/calendar"//============================================================================public bool AddMessageAlternateView(ref System.Net.Mail.MailMessage oMsg, string sBody, string sContentType) { bool bRet = false; try { AlternateView avAlternateView = null; System.Net.Mime.ContentType ctContentType = new System.Net.Mime.ContentType(sContentType); avAlternateView = AlternateView.CreateAlternateViewFromString(sBody, ctContentType); oMsg.AlternateViews.Add(avAlternateView); bRet = true; } catch (Exception ex) { MessageBox.Show(ex.InnerException.ToString(), "Error Adding Alternate View for \"" + sContentType + "\"", MessageBoxButtons.OK); bRet = false; }
return bRet;}
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:calendarmessageContent-Type: text/calendar; method=REQUEST; name="meeting.ics"Content-Transfer-Encoding: 8bit
BEGIN:VCALENDARMETHOD:REQUESTBEGIN:VEVENTDTSTAMP:20080325T202857ZDTSTART:20080325T200000ZDTEND:20080325T220000ZSUMMARY:Test meeting requestUID:040000008200E00074C5B7101A82E00800000000B2BB07349575C80100000000000000001000000019BF8D0149C50643A81325C54140C093ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="MYF":MAIL TO:myfriend@myserver.mycompany.comORGANIZER;CN="Me Myself":MAILTO:memyself@myserver.mycompany.comLOCATION: HereDESCRIPTION:Test Request\NSEQUENCE:0PRIORITY:5CLASS:CREATED:20080321T190958ZSTATUS:CONFIRMEDTRANSP:OPAQUEEND:VEVENTEND:VCALENDAR