![]() |
Web Development »
ASP.NET »
Reporting
Intermediate
License: The Code Project Open License (CPOL)
Send Mail and Print Report in Report Viewer ControlBy santosh poojariThis article demonstrates the implementation of Print and Send Mail operation in Report Viewer |
C#.NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, ASP.NET, Architect, Dev, Design
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I have been working with Report Viewer and have come across a lot of problems related to print option and mailing reports as an attachment to end users. This article aims to help developers facing similar problems.
Please refer to the following article:
One can drag the button and code the lines below so as to send a report as an attachment. In the code below, the attachment can be PDF, HTML, DOC, Excel, etc. For PDF, one needs to code reportViewer.LocalReport.Render("PDF", null,... The code here is self explanatory.
using System.Net.Mail;
private void SendMail(ReportViewer reportViewer)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = reportViewer.LocalReport.Render
("Excel", null, out mimeType, out encoding, out extension, out
streamids, out warnings);
MemoryStream memoryStream = new MemoryStream(bytes);
memoryStream.Seek(0, SeekOrigin.Begin);
MailMessage message = new MailMessage();
Attachment attachment = new Attachment(memoryStream,"BusinessReport.xls");
message.Attachments.Add(attachment);
message.From = new MailAddress("santosh.poojari@gmail.com");
message.To.Add("santosh.poojari@gmail.com");
message.CC.Add("santosh.poojari@gmail.com");
message.Subject = "Business Report";
message.IsBodyHtml = true;
message.Body = "Please find Attached Report herewith."
if (ConfigurationManager.AppSettings["SendMail"].ToString() == "Y")
{
SmtpClient smtp = new SmtpClient("SMTP Server Name");
smtp.Send(message);
}
else
{
//This is for testing.
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
memoryStream.Close();
memoryStream.Dispose();
}
< rsweb:ReportViewer ID="ReportViewer1" runat="server"
Font-Names="Verdana" Font-Size="8pt"
>
<LocalReport ReportPath="Report.rdlc" >
<DataSources >
< rsweb:ReportDataSource / >
</DataSources >
</LocalReport >
</rsweb:ReportViewer >
One can test this code snippet by adding the below configuration in web config.
< system.net >
< mailSettings >
< smtp deliveryMethod="SpecifiedPickupDirectory" >
< specifiedPickupDirectory pickupDirectoryLocation="C:\Test\" / >
</smtp >
</mailSettings>
</system.net >
Please refer to the link below:
Hope I met the expectation of developers working on similar problems.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Dec 2008 Editor: Deeksha Shenoy |
Copyright 2008 by santosh poojari Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |