Click here to Skip to main content
Licence CPOL
First Posted 29 Dec 2008
Views 21,715
Bookmarked 38 times

Send Mail and Print Report in Report Viewer Control

By santosh poojari | 29 Dec 2008
This article demonstrates the implementation of Print and Send Mail operation in Report Viewer

1

2
3 votes, 50.0%
3
3 votes, 50.0%
4

5
3.45/5 - 6 votes
μ 3.45, σa 0.96 [?]

Introduction

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.

Bind RDLC -to Report Viewer

Please refer to the following article:

Send Mail Attachment of Report in Report Viewer

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

Design HTML

< rsweb:ReportViewer ID="ReportViewer1" runat="server" 
	Font-Names="Verdana" Font-Size="8pt"
                >
                <LocalReport ReportPath="Report.rdlc" >
                   <DataSources >
                        < rsweb:ReportDataSource / >
                    </DataSources >
                </LocalReport >
            </rsweb:ReportViewer >  

Web Configuration

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 >  

Print Operation in Report Viewer

Please refer to the link below:

Conclusion

Hope I met the expectation of developers working on similar problems.

History

  • 29th December, 2008: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

santosh poojari

Technical Lead

India India

Member
He is presently working as tech arch in one of the leading IT company.He has total 8 years of experience in C#.net. He is a B.E graduate in Computers from Bombay University.
 
Most of his experiences are in designing architect for end to end solutions. His interest areas are WCF,Spring.net,Architecture- Model View Presenter,UML,Webservice,Performance Engineering/tuning,Design patterns,Generics,Enterprise Library,Regular expressions,Silverlight and WWF.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120209.1 | Last Updated 29 Dec 2008
Article Copyright 2008 by santosh poojari
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid