Get the file to send as an attachment from an array of bytes

I have an ASP.NET MVC application that should send an email to the recipient list with an application that details the specific "Project" in detail. I get the details of this report from a report using SQL Server Reporting Services (SSRS).

I have never used SSRS before, but I got the code from a colleague where he used it. What he does with the report, he downloads it in a browser to the client machine. So, if I do not do this, I am sitting with a byte array containing the report data.

Is there a way to send this as an attachment without first writing the file to the server file system? The report will be either in excel format or in pdf format.

Edit: I am using SmtpClient to send email.

+6
source share
2 answers

To do this, you need to use the SSRS ReportManager API as follows.

  • Read in a web service report first using SSRS
  • Read the file in memory, but do not save it on the server or client.
  • Send the MemoryStream object directly to the email server.

Reporting Services: Get the PDF file of the generated report. How do I send an email with attachments using SmtpClient.SendAsync?

 string strReportUser = "RSUserName"; string strReportUserPW = "MySecretPassword"; string strReportUserDomain = "DomainName"; string sTargetURL = "http://SqlServer/ReportServer?" + "/MyReportFolder/Report1&rs:Command=Render&rs:format=PDF&ReportParam=" + ParamValue; HttpWebRequest req = (HttpWebRequest)WebRequest.Create( sTargetURL ); req.PreAuthenticate = true; req.Credentials = new System.Net.NetworkCredential( strReportUser, strReportUserPW, strReportUserDomain ); HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse(); Stream fStream = HttpWResp.GetResponseStream(); HttpWResp.Close(); //Now turn around and send this as the response.. ReadFullyAndSend( fStream ); 

ReadFullyAnd send method. NB: a call to SendAsync, so you do not wait until the server sends the full email before you take the user out of the ground with a nod.

 public static void ReadFullyAndSend( Stream input ) { using ( MemoryStream ms = new MemoryStream() ) { input.CopyTo( ms ); MailMessage message = new MailMessage(" from@foo.com ", " too@foo.com "); Attachment attachment = new Attachment(ms, "my attachment",, "application/vnd.ms-excel"); message.Attachments.Add(attachment); message.Body = "This is an async test."; SmtpClient smtp = new SmtpClient("localhost"); smtp.Credentials = new NetworkCredential("foo", "bar"); smtp.SendAsync(message, null); } } 
+4
source

Get file data in byte[]

 byte[] binaryFile = // get your file data from the SSRS ... string filename = "SSRS.pdf"; 

Prepare a list or array of destination addresses:

 string[] addresses = // get addresses somehow (db/hardcoded/config/...) 

sending smtp message through SmtpClient :

 MailMessage mailMessage= new MailMessage(); mailMessage.From = new MailAddress("sender email address goes here"); // Loop all your clients addresses foreach (string address in addresses) { mailMessage.To.Add(address); } mailMessage.Subject = "your message subject goes here"; mailMessage.Body = "your message body goes here"; MemoryStream memoryStream = new MemoryStream(binaryFile); mailMessage.Attachments.Add( new Attachment( memoryStream, filename , MediaTypeNames.Application.Pdf )); SmtpClient smtpClient = new SmtpClient(); smtpClient.Send(mailMessage); 
+7
source

All Articles