Exported Report as EmailAttachment

Stimulsoft Ultimate discussion
Post Reply
AbdulAleem
Posts: 37
Joined: Tue Sep 15, 2009 9:42 am
Location: jeddah

Exported Report as EmailAttachment

Post 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;
}
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Exported Report as EmailAttachment

Post 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.
AbdulAleem
Posts: 37
Joined: Tue Sep 15, 2009 9:42 am
Location: jeddah

Re: Exported Report as EmailAttachment

Post 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.
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Exported Report as EmailAttachment

Post 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.
AbdulAleem
Posts: 37
Joined: Tue Sep 15, 2009 9:42 am
Location: jeddah

Re: Exported Report as EmailAttachment

Post by AbdulAleem »


Thankyou so much,It is working now.
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Exported Report as EmailAttachment

Post by HighAley »

Hello.

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

Thank you.
Post Reply