Aspose.Email  for .NET

Send Email via SMTP in C#

A step-by-step guide to send emails via SMTP. Configure SmtpClient using a progressive .NET library for fast and secure mail relay.

  Download Free Trial

Advanced C# SMTP Client for Secure Email Delivery

SMTP (Simple Mail Transfer Protocol) is a fundamental communication protocol that mail servers use to send and receive email messages. SMTP operates at the application layer of the TCP/IP model, ensuring reliable and efficient transmission of emails between servers, devices, and users.

With Aspose.Email for .NET, sending emails via SMTP is easy and efficient. Since the System.Net.Mail.SmtpClient class has been marked obsolete, which was once a popular way to send data in .NET applications, it is not recommended to use it for new development, as it doesn’t support many modern protocols. Aspose.Email for .NET offers a superior alternative, addressing the limitations of the old C# SMTP Client with modern security features, including OAuth2, SSL/TLS, and enhanced performance. It provides flexibility with asynchronous sending, multiple authentication options, and secure message handling, making it the go-to solution for .NET developers seeking a reliable and scalable email-sending solution.

Now developers don’t need to worry about the intricate details of the protocol - to fully understand or manually manage the complex, low-level processes involved in sending emails - Aspose.Email handles most of it internally. Establishing a connection to the server, handling authentication, formatting emails correctly, and managing the communication between the client and server have become abstract with the intuitive API that automates these processes. This allows developers to focus on high-level tasks like setting up the message content and recipient information, while the library takes care of all the protocol-specific details behind the scenes.

Key Features of Aspose.Email C# SMTP Client

Aspose.Email for .NET is an excellent choice for developers looking to integrate email-sending functionality into their .NET applications using the SMTP protocol. Its C# SMTP Client offers flexible message delivery options, support for multiple message formats (MSG, EML, etc.), and seamless proxy support, making it ideal for modern .NET applications.

  • Cross-platform compatibility: Works on Windows, Linux, and macOS.
  • Asynchronous sending: Ideal for web applications, preventing thread blocking.
  • Advanced authentication methods: Supports OAuth2 for secure mail relay.
  • Multiple Security Options: Explicit or implicit SSL/TLS, with automatic security negotiation.
  • Custom delivery settings: Offers network and directory-based email delivery.
  • Comprehensive email handling: Parses and manipulates incoming and outgoing messages.
  • Full email format support: Handles MSG, EML, MHT, PST, and more for email management.

C# SMTP Email Example to Automate Mail Relay

Aspose.Email automates several key processes involved in sending emails via SMTP by providing a straightforward API that hides the complex details of the protocol. Instead of manually writing extensive code to establish a connection, perform the server handshake, format the message, and handle authentication, the API allows developers to achieve this with just a few lines of code. The library takes care of the underlying protocol details - connection setup, message formatting, and transmission. Below is a simple step-by-step guide with C# SMTP email example demonstrating how the API automates the process of sending an email.

Install the .NET API

Before you start, spare a minute to install the library into your project. You can do this by any of the most convenient ways for you:

  • using NuGet Package Manager
  • referencing the required assemblies
  • or simply by using the following command within the Package Manager Console in Visual Studio:

Command


PM> Install-Package Aspose.Email

Create an Email Message

The MailMessage class simplifies the creation of the email message, including setting the subject, body, recipients, and attachments. Aspose.Email automatically formats the message according to SMTP requirements.

Create an Email Message

// Create a new email 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 API.";

Connect to SMTP Server

Connecting to an SMTP server is a crucial step in sending emails programmatically. With Aspose.Email, this process is streamlined and straightforward. The SmtpClient class automatically handles the connection to the specified SMTP server. This includes all low-level socket connections and the necessary handshakes between the client and server.

The API simplifies SMTP authentication. You only need to provide the username and password, and it will handle the appropriate authentication mechanisms required by your SMTP server.

Configuration Settings

To establish a connection with a server, you need to configure the following settings:

  • Host Address: The address of your SMTP server.
  • SMTP Port Number: The port number used by the server (commonly 587 for TLS/STARTTLS or 465 for SSL).
  • Credentials: Your SMTP username and password.
  • Encryption: Whether to use SSL/TLS encryption. The library provides options for these settings.

Here’s a code sample illustrating how to configure these settings and establish a connection:

SMTP Client C# Example

SmtpClient client = new SmtpClient("smtp.example.com", 587);
client.Username = "your_smtp_username";
client.Password = "your_smtp_password";
client.SecurityOptions = SecurityOptions.Auto; // Automatically determines the best security option (SSL/TLS)

In this example:

  • The SmtpClient is configured to connect to “smtp.example.com” using port 587.
  • It uses the provided Username and Password for authentication.
  • The SecurityOptions property is set to SecurityOptions.Auto , allowing Aspose.Email to automatically select the best security protocol (SSL/TLS) based on the server’s requirements.

Send Message

Once the email is created, the SmtpClient.Send method automates the entire process of submitting the message to the server, ensuring proper communication and error handling.

C# Send Email using SMTP

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

By setting SecurityOptions.SSLExplicit , Aspose.Email manages the secure communication between the client and server, using encryption protocols like SSL/TLS without requiring the developer to manually implement security measures.

Note: If you’re working on an ASP.NET application, consider the asynchronous send method to avoid blocking the main thread. You can use client.SendAsync for asynchronous sending.

SMTPClient C#: Advanced Settings

  • Connection methods: Our SmtpClient can connect to the SMTP server through a proxy. The proxy server settings can be specified during initialization.

  • Authentication methods: There are numerous methods working well with our SMTPClient. Plain and OAuth2 are just two most common examples.

  • Delivery methods: The supported delivery methods are network, specified pick-up directory and pick-up directory from IIS.

  • Security options: SSL/TLS explicit/implicit/auto.

Consider also methods and properties that make Aspose.Email SmtpClient highly configurable and reliable for various use cases: the GreetingTimeout property customizes SMTP handshake timing, the GetCapabilities method identifies supported server commands like STARTTLS, the ValidateCredentials method ensures valid authentication before email delivery, etc. For more information and best practices, please refer to our documentation .