Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
hi all,
I am using RDLC Reports in web application, when i click on email button report is sending in xls format only

I should send the email in any of the format which is selected by the user i.e. xls,PDF,CSV

How can send the email with attachment as pdf and csv

below is the code :

<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt"
InteractiveDeviceInfos="(Collection)" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt"
BackColor="#CCCCFF" ShowPrintButton="true" ShowPageNavigationControls="True"
ShowBackButton="True" ShowCredentialPrompts="True" ShowDocumentMapButton="True" ShowExportControls="True"
ShowFindControls="True" ShowParameterPrompts="True" ShowPromptAreaButton="True" ShowRefreshButton="True" ShowZoomControl="True" >

<LocalReport ReportPath="Report1.rdlc" >
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1" />
</DataSources>
</LocalReport >
</rsweb:ReportViewer>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="DataSet1TableAdaptersYourTableAdapter"></asp:ObjectDataSource>
<asp:LinkButton ID="lnkToCSV" runat="server" visible="false">ToCSV</asp:LinkButton>

///aspx.cs

private void SendPDFEmail(ReportViewer reportViewer)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{

StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b> Report</b></td></tr>");
sb.Append("<tr><td colspan = '2'></td></tr>");
sb.Append("Report1.rdlc");

sb.Append("</td></tr>");
sb.Append("</table>");
sb.Append("<br />");
sb.Append("<table border = '1'>");
sb.Append("<tr>");


sb.Append("</tr>");

sb.Append("</table>");
StringReader sr = new StringReader(sb.ToString());

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();

MailMessage mm = new MailMessage("aaa@gmail.com", "aaa@gmail.com");
mm.Subject = "iTextSharp PDF";
mm.Body = "iTextSharp PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "iTextSharpPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "aaa@gmail.com";
NetworkCred.Password = "<xxx>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
with above code i am getting error
How can i resolve it and how should i send the mail with PDF and CSV Format

Thanks in advance
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900