Exported Report as EmailAttachment
Posted: Thu Jul 12, 2012 10:52 am
Hi
I want to send email with exported report as attachment without using viewer,I have used the code (mentioned below)
The code is working fine when i use it in windows application,
But when i run this code in a windows service,it is throwing error at report.render()
Is there any other alternative for report.render()
Please help me it is very critical
private bool SendMail()
{
SmtpClient SClient = new SmtpClient();
System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage();
SClient.Host = "192.168.0.11";
SClient.Port = Convert.ToInt32(25);
MailAddress strFrom = new MailAddress("Abdulaleem@Almaalim");
string strTo = "Abdulaleem@Almaalim";
string strSubject = "Test";
string strEmailBody = @"
<html>
<head>
</head>
<body >
<div >
<table>
<tr>
<td>
</td>
</tr>
</table>
</div>
</body>
</html>";
msgMail.Body = strEmailBody;
msgMail.Subject = strSubject;
msgMail.To.Add(strTo);
msgMail.From = strFrom;
msgMail.IsBodyHtml = true;
StiReport report = new StiReport();
report.Load(@"C:\Test.mrt");
report.Compile();
report.Render();
MemoryStream ms = new MemoryStream();
report.ExportDocument(StiExportFormat.Pdf, ms);
ms.Seek(0, SeekOrigin.Begin);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(ms, "Receipt.pdf", "application/pdf");
msgMail.Attachments.Add(attachment);
if (strTo.Trim() != "")
{
try
{
SClient.Send(msgMail);
return true;
}
catch (Exception e1)
{
return false;
}
}
return false;
}
I want to send email with exported report as attachment without using viewer,I have used the code (mentioned below)
The code is working fine when i use it in windows application,
But when i run this code in a windows service,it is throwing error at report.render()
Is there any other alternative for report.render()
Please help me it is very critical
private bool SendMail()
{
SmtpClient SClient = new SmtpClient();
System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage();
SClient.Host = "192.168.0.11";
SClient.Port = Convert.ToInt32(25);
MailAddress strFrom = new MailAddress("Abdulaleem@Almaalim");
string strTo = "Abdulaleem@Almaalim";
string strSubject = "Test";
string strEmailBody = @"
<html>
<head>
</head>
<body >
<div >
<table>
<tr>
<td>
</td>
</tr>
</table>
</div>
</body>
</html>";
msgMail.Body = strEmailBody;
msgMail.Subject = strSubject;
msgMail.To.Add(strTo);
msgMail.From = strFrom;
msgMail.IsBodyHtml = true;
StiReport report = new StiReport();
report.Load(@"C:\Test.mrt");
report.Compile();
report.Render();
MemoryStream ms = new MemoryStream();
report.ExportDocument(StiExportFormat.Pdf, ms);
ms.Seek(0, SeekOrigin.Begin);
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(ms, "Receipt.pdf", "application/pdf");
msgMail.Attachments.Add(attachment);
if (strTo.Trim() != "")
{
try
{
SClient.Send(msgMail);
return true;
}
catch (Exception e1)
{
return false;
}
}
return false;
}