Hello,
I am having difficulty writing a method that will render my report as a PDf so that I can attach it to an e-mail. Here is my code:
I am receiving a PDF attached to my e-mail but it is smaller than expected and display an error message.
Thanks in advance!
I am having difficulty writing a method that will render my report as a PDf so that I can attach it to an e-mail. Here is my code:
[HttpPost][Route("api/supportingreports/email/{reportName}")]public bool SendReportViaEmail(string reportName){ // TODO verify user access to report try { var reportProcessor = new ReportProcessor(); var report = new Report {DocumentName = HttpContext.Current.Server.MapPath(string.Format("{0}{1}.trdx", FileUtility.ReportPath, reportName))}; var instanceReportSource = new InstanceReportSource {ReportDocument = report}; instanceReportSource.Parameters.Add(new Parameter("userId", 3)); instanceReportSource.Parameters.Add(new Parameter("eventId", 0)); instanceReportSource.Parameters.Add(new Parameter("exceptionId", 14)); instanceReportSource.Parameters.Add(new Parameter("employeeId", 3860)); var result = reportProcessor.RenderReport("PDF", instanceReportSource, new Hashtable()); var fileName = report.Name + "." + result.Extension; var path = Path.GetTempPath(); var filePath = Path.Combine(path, fileName); var mailMessage = new MailMessage {From = new MailAddress(ConfigurationManager.AppSettings["fromAddress"])}; using (var fs = new FileStream(filePath, FileMode.Create)) { fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length); fs.Seek(0, SeekOrigin.Begin); var a = new Attachment(fs, report.Name + "." + result.Extension); mailMessage.Attachments.Add(a); mailMessage.To.Add(new MailAddress("cbohatka@aztekweb.com")); mailMessage.Subject = "TEST"; var smtpClient = new SmtpClient(); smtpClient.Send(mailMessage); } } catch (Exception ex) { Debug.WriteLine(ex.Message); throw; } return true; // dummy return}I am receiving a PDF attached to my e-mail but it is smaller than expected and display an error message.
Thanks in advance!