【问题标题】:Attach a reportviewer PDF to outlook email C#, WInForms将报表查看器 PDF 附加到 Outlook 电子邮件 C#、WINForms
【发布时间】:2013-01-17 16:40:27
【问题描述】:

我正在尝试将由 ReportViewer 控件生成的 PDF 附加到 Outlook 电子邮件。我有 Office 2013。

当我尝试它时,会发生以下情况(例外:“抱歉,出了点问题。您可能想再试一次。”有什么想法吗?

private void btnEmail_Click(object sender, EventArgs e)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string filenameExtension;

        byte[] bytes = reportViewer1.LocalReport.Render(
            "PDF", null, out mimeType, out encoding, out filenameExtension,
            out streamids, out warnings);

        using (FileStream fs = new FileStream("Confirmation Letter.pdf", FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
            School school = School.FromID(Convert.ToInt32(booking.School));

            Outlook.Application outlookApp = new Outlook.Application();
            Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = SystemData.SystemInformation.GetValue("LetterSubject");
            mailItem.To = school.MainEmail;
            mailItem.Body = SystemData.SystemInformation.GetValue("LetterMessage");
            mailItem.Importance = Outlook.OlImportance.olImportanceLow;

            Attachment att = new Attachment(new MemoryStream(bytes), "Confirmation Letter.pdf");

            mailItem.Attachments.Add(att);
            mailItem.Display(true);
        }
    }

【问题讨论】:

  • IMO 在这里写你的代码比放一张图片要好..
  • @SonerGönül 是的,但图像显示了异常消息(是的,这可以手动添加到问题中)。

标签: c# winforms reporting-services


【解决方案1】:



在我研究 Outlook.MailItem.Attatchment.add() 函数时,它接受两个参数: 1. 要作为附件附加的文件的物理路径。 2. 一个邮件项目对象,它已经有一个文件作为附件。 (这又是一个物理文件路径。)

所以我们必须为attatchment.add() 方法传递一个物理文件路径

因此,在您的情况下,您必须将渲染流保存为硬盘上的文件,然后在此方法中传递文件的物理路径。

示例:MailItem.Attachment.add(@"C:\temp\abc.pdf");

参考以下链接

http://msdn.microsoft.com/en-us/library/office/ff869553.aspx

如果满意,标记答案。

愉快的编码:)

【讨论】:

    【解决方案2】:

    使用此代码,我认为这会起作用。

    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);
    
    oMsg.To = "";
    oMsg.Subject = "Here comes your subject";
    oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
    oMsg.HTMLBody = "text body"; //Here comes your body;
    oMsg.Attachments.Add("c:/yourSavedFile.pdf", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
    oMsg.Display(true);
    

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多