Howto: Add a body part using System.Net.Mail
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: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