Multipart E-mail Using MailMessage
Try sending the VCALENDAR as an Attachment
with the Inline
attribute set to true
:
using (MailMessage mm = new MailMessage("...", "...", "Subject here", "Body here")) //Pick whatever constructor you want
{
using (Attachment a = new Attachment("c:\\test.ics", "text/calendar")) //Either load from disk or use a MemoryStream bound to the bytes of a String
{
a.Name = "meeting.ics"; //Filename, possibly not required
a.ContentDisposition.Inline = true; //Mark as inline
mm.Attachments.Add(a); //Add it to the message
using (SmtpClient s = new SmtpClient("...")) //Send using normal
{
s.Send(mm);
}
}
}
EDIT
Okay, I've updated the code to not rely on a file, so that we're using the exact same ICS file. Update the strings at the top and the SmtpClient
if needed but otherwise leave the code exactly as is. The ICS is from the middle of this page.
String mailFrom = "[email protected]";
String mailTo = "[email protected]";
String mailSubject = "This is a test";
String mailBody = "<p><strong>Hello</strong> world</p>";
String smtpServer = "mail.example.com";
using (var mm = new MailMessage()) //Pick whatever constructor you want
{
mm.To.Add(mailFrom);
mm.From = new MailAddress(mailTo);
mm.Subject = mailSubject;
mm.Body = mailBody;
mm.IsBodyHtml = true;
String t = "BEGIN:VCALENDAR\r\n" +
"METHOD:REQUEST\r\n" +
"BEGIN:VEVENT\r\n" +
"DTSTAMP:20080325T202857Z\r\n" +
"DTSTART:20080325T200000Z\r\n" +
"DTEND:20080325T220000Z\r\n" +
"SUMMARY:Test meeting request\r\n" +
"UID:040000008200E00074C5B7101A82E00800000000B2BB07349575C80100000000000000001000000019BF8D0149C50643A81325C54140C093\r\n" +
"ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=\"Dan\":MAIL\r\n" +
" TO:[email protected]\r\n" +
"ORGANIZER;CN=\"Administrator\":MAILTO:[email protected]\r\n" +
"LOCATION: Here\r\n" +
"DESCRIPTION:Test Request\r\n" +
"SEQUENCE:0\r\n" +
"PRIORITY:5\r\n" +
"CLASS:\r\n" +
"CREATED:20080321T190958Z\r\n" +
"STATUS:CONFIRMED\r\n" +
"TRANSP:OPAQUE\r\n" +
"END:VEVENT\r\n" +
"END:VCALENDAR";
Byte[] bytes = System.Text.Encoding.ASCII.GetBytes(t);
using (var ms = new System.IO.MemoryStream(bytes))
{
using (var a = new Attachment(ms, "meeting.ics", "text/calendar")) //Either load from disk or use a MemoryStream bound to the bytes of a String
{
a.ContentDisposition.Inline = true; //Mark as inline
mm.Attachments.Add(a); //Add it to the message
using (SmtpClient s = new SmtpClient(smtpServer)) //Send using normal
{
s.Send(mm);
}
}
}
}
I believe you have to send your vCalendear (*.vcs) or iCalendar (*.ics) file as an attachment for Outlook to know what to do with it.
- http://support.microsoft.com/kb/287625
- http://blogs.msdn.com/b/webdav_101/archive/2008/02/26/building-vcalendar-and-ical-is-not-supported-by-ms.aspx
The recipient will then need to open the email in Outlook and double-click the attachment to import it into the Outlook/Exchange calendar.