使用C#和ASP.NET在E邮件附件中发送SQL报告

ado*_*lot 6 c# asp.net reporting-services

我正在尝试使用ASP.NET和C#从sql reportserver 2008发送报告作为电子邮件附件,直到现在我学会了如何在我的代码中获取PDF报告,现在我想要组合代码行

byte[] bytes = rview.ServerReport.Render(format, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
Response.OutputStream.Write(bytes, 0, bytes.Length);
Attachment reportAttachment = new Attachment(Response.OutputStream.Write(bytes,0,bytes.Length),"ado"); //Here I go wrong
Run Code Online (Sandbox Code Playgroud)

Thanx提前

小智 14

您可以尝试使用从报表服务器返回的byte []并附加以下内容,而不是首先将pdf保存到文件系统:

MemoryStream ms = new MemoryStream(bytes); 
mail.Attachments.Add(new Attachment(ms, "temp.pdf"));
Run Code Online (Sandbox Code Playgroud)

为了快速了解我的一份报告,我做了以下工作:

WebClient client = new WebClient();
byte[] bytes = client.DownloadData("http://localhost/ReportServer/Pages/ReportViewer.aspx? *** report name *** &rs%3aFormat=PDF");
MemoryStream ms = new MemoryStream(bytes);
mail.Attachments.Add(new Attachment(ms, "temp.pdf"));
Run Code Online (Sandbox Code Playgroud)

希望这是有帮助的


Edu*_*eni 2

也许我弄错了(我对 SSRS 知之甚少),但我认为你应该

  1. 将文件保存到文件系统

    System.IO.File.WriteAllBytes("c:\temp\temp.pdf", bytes);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过电子邮件发送文件

    MailMessage mail = new MailMessage();
    mail.From        = "Me";
    mail.To          = "You";
    mail.Subject     = "Subject";
    mail.Body        = "Body";
    mail.BodyFormat  = MailFormat.Html;
    mail.Attachments.Add(new MailAttachment("c:\temp\temp.pdf"));
    
    try
    {
       SmtpMail.Send(mail); 
    }
    catch(Exception ex)
    {
       Response.Write("Ouch! " + ex.Message);
    }
    
    Run Code Online (Sandbox Code Playgroud)