2009-05-28 15 views
5

Sto cercando di inviare report da SQL Server Report 2008 come allegato di posta elettronica utilizzando ASP.NET e C#, Fino ad ora ho imparato come Get report as PDF nel mio codice, ora voglio combinare linea di codici comeInvia report SQL in allegato E mail utilizzando C# e ASP.NET

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 

Grazie in anticipo

risposta

3

Forse ho ottenere questo torto (io so poco di SSRS) ma penso che si dovrebbe

  1. salvare il file nel file system

    System.IO.File.WriteAllBytes("c:\temp\temp.pdf", bytes); 
    
  2. Invia il file per 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); 
    } 
    
+0

Grazie System.IO funziona come fascino, solo un po 'di osservazione, argomento abeti è l'indirizzo della stringa di file e il secondo è byte []. In qualsiasi modo ora ho un'idea di fare il servizio web per fare questo lavoro, Sarà così difficile e diverso da fare solo una semplice pagina Web – adopilot

+0

Hai ragione! Corretta la risposta –

13

Invece di salvare il pdf al file system prima, si potrebbe tentare di usare il byte [] restituito dal server di report e collegare questo:

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

Per una rapida fudge su un dei miei rapporti che ho fatto la seguente:

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

Spero che questo è utile

+0

Il collegamento senza salvare il file nel file system è una soluzione molto più elegante, secondo me. Grazie per la soluzione. – Mozy

+1

Funziona bene anche se dovevo usare System.Net.Mail piuttosto che potevo creare l'allegato da un MemoryStream. – openshac

1

Si potrebbe anche usare SSRS sottoscrizioni. Ha funzionalità di posta elettronica integrate.

Prerequisiti:

  • avete il vostro rapporto impostato.
  • Hai il tuo rapporto SSRS Server configurato per inviare e-mail.
  • Il server SSRS è 2010 o successivo. Non sono sicuro se il 2008 e prima degli abbonamenti di supporto.

Microsoft's documentation from the UI standpoint

Codice:

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