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
我正在尝试使用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
Thanx in advance
提前完成
3 个解决方案
#1
3
Maybe I getting this wrong (I know little about SSRS) but I think you should
也许我弄错了(我对SSRS知之甚少),但我认为你应该这样做
-
Save the file to the file system
将文件保存到文件系统
System.IO.File.WriteAllBytes("c:\temp\temp.pdf", bytes);
-
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); }
#2
14
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:
您可以尝试使用从报表服务器返回的byte []并附加以下内容,而不是首先将pdf保存到文件系统:
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
希望这有用
#3
1
You could also use SSRS Subscriptions. It has email functionality built-in.
您还可以使用SSRS订阅。它内置了电子邮件功能。
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.
您已设置报告。
您已将SSRS报告服务器配置为发送电子邮件。
您的SSRS服务器是2010或更高版本。我不确定2008年是否支持订阅。
Microsoft's documentation from the UI standpoint
从UI角度看微软的文档
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 = "BOB@company.com;Sharon@company.com;Bill.Gates@microsoft.com;"
//CC, can be omitted.
extensionParams[1] = new ParameterValue();
extensionParams[1].Name = "CC";
extensionParams[1].Value = "Doug@company.com;Hillary@company.com;"
//BCC, can be omitted.
extensionParams[2] = new ParameterValue();
extensionParams[2].Name = "BCC";
extensionParams[2].Value = "Doug@company.com;Hillary@company.com;"
//Reply to, where replies should go.
extensionParams[3] = new ParameterValue();
extensionParams[3].Name = "ReplyTo";
extensionParams[3].Value = "YourMonitoredMailbox@company.com"
//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 = "Bob.McCarthy@company.com";
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);
#1
3
Maybe I getting this wrong (I know little about SSRS) but I think you should
也许我弄错了(我对SSRS知之甚少),但我认为你应该这样做
-
Save the file to the file system
将文件保存到文件系统
System.IO.File.WriteAllBytes("c:\temp\temp.pdf", bytes);
-
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); }
#2
14
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:
您可以尝试使用从报表服务器返回的byte []并附加以下内容,而不是首先将pdf保存到文件系统:
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
希望这有用
#3
1
You could also use SSRS Subscriptions. It has email functionality built-in.
您还可以使用SSRS订阅。它内置了电子邮件功能。
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.
您已设置报告。
您已将SSRS报告服务器配置为发送电子邮件。
您的SSRS服务器是2010或更高版本。我不确定2008年是否支持订阅。
Microsoft's documentation from the UI standpoint
从UI角度看微软的文档
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 = "BOB@company.com;Sharon@company.com;Bill.Gates@microsoft.com;"
//CC, can be omitted.
extensionParams[1] = new ParameterValue();
extensionParams[1].Name = "CC";
extensionParams[1].Value = "Doug@company.com;Hillary@company.com;"
//BCC, can be omitted.
extensionParams[2] = new ParameterValue();
extensionParams[2].Name = "BCC";
extensionParams[2].Value = "Doug@company.com;Hillary@company.com;"
//Reply to, where replies should go.
extensionParams[3] = new ParameterValue();
extensionParams[3].Name = "ReplyTo";
extensionParams[3].Value = "YourMonitoredMailbox@company.com"
//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 = "Bob.McCarthy@company.com";
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);