Page 1 of 1

Exported Report as EmailAttachment

Posted: Thu Jul 12, 2012 10:52 am
by AbdulAleem
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;
}

Re: Exported Report as EmailAttachment

Posted: Fri Jul 13, 2012 10:56 am
by HighAley
Hello.
AbdulAleem wrote: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
At first try to call

Code: Select all

report.render(false)
If you still have a error please send us its text and full error stack.

Thank you.

Re: Exported Report as EmailAttachment

Posted: Sat Jul 14, 2012 7:31 am
by AbdulAleem
Hi,

When I use Report.Render(false) ,Mail is being send with pdf file as attachment, but Pdf file do not contain any pages,it has size of 6kb,
When I try to open the pdf in the attachment it gives the following message
"There was an error in opening the document,The file cannot be opened because it has no pages".

At Report.render(false) ,I am getting the error as follows :
"Could not load file or assembly 'System.Data.Dll' or one of its dependencies. The system cannot find the file specified"

Note : The above description is regarding code being executed in a windows service.

Thanks,
Abdul Aleem.

Re: Exported Report as EmailAttachment

Posted: Mon Jul 16, 2012 10:31 am
by Alex K.
Hello,

There is a strangeness in work of .NET Framework 4.0. In some cases, the compiled project cannot load the System.Data.dll system library, although the link to the library project is added.
You should do the following to solve this problem:
in start of your application please add the following lines of code:

using System.Data;
....
DataSet ds = null;

This lines of code will be force to load System.Data library in the memory.

http://stimulsoft.helpserve.com/index.p ... icleid=358
Thank you.

Re: Exported Report as EmailAttachment

Posted: Tue Jul 17, 2012 6:02 am
by AbdulAleem

Thankyou so much,It is working now.

Re: Exported Report as EmailAttachment

Posted: Tue Jul 17, 2012 11:02 am
by HighAley
Hello.

It's good. Let us know if you'll need any help.

Thank you.