I am trying to email a Calendar Event to myself. I folowed the way Microsoft says to do it here: http://msdn2.microsoft.com/en-us/library/bb655909.aspx
The only difference is that I am using a memory stream and not a Response.Write.
I will show my code but let me expain the problem first.
When I do this the following it doesn't really show as a valid attachment. it does show the number of bytes of the attachment.
reminderMsg.Attachments.Add(
new Attachment(mStream, "Add2Calendar.ics", @"text/calendar"));
When I do the following it appears to be a valid attachment but when I click on it I get "The file cannot be previewed because there is no previewer installed for it".
reminderMsg.Attachments.Add(
new Attachment(mStream, "Add2Calendar.ics",MediaTypeNames.Text.Plain));
I know this all works in my Outlook because I have received email calendar attachement before.
Show I use "text/calendar"or MediaTypeNames.Text.Plain? Any help will be appreciated.
Here is my code:
MemoryStream mStream = new MemoryStream();
StreamWriter writer = new StreamWriter(mStream);writer.AutoFlush = true;
writer.WriteLine(
"BEGIN:VCALENDAR");writer.WriteLine("VERSION:2.0");
writer.WriteLine(
"PRODID:-//ICSTestCS/");writer.WriteLine("CALSCALE:GREGORIAN");
// Define time zones.
// US/Easternwriter.WriteLine("BEGIN:VTIMEZONE");
writer.WriteLine(
"TZID:US/Eastern");writer.WriteLine("BEGIN:STANDARD");
writer.WriteLine(
"DTSTART:20071104T020000");writer.WriteLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
writer.WriteLine(
"TZOFFSETFROM:-0400");writer.WriteLine("TZOFFSETTO:-0500");
writer.WriteLine(
"TZNAME:EST");writer.WriteLine("END:STANDARD");
writer.WriteLine(
"BEGIN:DAYLIGHT");writer.WriteLine("DTSTART:20070311T020000");
writer.WriteLine(
"RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3");writer.WriteLine("TZOFFSETFROM:-0500");
writer.WriteLine(
"TZOFFSETTO:-0400");writer.WriteLine("TZNAME:EDT");
writer.WriteLine(
"END:DAYLIGHT");writer.WriteLine("END:VTIMEZONE");
// US/Centralwriter.WriteLine("BEGIN:VTIMEZONE");
writer.WriteLine(
"TZID:US/Central");writer.WriteLine("BEGIN:STANDARD");
writer.WriteLine(
"DTSTART:20071104T020000");writer.WriteLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
writer.WriteLine(
"TZOFFSETFROM:-0500");writer.WriteLine("TZOFFSETTO:-0600");
writer.WriteLine(
"TZNAME:CST");writer.WriteLine("END:STANDARD");
writer.WriteLine(
"BEGIN:DAYLIGHT");writer.WriteLine("DTSTART:20070311T020000");
writer.WriteLine(
"RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3");writer.WriteLine("TZOFFSETFROM:-0600");
writer.WriteLine(
"TZOFFSETTO:-0500");writer.WriteLine("TZNAME:CDT");
writer.WriteLine(
"END:DAYLIGHT");writer.WriteLine("END:VTIMEZONE");
// US/Mountainwriter.WriteLine("BEGIN:VTIMEZONE");
writer.WriteLine(
"TZID:US/Mountain");writer.WriteLine("BEGIN:STANDARD");
writer.WriteLine(
"DTSTART:20071104T020000");writer.WriteLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
writer.WriteLine(
"TZOFFSETFROM:-0600");writer.WriteLine("TZOFFSETTO:-0700");
writer.WriteLine(
"TZNAME:MST");writer.WriteLine("END:STANDARD");
writer.WriteLine(
"BEGIN:DAYLIGHT");writer.WriteLine("DTSTART:20070311T020000");
writer.WriteLine(
"RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3");writer.WriteLine("TZOFFSETFROM:-0700");
writer.WriteLine(
"TZOFFSETTO:-0600");writer.WriteLine("TZNAME:MDT");
writer.WriteLine(
"END:DAYLIGHT");writer.WriteLine("END:VTIMEZONE");
// US/Pacificwriter.WriteLine("BEGIN:VTIMEZONE");
writer.WriteLine(
"TZID:US/Pacific");writer.WriteLine("BEGIN:STANDARD");
writer.WriteLine(
"DTSTART:20071104T020000");writer.WriteLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11");
writer.WriteLine(
"TZOFFSETFROM:-0700");writer.WriteLine("TZOFFSETTO:-0800");
writer.WriteLine(
"TZNAME:PST");writer.WriteLine("END:STANDARD");
writer.WriteLine(
"BEGIN:DAYLIGHT");writer.WriteLine("DTSTART:20070311T020000");
writer.WriteLine(
"RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3");writer.WriteLine("TZOFFSETFROM:-0800");
writer.WriteLine(
"TZOFFSETTO:-0700");writer.WriteLine("TZNAME:PDT");
writer.WriteLine(
"END:DAYLIGHT");writer.WriteLine("END:VTIMEZONE");
// Define the event.writer.WriteLine("BEGIN:VEVENT");writer.WriteLine("DTSTART;TZID=" + "Mountain" + ":");
writer.WriteLine(evt.StartTime.Year.ToString());
writer.WriteLine(FormatDateTimeValue(evt.StartTime.Month));
writer.WriteLine(FormatDateTimeValue(evt.StartTime.Day) + "T");
writer.WriteLine(evt.StartTime.ToShortTimeString());
writer.WriteLine("DTEND;TZID=" + "Mountain" + ":");
writer.WriteLine(evt.EndTime.Year.ToString());
writer.WriteLine(FormatDateTimeValue(evt.EndTime.Month));
writer.WriteLine(FormatDateTimeValue(evt.EndTime.Day) + "T");
writer.WriteLine(evt.EndTime.ToShortTimeString());
writer.WriteLine("SUMMARY:" + evt.Title);
writer.WriteLine(
"DESCRIPTION:" + evt.Description);writer.WriteLine("UID:1");
writer.WriteLine(
"SEQUENCE:0");writer.WriteLine("DTSTAMP:" + dtNow.Year.ToString());
writer.WriteLine(FormatDateTimeValue(dtNow.Month));
writer.WriteLine(FormatDateTimeValue(dtNow.Day) + "T");
writer.WriteLine(FormatDateTimeValue(dtNow.Hour));
writer.WriteLine(FormatDateTimeValue(dtNow.Minute) + "00");
writer.WriteLine(
"END:VEVENT");writer.WriteLine("END:VCALENDAR");
Configuration configurationFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.Config");System.Net.Configuration.MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as System.Net.Configuration.MailSettingsSectionGroup;
MailMessage reminderMsg = new System.Net.Mail.MailMessage(mailSettings.Smtp.From, recipient);reminderMsg.Subject = "Calendar Event";
reminderMsg.Body = "I won't show the body";
reminderMsg.IsBodyHtml = true;
reminderMsg.Priority = System.Net.Mail.
MailPriority.Normal;reminderMsg.Attachments.Add(new Attachment(mStream, "Add2Calendar.ics", @"text/calendar"));
reminderMsg.Headers.Add("Content-Type", @"text/calendar");
reminderMsg.Headers.Add("content-disposition", "attachment; filename=Add2Calendar.ics");System.Net.Mail.SmtpClient scMail = new System.Net.Mail.SmtpClient(mailSettings.Smtp.Network.Host, mailSettings.Smtp.Network.Port);
try
{
scMail.Send(reminderMsg);
}
catch (SmtpFailedRecipientsException ex)
{
//divError.InnerText = "The message did not reeach all intended recipients.";
}