Aspose.Email  for .NET

Send email via SMTP in C#

A secure and flexible way to send emails in C# using SMTP, OAuth, and TLS. Includes async support and cross-platform compatibility.

  Download Free Trial

C# SMTP Client for Secure Email Delivery

SMTP (Simple Mail Transfer Protocol) is the standard protocol for sending email across networks. It operates at the application layer of the TCP/IP model, ensuring reliable and efficient transmission of data between servers, devices, and users.

Aspose.Email for .NET streamlines the use of the protocol in .NET applications with a secure, modern API. The library provides a modern, feature-rich SmtpClient designed to meet the needs of today’s secure, high-performance applications. As a powerful alternative to the obsolete System.Net.Mail.SmtpClient, it simplifies mail transmission in .NET environments by abstracting complex protocol-level operations and offering robust functionality for secure, flexible delivery.

With support for modern protocols like OAuth 2.0 and SSL/TLS, seamless async operations, and compatibility across platforms, it’s an ideal solution for developers building scalable systems without worrying about the low-level implementation of SMTP mechanics.

Key Features of Aspose.Email SMTP Client:

  • OAuth 2.0 authentication for secure, token-based sending

  • SSL/TLS encryption with automatic or configurable security negotiation

  • Asynchronous sending for non-blocking operations in web and background services

  • Cross-platform support on Windows, Linux, and macOS

  • Flexible delivery methods: network, pickup directory, or IIS SMTP service

  • Full proxy support to operate securely in restricted environments

  • Rich format handling including MSG, EML, MHT, PST, and others

  • Integrated message processing: parse, manipulate, and manage email content

  • Simple, high-level API that abstracts SMTP protocol complexities

Focus on crafting the content and business logic, while Aspose.Email reliably manages server communication, message formatting, and secure delivery. The library offers all the necessary classes and methods for making desirable configurations in C#. It is a powerful library that allows you to work with messages and provides functionalities to send, receive, and manipulate them. .NET developers can leverage its capabilities to reach a successful mail relay.

C# Example: Send Email Using SMTP Client

SMTP email sending in C# is simplified by handling message formatting, server connection, encryption, and authentication internally. Developers no longer need to manually manage low-level SMTP protocol details - just configure the client and send. This makes it easy to implement email features in ASP.NET and .NET applications using familiar C# syntax.

  1. Create a message using the MailMessage class.

  2. Configure the SMTP client with server address, port, credentials, and encryption settings.

  3. Send the message using SmtpClient.Send().

Send email sample code - C#

// Create a message
MailMessage message = new MailMessage();
message.From = new MailAddress("sender@example.com");
message.To.Add("recipient@example.com");
message.Subject = "Test Email";
message.Body = "This is a test email sent using Aspose.Email SMTP client.";

// Set up the SMTP client
SmtpClient client = new SmtpClient("smtp.example.com", 587);
client.Username = "your_smtp_username";
client.Password = "your_smtp_password";
client.SecurityOptions = SecurityOptions.Auto; // SSL/TLS negotiation

// Send the email
try
{
    client.Send(message);
    Console.WriteLine("Email sent successfully!");
}
catch (SmtpException ex)
{
    Console.WriteLine("Failed to send email: " + ex.Message);
}

This C# SMTP client example uses port 587 (STARTTLS) and auto-negotiates secure SSL/TLS encryption. It’s fully compatible with popular SMTP providers like Gmail, Outlook, or enterprise mail servers. For ASP.NET projects, asynchronous sending with SendAsync() is also supported.

Send Email Asynchronously via SMTP in C#

Asynchronous sending is essential for high-performance web apps, especially when SMTP servers may take time to respond or retry delivery. Aspose.Email for .NET supports async SMTP operations using the Task-based Asynchronous Pattern (TAP).

The following code sample demonstrates how to create a message and prepare it for sending using an asynchronous method with SMTP:

Code example for sending messages asynchronously

// Create and send a message
var message = new MailMessage("from@domain.com", "to@domain.com", "Async Subject", "Async Body");
var sendOptions = SmtpSend.Create();
sendOptions.AddMessage(message);

await client.SendAsync(sendOptions);
Console.WriteLine("Email sent asynchronously.");

For more information and code samples, please consult the SMTP Client to Send Emails, Forward Messages & Perform Mail Merge in C# in our user documentation.

Send Email Using OAuth 2.0

Modern SMTP servers - especially those from Microsoft (Outlook 365) and Google - require OAuth 2.0 for secure, password-free authentication. Aspose.Email for .NET fully supports sending messages with OAuth 2.0 tokens, eliminating the need to store user credentials.

Try the following code sample that demonstrates how to authenticate with OAuth and send a message:

OAuth 2.0 Authentication - C#

// Set up OAuth2 credentials
var tokenProvider = new TokenProvider(clientId, tenantId, redirectUri, scopes);

// Create the SMTP client using OAuth2 and connect
var client = await SmtpClient.CreateAsync("smtp.office365.com", username, tokenProvider, 587);

// Compose and send the message
var message = new MailMessage("sender@domain.com", "recipient@domain.com", "OAuth Subject", "Secure email body");
var options = SmtpSend.Create();
options.AddMessage(message);

await client.SendAsync(options);
Console.WriteLine("Message sent securely via OAuth 2.0.");

Install Aspose.Email for .NET

To start using the library, you need to install it in your project. The easiest way to do it is to use NuGet Package Manager:

Option 1: Using Visual Studio

  1. Open your C# project in Visual Studio.

  2. Right-click on the project name in Solution Explorer and select Manage NuGet Packages.

  3. Search for Aspose.Email and click Install.

Option 2: Using .NET CLI

Run the following command in your terminal:

dotnet add package Aspose.Email

Compatibility with .NET Framework, .NET Core, and .NET 5/6/7+ makes it ideal for modern desktop, web, and cloud-based applications.

Discover More with Aspose.Email for .NET

Beyond SMTP, the library enables complete email processing - from creating and parsing messages to managing PST/OST files, working with Microsoft Exchange, and integrating with Microsoft Graph. It supports mail merge, appointment scheduling, and complex conversions (EML ↔ MSG, MBOX ↔ PST, etc.), all without needing Microsoft Outlook or interop dependencies.

Explore everything you can build: View Full Feature List