Articles in this section
Category / Section

Sending PDF Reports via Email Using BoldReports Writer in ASP.NET Core

Published:
Updated:

Introduction

This knowledge base article provides a step-by-step guide for sending PDF reports via email using BoldReports.Writer in an .NET Core application.

Prerequisites

Before you begin, ensure the following:

  • BoldReports.Writer library is added to your ASP.NET Core project.
  • SMTP server details (e.g., Gmail, Outlook, Office 365).

Steps to Send PDF Reports via Email

1. Set Up Your ASP.NET Core Project

Ensure your project is properly configured and the BoldReports.Writer library is integrated. Refer to the BoldReports documentation for guidance on creating a ReportWriter.

2. Add Required Namespaces

Add the following namespaces to your controller:

using System.Runtime.InteropServices;
using System.Net.Mail;
using System.Collections.Generic; 

3. Generate PDF and Send via SMTP

Method: GetReport()
This method performs the following tasks:

  • Loads the RDL report from the file system.
  • Creates a PDF using BoldReports.Writer.
  • Saves the PDF temporarily to disk.
  • Sends the PDF as an email attachment.
  • Deletes the temporary file after sending.
public IActionResult GetReport()
{
   FileStream reportStream = new FileStream(_hostingEnvironment.WebRootPath + @"\\Resources\\sales-order-detail.rdl", FileMode.Open, FileAccess.Read);
   ReportWriter writer = new ReportWriter(reportStream);
   MemoryStream memoryStream = new MemoryStream();
   writer.Save(memoryStream, WriterFormat.PDF);
   string tempPdfPath = Path.Combine(_hostingEnvironment.ContentRootPath, "sales-order-detail.pdf");
   using (FileStream fileStream = new FileStream(tempPdfPath, FileMode.Create, FileAccess.Write))
   {
       memoryStream.WriteTo(fileStream);
   }
   memoryStream.Dispose();
   reportStream.Dispose();
   SendEmailWithAttachment(tempPdfPath, "Email Subject", "Email Body");
   System.IO.File.Delete(tempPdfPath);
   return Content("Email with attached report sent successfully!");
} 

4. Configure SMTP and Send Email

Method: SendEmailWithAttachment()
This method:

  • Creates an email message with subject, body, and attachment.
  • Configures the SMTP client with host, port, and credentials.
  • Sends the email securely using SSL.
  • Cleans up resources after sending.
private void SendEmailWithAttachment(string attachmentPath, string subject, string body)
{
   string smtpHost = "smtp.gmail.com";
   int smtpPort = 587;
   string smtpUser = "your-email@gmail.com";
   string smtpPass = "your-app-password";
   MailMessage mail = new MailMessage();
   mail.From = new MailAddress(smtpUser);
   mail.To.Add("recipient@example.com");
   mail.Subject = subject;
   mail.Body = body;
   Attachment attachment = new Attachment(attachmentPath);
   mail.Attachments.Add(attachment);
   SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort)
   {
       Credentials = new NetworkCredential(smtpUser, smtpPass),
       EnableSsl = true
   };
   smtpClient.Send(mail);
   attachment.Dispose();
   mail.Dispose();
} 

You can download the sample from here.

Send_PDF_Reports_via_Email.zip
Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied