Send SQL Report in E mail attachment using C# and ASP.NET

17,828

Solution 1

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

Solution 2

You could also use SSRS Subscriptions. It has email functionality built-in.

Prerequisites:

  • You have your report set up.
  • You have your SSRS report Server configured to send emails.
  • Your SSRS server is 2010 or later. I'm not sure if 2008 and before support subscriptions.

Microsoft's documentation from the UI standpoint

Code:

var service = new ReportingService2010();
service.Url = reportServiceURL;
service.Credentials = new NetworkCredential(userName, password, domain);

var reportPath = "your/report/location";

string report = $"{reportPath}YourReportName.rdl";
string fileName = $"Your Custom Report";

//If your report requires input parameters, specify them in an array.
ParameterValue[] reportParameters = new ParameterValue[1];

reportParameters[0] = new ParameterValue();
reportParameters[0].Name = "ID";
reportParameters[0].Value = ID.ToString();

ParameterValue[] extensionParams = new ParameterValue[11];//Adjust this if you omit parameters.

extensionParams[0] = new ParameterValue();
extensionParams[0].Name = "TO";
extensionParams[0].Value = "[email protected];[email protected];[email protected];"

//CC, can be omitted.
extensionParams[1] = new ParameterValue();
extensionParams[1].Name = "CC";
extensionParams[1].Value = "[email protected];[email protected];"

//BCC, can be omitted.
extensionParams[2] = new ParameterValue();
extensionParams[2].Name = "BCC";
extensionParams[2].Value = "[email protected];[email protected];"

//Reply to, where replies should go.
extensionParams[3] = new ParameterValue();
extensionParams[3].Name = "ReplyTo";
extensionParams[3].Value = "[email protected]"

//Include report as an attachment, this should be TRUE.
extensionParams[4] = new ParameterValue();
extensionParams[4].Name = "IncludeReport";
extensionParams[4].Value = "True"

//What you want the attached file to render as. You have some options.
extensionParams[5] = new ParameterValue();
extensionParams[5].Name = "RenderFormat";
extensionParams[5].Value = "PDF"; //pdf
//extensionParams[5].Value = "WORD"; //doc, word 2003 - 2007
//extensionParams[5].Value = "WORDOPENXML"; //docx, word 2010 - 2013+
//extensionParams[5].Value = "EXCEL"; //xls, excel 2003 - 2007
//extensionParams[5].Value = "EXCELOPENXML"; //xlsx, excel 2010 - 2013+
//extensionParams[5].Value = "IMAGE"; //TIFF file
//extensionParams[5].Value = "CSV"; //CSV file
//extensionParams[5].Value = "XML"; //XML file

//Optional, set the priority of the message.
extensionParams[6] = new ParameterValue();
extensionParams[6].Name = "Priority";
extensionParams[6].Value = "High";
//extensionParams[6].Value = "Normal";
//extensionParams[6].Value = "Low";

//Subject Line
extensionParams[7] = new ParameterValue();
extensionParams[7].Name = "Subject";
extensionParams[7].Value = "Your lovely report";

//Comment, I believe this is the "body" of the email.
extensionParams[8] = new ParameterValue();
extensionParams[8].Name = "Comment";
extensionParams[8].Value = "Hi there, <br/><br/>I have your report. Thanks for automating me out of the job, you programmer you!";

//Include a hyperlink to run the report in the email body?  Can be omitted.
extensionParams[9] = new ParameterValue();
extensionParams[9].Name = "IncludeLink";
extensionParams[9].Value = "False";

//If you want to send it "on behalf of" someone, use this. Can be omitted.
extensionParams[10] = new ParameterValue();
extensionParams[10].Name = "SendEmailToUserAlias";
extensionParams[10].Value = "[email protected]";

ExtensionSettings extSettings = new ExtensionSettings();
extSettings.ParameterValues = extensionParams;
extSettings.Extension = "Report Server Email";

//FYI date must be in ISO 8601 format. This sets it to one minute from now, and will only run once.
string matchData = $"<ScheduleDefinition><StartDateTime>{DateTime.Now.AddMinutes(1).ToString("s")}</StartDateTime></ScheduleDefinition>";

reportingService.CreateSubscription(reportName, extSettings, fileName, "TimedSubscription", matchData, reportParameters);
Share:
17,828
adopilot
Author by

adopilot

Hello ! I am coming from Bosnia and Herzegovina,Sarajevo capital, I work in family firm as an IT administrator, My primary task is to eventing works well and lot of other stuff such as buying software, buying equipment, administrating SQL servers and much more. Our Company business is retrial and We have shops all around country with more than 300 working station with more than 1000 employed. In free time I try to learn programing in .NET and I having fun driving motocross bike, some time by summer I do a solar gliding flaying as sport pilot. If You fund my English is bad do not be maid my education was mostly during the war in Bosnia, in that time we did not have to study much for good ratings. Best regards Admir

Updated on June 13, 2022

Comments

  • adopilot
    adopilot almost 2 years

    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

  • adopilot
    adopilot almost 15 years
    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