在 Outlook 中添加 XtraReport 作为附件

Add XtraReport as attachment in outlook

这是我的代码:

   using (RepMissingStatusProject report = new RepMissingStatusProject())
        {
            report.DataSource = await inventory.RepMissingStatusProject(Convert.ToInt32(oListProject.cmbProject.EditValue)).ConfigureAwait(true);
            report.CreateDocument();

            Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

            MemoryStream mem = new MemoryStream();
            report.ExportToPdf(mem);
            mem.Seek(0, System.IO.SeekOrigin.Begin);

            oMsg.To = "Test@Test.com";
            oMsg.Subject = "Test";
            oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
            oMsg.Display(false); 
            oMsg.HTMLBody = "Veuillez trouver ci-joint:" + "<br />" + oMsg.HTMLBody;
            oMsg.Attachments.Add(mem, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
        }

Outlook 打开正常,所有数据都正确,但附件文件除外我收到此错误:

System.Runtime.InteropServices.COMException: 'Member not found. (Exception de HRESULT : 0x80020003 (DISP_E_MEMBERNOTFOUND))'

我该如何解决这个问题?
提前感谢您的帮助

更新:
我尝试使用此代码

System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(mem,ct);
attach.ContentDisposition.FileName = "État De Manque.pdf";

但我收到另一个错误

System.ArgumentException HResult=0x80070057 Message=Sorry, something went wrong. You may want to try again. StackTrace "at Microsoft.Office.Interop.Outlook.Attachments.Add(Object Source, Object Type, Object Position, Object DisplayName)\r\n at Smart_Industrial_Management.PL.FrmInventory.d__44.MoveNext() in D:\SIM Windows7\Smart Industrial Management\PL\FrmInventory.cs:line 859" string

oMsg.Attachments.Add(mem

第一个参数应该是文件在您的文件系统上的路径。 因此您需要将 pdf 导出到文件(使用随机生成的名称)并将路径作为第一个参数放入 oMsg.Attachments.Add() 方法调用中。

所以你的代码应该是 lilke

report.ExportToPdf(randomName);
...
oMsg.Attachments.Add(randomName,...);
File.Delete(randomName);