Skip to footer content
USING IRONPDF

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF

Automating document delivery is a common requirement in .NET applications. Whether distributing invoices, reports, or receipts, developers need a reliable way to create PDF documents programmatically and send them directly via email. The ability to send PDF documents as email attachments streamlines business workflows and improves customer communication. In many event-driven applications, such as button clicks or scheduled tasks, an email-sending method may even include parameters like string subject, object sender and EventArgs as part of the system’s workflow. This ensures your process is efficient even when handling a dim document or other dynamically generated assets.

This tutorial demonstrates how to combine IronPDF's HTML to PDF conversion capabilities with C# email functionality and how to send generated PDF file as attachment in email from C#. Using C#, developers can generate a PDF file, attach it to a mail message, and send an email to any recipient in just a few lines of code.

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 1 - IronPDF

What Tools Are Needed for PDF Generation and Email in C#?

Two components make this workflow possible: a PDF generation library and an email sending mechanism. IronPDF handles PDF document creation from HTML content, while .NET supports email capabilities through System.Net.Mail or the modern MailKit library.

Install the IronPDF NuGet package via Visual Studio's Solution Explorer or Package Manager Console:

Install-Package IronPdf

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 2 - Installation

For modern email functionality, install MailKit:

Install-Package MailKit

IronPDF uses a Chromium-based rendering engine that produces pixel-perfect PDFs from HTML, CSS, and JavaScript. The library works across Windows, Linux, and macOS environments, and .NET Core applications benefit from cross-platform compatibility.

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 3 - Features

How Can Developers Generate a PDF in Memory Using C#?

The ChromePdfRenderer class converts HTML content to PDF documents. Rather than saving the PDF file to disk, the resulting PdfDocument object provides direct access to the binary data through its BinaryData property or via a new MemoryStream via its Stream property.

using IronPdf;
// Create a new instance of the renderer
var renderer = new ChromePdfRenderer();
// Generate PDF document from HTML content
string htmlContent = @"
    <h1>Order Confirmation</h1>
    <p>Thank you for your purchase.</p>
    <p>Order Total: $99.99</p>";
PdfDocument PDF = renderer.RenderHtmlAsPdf(htmlContent);
// Access PDF as byte array for file attachment
byte[] pdfBytes = pdf.BinaryData;
using IronPdf;
// Create a new instance of the renderer
var renderer = new ChromePdfRenderer();
// Generate PDF document from HTML content
string htmlContent = @"
    <h1>Order Confirmation</h1>
    <p>Thank you for your purchase.</p>
    <p>Order Total: $99.99</p>";
PdfDocument PDF = renderer.RenderHtmlAsPdf(htmlContent);
// Access PDF as byte array for file attachment
byte[] pdfBytes = pdf.BinaryData;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The RenderHtmlAsPdf method accepts an HTML string and returns a fully rendered PDF document. The BinaryData property provides the PDF content as a byte array, which is the exact format needed for adding attachments to an email message. This approach avoids file system operations entirely, keeping the PDF file in memory throughout the entire process from generation to delivery as shown in the following screenshot.

Output

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 4 - PDF Output

For more complex documents, IronPDF supports external CSS and images through the optional BasePath parameter, enabling professional document layouts with branded styling.

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 5 - How to send generated PDF file as attachment in email from C# - IronPDF

How is an In-Memory PDF Attached to an Email Message?

MailKit provides a modern, Microsoft-recommended approach for sending emails in .NET applications and web applications. The BodyBuilder class handles attachments by accepting byte arrays directly, making integration with IronPDF straightforward.

using IronPdf;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
// Generate PDF document with IronPDF
var renderer = new ChromePdfRenderer();
string HTML = "<h1>Monthly Report</h1><p>Generated automatically.</p>";
PdfDocument PDF = renderer.RenderHtmlAsPdf(html);
// Build new email message
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender", "sender@example.com"));
message.To.Add(new MailboxAddress("Recipient", "recipient@example.com"));
message.Subject = "Your Report is Ready";
// Create body with PDF attachment
var builder = new BodyBuilder();
builder.TextBody = "Please find your report attached.";
builder.Attachments.Add("Report.pdf", pdf.BinaryData, new ContentType("application", "pdf"));
message.Body = builder.ToMessageBody();
// Send email via SMTP server
using var client = new SmtpClient();
await client.ConnectAsync("smtp.example.com", 587, SecureSocketOptions.StartTls);
await client.AuthenticateAsync("username", "password");
await client.SendAsync(message);
await client.DisconnectAsync(true);
using IronPdf;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
// Generate PDF document with IronPDF
var renderer = new ChromePdfRenderer();
string HTML = "<h1>Monthly Report</h1><p>Generated automatically.</p>";
PdfDocument PDF = renderer.RenderHtmlAsPdf(html);
// Build new email message
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender", "sender@example.com"));
message.To.Add(new MailboxAddress("Recipient", "recipient@example.com"));
message.Subject = "Your Report is Ready";
// Create body with PDF attachment
var builder = new BodyBuilder();
builder.TextBody = "Please find your report attached.";
builder.Attachments.Add("Report.pdf", pdf.BinaryData, new ContentType("application", "pdf"));
message.Body = builder.ToMessageBody();
// Send email via SMTP server
using var client = new SmtpClient();
await client.ConnectAsync("smtp.example.com", 587, SecureSocketOptions.StartTls);
await client.AuthenticateAsync("username", "password");
await client.SendAsync(message);
await client.DisconnectAsync(true);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code sample creates a complete working example of the PDF-to-email workflow in a single operation. The BodyBuilder.Attachments.Add method accepts three parameters: the file name recipients will see, the PDF byte array from IronPDF, and the MIME type specifying the attachment format as application/pdf. The async methods ensure the operation doesn't block the calling thread, which is essential for web applications handling multiple concurrent requests.

The SecureSocketOptions.StartTls parameter enables encrypted communication with the SMTP server address, protecting credentials and email body content during transmission. Replace the placeholder SMTP client settings with your email provider's configuration values. Note that services like Gmail require an app password for SMTP authentication.

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 6 - Cross-platform compatibility

How Can System.Net.Mail Be Used as an Alternative for File Attachment?

For projects using older .NET versions or requiring minimal dependencies, System.Net.Mail provides built-in email functionality. While Microsoft recommends MailKit for new development, this approach remains viable for simpler scenarios. The following code demonstrates a complete working example using the SmtpClient client and MailMessage classes.

using IronPdf;
using System.Net;
using System.Net.Mail;
// Generate PDF document
var renderer = new ChromePdfRenderer();
PdfDocument PDF = renderer.RenderHtmlAsPdf("<h1>Invoice #1001</h1>");
// Create new MailMessage with attachment
using var message = new MailMessage("sender@example.com", "recipient@example.com");
message.Subject = "Invoice Attached";
message.Body = "Your invoice is attached to this email.";
// Attach PDF file from new MemoryStream
var stream = new MemoryStream(pdf.BinaryData);
message.Attachments.Add(new Attachment(stream, "Invoice.pdf", "application/pdf"));
// Send email via SMTP server
using var client = new SmtpClient("smtp.example.com", 587);
client.Credentials = new NetworkCredential("username", "password");
client.EnableSsl = true;
await client.SendMailAsync(message);
using IronPdf;
using System.Net;
using System.Net.Mail;
// Generate PDF document
var renderer = new ChromePdfRenderer();
PdfDocument PDF = renderer.RenderHtmlAsPdf("<h1>Invoice #1001</h1>");
// Create new MailMessage with attachment
using var message = new MailMessage("sender@example.com", "recipient@example.com");
message.Subject = "Invoice Attached";
message.Body = "Your invoice is attached to this email.";
// Attach PDF file from new MemoryStream
var stream = new MemoryStream(pdf.BinaryData);
message.Attachments.Add(new Attachment(stream, "Invoice.pdf", "application/pdf"));
// Send email via SMTP server
using var client = new SmtpClient("smtp.example.com", 587);
client.Credentials = new NetworkCredential("username", "password");
client.EnableSsl = true;
await client.SendMailAsync(message);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The key difference here is creating a new MemoryStream from the byte array before attaching the PDF file. The Attachment class constructor accepts a stream, file name, and MIME type as parameters. Both the MailMessage and SmtpClient are wrapped in using statements to ensure proper resource disposal after sending completes. The message.Attachments.Add method creates a new attachment object from the stream.

Output

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 7 - Generated PDF Output

Choose MailKit for new projects requiring advanced features like OAuth authentication, IMAP support, or complex MIME handling. Use System.Net.Mail for simpler requirements or when minimising external dependencies is a priority.

What Are Practical Applications for PDF Email Attachments?

This PDF generation and email delivery pattern supports numerous business scenarios. Invoice automation sends purchase confirmations immediately after transactions complete. Report distribution delivers scheduled analytics to stakeholders without manual intervention. Receipt generation provides customers with instant documentation of their purchases. Each use case benefits from the ability to create a PDF document and send it as an attached file automatically.

The in-memory approach offers advantages over file-based alternatives. Applications avoid disk I/O overhead and eliminate temporary file cleanup requirements. For high-traffic web applications, this efficiency becomes particularly valuable.

IronPDF's rendering options allow customisation of margins, headers, footers, and page sizes. Developers can create professional document output suitable for customer-facing communications, including plain text or HTML-formatted email body content alongside the PDF attachment.

Conclusion

Combining IronPDF with C# email libraries creates an efficient workflow for automated document delivery. IronPDF's BinaryData property provides direct access to PDF content as a byte array, while MailKit or System.Net.Mail handle email transmission seamlessly. This approach keeps operations efficient by avoiding unnecessary file system interactions.

The pattern scales from simple notification emails to complex document automation systems. Whether building invoice systems, report generators, or customer communication tools, this in-memory PDF-to-email workflow provides a solid foundation. The complete working example code can be downloaded and adapted for specific project requirements.

Start a free trial to explore IronPDF's PDF generation capabilities, or view licensing options for production deployment.

How to Send Generated PDF File as Attachment in Email from C# Using IronPDF: Image 8 - Licensing

Frequently Asked Questions

How can I send a generated PDF as an email attachment in C#?

Using IronPDF, you can easily send generated PDF files as email attachments by integrating its PDF creation capabilities with .NET's email-sending features.

What are the benefits of sending PDF files via email in .NET applications?

Sending PDF files via email in .NET applications helps automate document delivery, streamlining business workflows and enhancing customer communication.

Can IronPDF handle dynamic content in PDFs for email attachments?

Yes, IronPDF is capable of dynamically generating PDF content, making it suitable for event-driven applications that require sending customized PDFs as email attachments.

What parameters are commonly used in email-sending methods with IronPDF?

Common parameters include the email subject, the sender's information, and EventArgs, which ensure efficient processing in event-driven applications.

Why is IronPDF suitable for automating document delivery?

IronPDF is suitable for automating document delivery because it provides reliable PDF creation and integrates seamlessly with C# email-sending capabilities.

Is it possible to schedule PDF email sending with IronPDF?

Yes, IronPDF can be integrated into scheduled tasks to automate sending PDF emails at specified times, improving workflow efficiency.

Does IronPDF support creating PDFs from various data sources for email attachments?

IronPDF supports creating PDFs from multiple data sources, allowing developers to generate comprehensive documents for email attachments.

How does IronPDF enhance email communication with customers?

By allowing the generation and sending of detailed PDF documents as attachments, IronPDF enhances the professionalism and clarity of email communication with customers.

Can I use IronPDF to send invoices and reports as PDF attachments?

Yes, IronPDF is ideal for generating and sending invoices, reports, and other documents as PDF attachments, catering to various business needs.

What role does IronPDF play in improving business workflows?

IronPDF improves business workflows by enabling the seamless creation and distribution of PDF documents, reducing manual intervention and errors.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More