0

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

4

1 回答 1

-1

在此代码中,您实际上并未附加文件,而是使用文本、HTML 和日历替代视图创建消息。但是,您不会在任何地方将文件附加到 MailMessage。

您已经声明了一个包含附件名称的字符串:

string attachmentName = "myPdf.pdf";

但是,永远不会再次引用此附件名称。如果您打算将此添加到消息中,则必须这样做:

msg.Attachments.Add(...)

您可以查看如何使用 System.Net.Mail 向电子邮件添加附件?有关如何实际添加附件的更多详细信息。

我希望我已经正确理解了这个问题,我希望这会有所帮助。

于 2013-02-03T07:23:50.950 回答