
Sending emails with file attachments using C# is a common requirement in .NET development — whether it’s for sharing reports, invoices, or log files. While .NET’s built-in namespaces offer basic email support, things can become difficult when dealing with attachments, encoding, or dynamically generated content.
To simplify these tasks, this guide introduces how to send an email with attachment in C# using Spire.Email for .NET. We’ll cover how to attach files from disk, memory streams, or byte arrays, and walk you through configuring SMTP clients — including settings for Outlook. Whether you're working on a C# console app, an ASP.NET project, or other types of .NET applications, this guide provides practical examples to help you get started.
Table of Contents
- 1. Getting Started: Sending Emails in C# Using Spire.Email
- 2. Send Email with File Attachment from Disk in C#
- 3. Attach Files from MemoryStream or Byte Array
- 4. Send Email with Outlook SMTP in C#
- 5. Real-World Example
- 6. Troubleshooting Tips and Best Practices
- 7. Summary and Next Steps
- 8. Frequently Asked Questions (FAQ)
1. Getting Started: Sending Emails in C# Using Spire.Email
Spire.Email for .NET is a professional email library that enables .NET applications to create, send, receive, and manage emails without relying on Outlook or other third-party clients. It supports SMTP, POP3, and IMAP protocols, and handles attachments, HTML formatting, and inline resources with ease.
Installation
You can install the library via NuGet with the following command:
Install-Package Spire.Email
Download Spire.Email for .NET from the official page and install manually.
2. Send Email with File Attachment from Disk in C#
Sending file attachments via email is a common requirement in many C# applications—especially when automating tasks like batch reporting or file distribution. This section demonstrates how to send an email with a PDF file attached from the local disk using Spire.Email.
Key steps include:
- Setting up sender and recipient addresses
- Composing an HTML-formatted email body
- Adding a file attachment using the Attachment class
- Configuring and authenticating the SMTP client
This hands-on example provides a clear overview of how to send emails with attachments in C#, using just a few lines of code.
Example Code
using Spire.Email;
using Spire.Email.Smtp;
class Program
{
static void Main()
{
try
{
// Create MailAddress objects for sender, recipient, and CC
MailAddress addressFrom = new MailAddress("user@yourcompany.com", "Sender Name");
MailAddress addressTo = new MailAddress("recipient@yourcompany.com", "Recipient Name");
MailAddress addressCc = new MailAddress("copy@yourcompany.com", "Cc Name");
// Create a new MailMessage and set From and To addresses
MailMessage message = new MailMessage(addressFrom, addressTo);
// Add CC recipient
message.Cc.Add(addressCc);
// Set the email subject and body
message.Subject = "Monthly Report - June 2025";
message.BodyHtml = "<div style='font-family:Segoe UI, sans-serif; font-size:14px; color:#333; line-height:1.6;'>" +
"<p>Dear all,</p>" +
"<p>Please find the <strong style='color:#2E86C1;'>monthly report</strong> attached.</p>" +
"<p>If you have any questions, feel free to reach out at your convenience.</p>" +
"<p style='margin-top:30px;'>Best regards,</p>" +
"<p style='font-style:italic; color:#555;'>Sender Name</p>" +
"</div>";
// Add an attachment from file
String filePath = @"Sample.pdf";
Attachment attachment = new Attachment(filePath);
message.Attachments.Add(attachment);
// Configure the SMTP client
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.yourcompany.com";
smtp.Port = 587;
smtp.Username = "your_username";
smtp.Password = "your_password";
smtp.ConnectionProtocols = ConnectionProtocols.StartTls;
// Attempt to send the email
smtp.SendOne(message);
// If no exception is thrown, email was sent successfully
Console.WriteLine("Email sent successfully.");
}
catch (Exception ex)
{
// If an error occurs, print the error message
Console.WriteLine("Failed to send email.");
Console.WriteLine("Error: " + ex.Message);
}
}
}
The following screenshot shows the result of sending a simple email with a PDF attachment using the C# code above:

You may also like: Extract and Delete Email Attachments Using C#
3. Attach Files from MemoryStream or Byte Array to an Email
In many scenarios — such as exporting a document or image on the fly — you may need to send an email with attachment from a memory stream using C#, without saving the file to disk. Spire.Email makes this easy by allowing you to attach files directly from a MemoryStream or byte array.
Example: Attach a PDF from MemoryStream
using System.IO;
using Spire.Email;
String filePath = @"Sample.pdf";
MemoryStream stream = new MemoryStream(File.ReadAllBytes(filePath));
Attachment attachment = new Attachment(stream, Path.GetFileName(filePath);
message.Attachments.Add(attachment);
You can also attach from a byte array:
String filePath = @"Sample.pdf";
byte[] fileBytes = File.ReadAllBytes(filePath);
MemoryStream memStream = new MemoryStream(fileBytes);
Attachment imgAttachment = new Attachment(memStream, Path.GetFileName(filePath));
message.Attachments.Add(imgAttachment);
This is especially useful in web applications or services where you generate documents dynamically.
When creating an Attachment object using a stream, make sure the file name includes a valid extension (e.g., .pdf, .xlsx) to ensure the correct MIME type is recognized.
4. Send Email with Outlook SMTP in C#
If you're using an Outlook or Microsoft 365 account, you can send emails through their SMTP server using Spire.Email.
Outlook SMTP Configuration
smtp.Host = "smtp.office365.com";
smtp.Port = 587;
smtp.Username = "your_outlook_email@domain.com";
smtp.Password = "your_password";
smtp.ConnectionProtocols = ConnectionProtocols.StartTls;
Spire.Email fully supports TLS and STARTTLS, ensuring secure connections to Microsoft’s mail servers.
If you want to send emails in Outlook MSG format, please refer to: How to Send Outlook MSG Emails with Attachments in C#
5. Real-World Example: Sending C# Emails with Attachments and Dynamic Content
In a real-world business scenario, you often need to send project progress reports to multiple recipients, each with personalized content. Using Spire.Email for .NET, you can easily send individual emails with attachments and customized greetings—all within a simple C# application.
The example below demonstrates how to send a project status report to multiple clients, using dynamic recipient names in the greeting line and attaching PDF or Excel documents.
Full Code Example
using Spire.Email;
using Spire.Email.Smtp;
class Program
{
static void Main()
{
// Define sender
MailAddress addressFrom = new MailAddress("user@yourcompany.com", "Sender");
// Define recipient list with name and email
List<MailAddress> recipients = new List<MailAddress>
{
new MailAddress("sales@clientA.com", "Sales Team A"),
new MailAddress("manager@clientB.com", "Manager B"),
new MailAddress("support@clientC.com", "Support Team C")
};
// Attachments to be included
string[] attachmentFiles = { @"Sample.pdf", @"Sample.xlsx" };
// Configure SMTP once
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.yourcompany.com";
smtp.Port = 587;
smtp.Username = "your_username";
smtp.Password = "your_password";
smtp.ConnectionProtocols = ConnectionProtocols.StartTls;
// Send email to each recipient individually
foreach (var recipient in recipients)
{
try
{
// Create a new message for each recipient
MailMessage message = new MailMessage(addressFrom, recipient);
message.Subject = "Project Status Update - Q2 2025";
// Use the display name in the greeting
string greetingName = string.IsNullOrEmpty(recipient.DisplayName)
? "team"
: recipient.DisplayName;
// Mail content
message.BodyHtml = $@"
<div style='font-family:Segoe UI, sans-serif; font-size:14px; color:#333; line-height:1.6;'>
<p>Dear {greetingName},</p>
<p>Please find attached the <strong>Q2 2025 Project Status Report</strong>, covering:</p>
<ul style='padding-left:18px; margin:0;'>
<li>Milestones achieved</li>
<li>Pending tasks</li>
<li>Key insights</li>
</ul>
<p>If you have any questions or would like to discuss the details, feel free to reach out.</p>
<p>Best regards,<br><strong>Sender</strong></p>
</div>";
// Send the message
smtp.SendOne(message);
Console.WriteLine($"Email sent to {recipient.Address}.");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to send email to {recipient.Address}.");
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
This example is applicable to C# console applications, ASP.NET apps, or any .NET project requiring email functionality.
Below is a sample of the personalized project status email generated in the complete working example:

6. Troubleshooting Tips and Best Practices
Here are some common issues developers encounter when sending email with attachments in C#:
- Authentication errors: Ensure your SMTP credentials are correct and SSL/TLS is properly set.
- Blocked ports: Many hosting environments block port 25. Use 465 (SSL) or 587 (TLS) instead.
- Attachment size limits: Most SMTP servers restrict attachment size. Consider compressing or splitting files.
- Secure credential handling: Avoid hardcoding sensitive credentials in code. Use secure storage or environment variables.
- MIME type issues: If you're attaching files from byte arrays or memory streams, make sure the attachment name includes a valid extension so the MIME type can be correctly inferred.
By using Spire.Email, you can handle encoding, multi-format attachments, and protocol security with ease—without writing verbose code.
7. Summary and Next Steps
In this tutorial, we’ve shown how to send an email with attachment in C# using Spire.Email for .NET. From basic file attachments to advanced scenarios involving memory streams or Outlook SMTP integration, Spire.Email provides a streamlined and developer-friendly API.
If you're looking for a way to simplify email functionality in your .NET applications, Spire.Email is worth exploring. You can apply for a free temporary license and try it in your own projects.
8. Frequently Asked Questions (FAQ)
❓ How do I attach a file in an email using C#?
You can use the Attachment class from Spire.Email to attach a file like this:
message.Attachments.Add(new Attachment(@"file.pdf"));
You can also attach files from a MemoryStream or byte array if the file is generated in memory.
❓ How can I send an email with an attached file?
Use the SmtpClient.SendOne() method in Spire.Email to send the message after adding attachments with MailMessage.Attachments.Add(). Ensure your SMTP configuration and authentication details are correct.
❓ Can I send an email with attachment in .NET Core?
Yes. Spire.Email for .NET supports .NET Core, .NET Standard, .NET 5+, .NET Framework, ASP.NET, MonoAndroid, Xamarin.iOS, and works with both C# and VB.NET.
❓ How do I send an email with Excel attachment in C#?
You can attach Excel files like .xlsx or .xls just like any other file:
message.Attachments.Add(new Attachment(@"report.xlsx"));
Make sure the file path is valid and the file is accessible to the application.
