当我尝试通过C#代码为Outlook附加我的日历事件中的文件时,我收到此错误并且邮件被触发但没有附件。
我的代码为贝娄
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;
}
这个MailMessage正在编写,然后我发送邮件。
除了日历活动的附件外,一切都很好。
请帮忙。
提前感谢你。 Umakanta(巴布)。
答案 0 :(得分:-1)
在此代码中,您实际上并未附加文件,而是创建包含文本,HTML和日历备用视图的消息。但是,您无处将文件附加到MailMessage。
您声明了一个包含附件名称的字符串:
string attachmentName = "myPdf.pdf";
但是,此attachmentName永远不会再次引用。如果您要将此内容添加到邮件中,则必须执行以下操作:
msg.Attachments.Add(...)
您可以查看How do I add an attachment to an email using System.Net.Mail?,了解有关如何实际添加附件的更多详细信息。
我希望我能正确理解这个问题,希望这会有所帮助。