When I am trying to attach file in My Calendar event through C# code for Outlook I am getting this error and mail is getting triggered but without the attachment.
My Code as Bellow
private static MailMessage MailMessageComp(DateTime dtStartDateTime, DateTime dtEndDateTime, string sSubject,
string sSummary, string sLocation, string sOrganizerName, string sOrganizerEmail, MailAddressCollection attendeeList, DataMailItem _mailItem, bool bStatus) {
MailMessage msg = new MailMessage();
System.Net.Mime.ContentType textType = new System.Net.Mime.ContentType("text/plain");
System.Net.Mime.ContentType HTMLType = new System.Net.Mime.ContentType("text/html");
System.Net.Mime.ContentType calendarType = new System.Net.Mime.ContentType("text/calendar");
calendarType.Parameters.Add("method", ((bStatus) ? "CANCEL" : "REQUEST")); // "REQUEST");
calendarType.Parameters.Add("name", "meeting.ics");
AlternateView textView = AlternateView.CreateAlternateViewFromString(_mailItem.Body, textType);
msg.AlternateViews.Add(textView);
AlternateView HTMLView = AlternateView.CreateAlternateViewFromString(_mailItem.Body, HTMLType);
string attachmentName = "myPdf.pdf";
msg.AlternateViews.Add(HTMLView);
//create the Body in VCALENDAR format
string calDateFormat = "yyyyMMddTHHmmssZ";
string bodyCalendar = "BEGIN:VCALENDAR\r\n" + "METHOD:" + ((bStatus) ? "CANCEL" : "REQUEST") + "\r\n" + "PRODID:Microsoft CDO for Microsoft Exchange\r\n" + "VERSION:2.0\r\n"
+ "BEGIN:VTIMEZONE\r\n" + "TZID:(GMT-06.00) Central Time (US & Canada)\r\n" + "X-MICROSOFT-CDO-TZID:11\r\n" + "BEGIN:STANDARD\r\n" + "DTSTART:16010101T020000\r\n"
+ "TZOFFSETFROM:-0500\r\n" + "TZOFFSETTO:-0600\r\n" + "RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=11;BYDAY=1SU\r\n" + "END:STANDARD\r\n" + "BEGIN:DAYLIGHT\r\n"
+ "DTSTART:16010101T020000\r\n" + "TZOFFSETFROM:-0600\r\n" + "TZOFFSETTO:-0500\r\n" + "RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=2SU\r\n"
+ "END:DAYLIGHT\r\n" + "END:VTIMEZONE\r\n" + "BEGIN:VEVENT\r\nDTSTAMP:{8}\r\n"
+ "ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=" + "TestFile.pdf" + ":{10}\r\n"
+ "DTSTART:{0}\r\n" + "SUMMARY:{7}\r\nUID:{5}\r\n" +
"{9}\":MAILTO:{9}\r\nACTION;RSVP=TRUE;CN=\"{4}\":MAILTO:{4}\r\nORGANIZER;CN=\"{3}\":mailto:{4}\r\nLOCATION:{2}\r\nDTEND:{1}\r\nDESCRIPTION:{7}\\N\r\nSEQUENCE:1\r\nPRIORITY:5\r\nCLASS:\r\nCREATED:{8}\r\nLAST-MODIFIED:{8}\r\nSTATUS:CONFIRMED\r\nTRANSP:OPAQUE\r\nX-MICROSOFT-CDO-BUSYSTATUS:BUSY\r\nX-MICROSOFT-CDO-INSTTYPE:0\r\nX-MICROSOFT-CDO-INTENDEDSTATUS:BUSY\r\nX-MICROSOFT-CDO-ALLDAYEVENT:FALSE\r\nX-MICROSOFT-CDO-IMPORTANCE:1\r\nX-MICROSOFT-CDO-OWNERAPPTID:-1\r\nX-MICROSOFT-CDO-ATTENDEE-CRITICAL-CHANGE:{8}\r\nX-MICROSOFT-CDO-OWNER-CRITICAL-CHANGE:{8}\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:REMINDER\r\nTRIGGER;RELATED=START:-PT00H15M00S\r\nEND:VALARM\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
bodyCalendar = string.Format(bodyCalendar,
dtStartDateTime.ToUniversalTime().ToString(calDateFormat),
dtEndDateTime.ToUniversalTime().ToString(calDateFormat),
sLocation,
sOrganizerName,
sOrganizerEmail,
Guid.NewGuid().ToString("B"),
sSummary,
((bStatus) ? "Cancelled " : "")+ sSubject,
DateTime.Now.ToUniversalTime().ToString(calDateFormat), BuildAttendees(attendeeList, ((bStatus) ? "CANCEL" : "NEEDS-ACTION"))
, GetRDocPDFAttachment(_mailItem.ProjectRefNo.ToString(), _mailItem.ResponseId.ToString()));
//attendeeList.ToString());
AlternateView calendarView = AlternateView.CreateAlternateViewFromString(bodyCalendar, calendarType);
//calendarView.TransferEncoding = TransferEncoding.Base64;
msg.AlternateViews.Add(calendarView);
// Adress the message
msg.From = new MailAddress(sOrganizerEmail, sOrganizerName);
foreach(MailAddress attendee in attendeeList) {
msg.To.Add(attendee);
}
msg.Bcc.Add(new MailAddress(_mailItem.From.EmailAddress, _mailItem.From.Name));
msg.Subject = sSubject + ((bStatus) ? " - Cancelled" : "");
return msg;
}
This MailMessage is getting composed and then I am mailing it.
Everything is fine except the attachment to calendar event.
Please help.
Thanking you in advance. Umakanta(Babu).