Send Crystal Report as PDF Attachment in Email using C# and VB.Net in ASP.Net

Chriz
 
on Nov 25, 2020 04:12 AM
4482 Views

Hi,

I am using crystal reports to create a pdf attachment and send it by email. I don't get any errors when i run the code but I don't receive the email either.

Thank you in advance.

Here's my code as follows.

        private void sendEmail()
        {
            MailMessage message = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            ReportDocument cryRpt = new ReportDocument();


            cryRpt.Load(@"C:\test\report1.rpt");
            cryRpt.SetDatabaseLogon("username", "pass");
            
            Stream stream = cryRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            byte[] file = new byte[stream.Length];
            stream.Read(file, 0, (int)stream.Length);
            stream.Close();

            message.From = new MailAddress("a@a.com");
            message.To.Add(new MailAddress("b@b.com"));
            message.Subject = "Subject";
            message.Body = "Body";
            message.IsBodyHtml = true;

            if (file != null)
            {
                message.Attachments.Add(new Attachment(new MemoryStream(file), "attachment.pdf"));
            }

            smtp.Send(message);
        }
Download FREE API for Word, Excel and PDF in ASP.Net: Download
Andrea
 
on Nov 25, 2020 04:12 AM
Hi @Chriz,
Please try the following

Export Crystal Report to PDF and send in Email as attachment using VB.Net in Windows Application

It might help you.

Cheers Andrea.
dharmendr
 
on Nov 25, 2020 04:31 AM
Chriz says:
SmtpClient smtp = new SmtpClient();

Set NetworkCredential, Host, Port for SmtpClient.

Refer below article.

Export Crystal Report to PDF and send as Email Attachment in ASP.Net