Monday, August 31, 2009

Creating a PDF with ReportViewer and emailing it

This code will render a report as a PDF file and then add it as an email attachment and send it. This is assuming you have a ReportViewer and it's all linked to a data source.

Source
Simply change fields that have in it to match your settings.

string mimeType;
string encoding;
string fileNameExtension;
Warning[] warnings;
string[] streamids;
byte[] bytes =
reportViewer1.LocalReport.Render("PDF",
null, out mimeType, out encoding, out fileNameExtension
, out streamids, out warnings);
Stream s = new MemoryStream(bytes);
SmtpClient sc = new SmtpClient("", );
MailMessage mm = new MailMessage(new MailAddress(""),
new MailAddress(""));
mm.Subject = "REPORT";
mm.Attachments.Add(new Attachment(s, "report" + "." + fileNameExtension));
mm.Body = "Report from report viewer is attached";
sc.Send(mm);

No comments:

Post a Comment

There was an error in this gadget