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();
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); } }
source share