Multipage Email Using MailMessage

I have an Email that I want to send with C #, which has vCalendar and HTML body parts.

I created MailMessage and defined 2 alternative views:

 AlternateView avCal = new AlternateView("VCALENDAR:...", null, "text/calendar"); AlternateView avHtml = new AlternateView("<p>some html</p>", null, "text/html"); mailMessage.AlternateViews.Add(avCal); mailMessage.AlternateViews.Add(avHtml); 

This gives me a message with Content-Type from multipart/alternative .

This will show both the appointment of the calendar and part of the HTML on my webmail, but not Outlook.

How can I show two different parts, such as different types of content? What I'm looking for is more like Content-Type: multipart/mixed , where both alternate views are displayed.

EDIT

When I use the @Chris Haas method, I get closer, but the markup is not displayed. It seems to be ignoring MailMessage.IsBodyHtml = true

html not showing up

not quite sure how to view it in Outlook, but only the headers ...

 Return-Path: <*****@****.com> X-Footer: ZWJyaWRnZS5jb20= Received: from localhost ([127.0.0.1]) by mail.foo.com for *****@****.com; Wed, 2 Jan 2013 17:20:14 -0500 MIME-Version: 1.0 From: "George Washington" <*****@****.com> To: "George Washington" <*****@****.com> Date: 2 Jan 2013 17:29:14 -0500 Subject: To-Do: test test - test Content-Type: multipart/mixed; boundary=--boundary_0_4fbc08b4-2198-45b1-bf2e-9659179aad84 
+6
source share
5 answers

Try sending 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

Well, I updated the code to not rely on the file, so we use the same ICS file. Update the lines at the top and SmtpClient , if necessary, but otherwise leave the code exactly as it is. ICS is in the middle of this page .

  String mailFrom = " xyz@example.com "; String mailTo = " xyz@example.com "; 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: myuser@mydom.com \r\n" + "ORGANIZER;CN=\"Administrator\":MAILTO: administrator@mydom.com \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); } } } } 
+3
source

I believe that you need to send the vCalendear (* .vcs) or iCalendar (* .ics) file as an attachment for Outlook to know what to do with it.

Then, the recipient needs to open the email in Outlook and double-click the attachment to import it into the Outlook / Exchange calendar.

+2
source

I had the same problem; I could not get the invitation to display HTML without displaying tags. I was able to solve the problem with something similar (the f variable contains all the BEGIN: VCALENDAR elements):

 System.Net.Mime.ContentType calendarType = new System.Net.Mime.ContentType("text/calendar"); AlternateView ICSview = AlternateView.CreateAlternateViewFromString(f.ToString(), calendarType); AlternateView HTMLV = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html")); MailMessage email = new MailMessage(" from@example.com ", " to@example.com ", "subject", "body"); email.AlternateViews.Add(ICSview); email.AlternateViews.Add(HTMLV); SmtpClient client = new SmtpClient(); client.Send(email); 

I am sure that the code could be removed ...

+1
source

I had the same problem.

Try adding two alternative controls to MailMessage, one with text / content calendar, with ics file, one with text / html type text, with an email body.

Worked for me :)

+1
source

None of these suggestions work for me.

The closest thing I still have to do is plug in a MemoryStream with text / calendar as a MIME type. However, GMail does not recognize this file in the sense that it does not display the summary from the .ICS file, and it does not allow me to "Add to Calendar".

However, when I forward the same email to myself (or others) on GMail, GMail DOES displays the contents of .ICS. I'm going crazy trying to check out various solutions.

0
source

All Articles