读取超时和写入超时 以编程方式将文件附加到日历事件时引发的异常

本文关键字:超时 事件 日历 异常 文件 编程 读取 方式 | 更新日期: 2023-09-27 17:57:05

当我尝试通过 Outlook 的 C# 代码在我的日历事件中附加文件时,我收到此错误并且邮件被触发但没有附件。

我的代码作为波纹管

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;
    }

这封邮件正在撰写,然后我正在邮寄它。

一切都很好,除了日历事件的附件。

请帮忙。

提前谢谢你。乌马坎塔(巴布)。

读取超时和写入超时 以编程方式将文件附加到日历事件时引发的异常

在此代码中,您实际上不是在附加文件,而是在创建包含文本、HTML 和日历备用视图的消息。 但是,您不会在任何地方将文件附加到邮件中。

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

string attachmentName = "myPdf.pdf";

但是,此附件名称永远不会再次引用。 如果要将其添加到消息中,则必须使用以下命令:

msg.Attachments.Add(...)

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

我希望

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