簡體   English   中英

使用C#和ASP.NET在E郵件附件中發送SQL報告

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

我正在嘗試使用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提前

您可以嘗試使用從報表服務器返回的byte []並附加以下內容,而不是首先將pdf保存到文件系統:

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

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

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

希望這是有幫助的

也許我弄錯了(我對SSRS知之甚少),但我認為你應該這樣做

  1. 將文件保存到文件系統

     System.IO.File.WriteAllBytes("c:\\temp\\temp.pdf", bytes); 
  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); } 

您還可以使用SSRS訂閱。 它內置了電子郵件功能。

先決條件:

  • 您已設置報告。
  • 您已將SSRS報告服務器配置為發送電子郵件。
  • 您的SSRS服務器是2010或更高版本。 我不確定2008年是否支持訂閱。

從UI的角度來看Microsoft的文檔

碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM