1

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).

Umakanta.Swain
  • 1,107
  • 2
  • 13
  • 24

1 Answers1

-1

In this code, you are not actually attaching a file, but are creating a message with text, HTML, and calendar alternate views. However, nowhere do you attach a file to the MailMessage.

You have declared a string holding an attachment name:

string attachmentName = "myPdf.pdf";

However, this attachmentName is never referenced again. If you mean to add this to the message, you must do so with:

msg.Attachments.Add(...)

You can see How do I add an attachment to an email using System.Net.Mail? for more details on how to actually add the attachment.

I hope I have understood the question correctly, and I hope this helps.

Community
  • 1
  • 1
J.T. Taylor
  • 4,147
  • 1
  • 23
  • 23
  • I have clearly mentioned attachment to the Calendar Event not to the mailmessage attachment. So My string composition contains the attachment procedure to attach which Like ATTACH: xxxxxxxxxxxx in the calendar string building with Base64 Conversion of the Attachment file. Please go through the question properly. – Umakanta.Swain Feb 03 '13 at 11:14
  • Well, your question is not the clearest one I have ever seen. At first, I thought maybe you meant attachment to the Calendar Event, but I could not see anything wrong with the code there, so I re-read the question and then thought "aha!", you were trying to attach the pdf file, as I did see something wrong there. I even built a test app to try this, so I am disappointed at the downvote as I put in some effort to understand this. Well, with downvoting people who are sincerely trying to help you, good luck getting this answered. – J.T. Taylor Feb 03 '13 at 15:47