views:

3018

answers:

2

I am trying to send report from sql reportserver 2008 as e mail attachment using ASP.NET and C#, Till now I learned how to Get report as PDF in my code , Now I wanna combine line of codes like

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

Thanx in advance

+1  A: 

Maybe I getting this wrong (I know little about SSRS) but I think you should

  1. Save the file to the file system

    System.IO.File.WriteAllBytes("c:\temp\temp.pdf", bytes);
    
  2. Send the file by email

    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);
    }
    
Eduardo Molteni
Thanks System.IO works like charm, Only one little remark, firs argument is String address of file and second is byte[].Any way now I have an idea to make web service for doing this job,Will it be so hard and different than making just sample Web page
adopilot
You are right! Corrected the answer.
Eduardo Molteni
+4  A: 

Instead of saving the pdf to the file system first, you could try to use the byte[] returned from the report server and attach this:

MemoryStream ms = new MemoryStream(bytes);

mail.Attachments.Add(new Attachment(ms, "temp.pdf"));

For a quick fudge on one of my reports I did the following:

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"));

Hope this is helpful

Attaching without saving the file to the file system is a much more elegant solution, in my opinion. Thanks for the solution.
Mozy