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

on Nov 25, 2020 04:12 AM


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.SetDatabaseLogon("username", "pass");
            Stream stream = cryRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            byte[] file = new byte[stream.Length];
            stream.Read(file, 0, (int)stream.Length);

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

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

Download FREE API for Word, Excel and PDF in ASP.Net: Download
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.
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