Aspose.Email  for .NET

Send email via SMTP in C#

A step-by-step guide to send email via SMTP. Configure SMTPClient using a progressive .NET library.

  Download Free Trial

SMTP is a simple and efficient protocol intended to provide seamless transmission of email messages.

SMTP stands for Simple Mail Transfer Protocol. It is broadly used to send and relay mail between servers. It is an indispensable part of the email communication process, enabling the transfer of outbound messages from the sender’s email client or application to the recipient’s email server.

The process involves the correct configuring of SMTP for smooth mail delivery. Aspose.Email for .NET 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 email messages and provides functionalities to send, receive, and manipulate them. .NET developers can leverage the library capabilities to reach a successful mail relay.

Send email with SMTP using C# and Aspose.Email API

Here’s a simple step-by-step guide to send an email using the API with SMTP protocol:

Install Aspose.Email Library

Before you start, you need to install the library in your C# project. You can do this by using NuGet Package Manager or by manually referencing the required assemblies.

Create a message

To send a message, you first need to create an instance of the MailMessage class from Aspose.Email. This class represents an email message and contains all the necessary information like sender, recipients, subject, body, attachments, etc.

// 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.";

Configure SMTP Settings

To send the message, you need to configure the settings for the mail server you want to use. These settings include the host address, SMTP port number, credentials (if required), and whether to use SSL/TLS encryption.

SmtpClient client = new SmtpClient("smtp.example.com", 587);
client.Username = "your_smtp_username";
client.Password = "your_smtp_password";
client.SecurityOptions = SecurityOptions.Auto;

Consider other settings allowed by our SMTPClient

  • 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.

Send the Message

Once you have created the email message and configured the SMTP settings, you can use the SmtpClient.Send method to send it.

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

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.

Why do people choose Aspose.Email for .NET for sending email?

Here are some of the key reasons why Aspose.Email is a popular choice:

  • Ease of Use: It provides a clear-cut and user-intuitive API for easy processing of email messages in .NET applications.

  • Feature-Rich: The library offers a comprehensive set of features for creating, sending, receiving, and managing email messages. It supports multiple formats, including HTML and plain text, and allows you to add attachments, set message priorities, work with embedded images, and more.

  • SMTP Support: It also provides complete and comprehensive functionality for the developers to send emails using SMTP integrating seamlessly into their applications. It supports both synchronous and asynchronous sending, which is especially important for web applications to prevent blocking the main thread.

  • Parsing and Manipulation: With its functionality, you can parse incoming emails, extract data from messages, and perform various manipulations on the content. This is particularly useful for applications that need to process incoming messages automatically.

  • Support for Popular Formats: The library supports a variety of email formats, such as EML, MSG, MHT, and PST.

  • Security Features: It includes various security features, such as S/MIME (Secure/Multipurpose Internet Mail Extensions) support which enables users to encrypt and sign emails, add digital signatures, and ensure secure communication.

  • Cross-Platform Compatibility: It functions smoothly across various platforms: Windows, Linux, and macOS are the most common. This allows developers to create applications that run on various operating systems.

  • Continuous Updates and Support: The developers behind it are known for providing regular updates and excellent technical support, ensuring that developers can rely on a stable and well-maintained library.

  • Documentation and Community: The library comes with comprehensive documentation and code examples, making it easier for developers to get started and find solutions to their queries. Additionally, there is an active community around our products, allowing developers to seek help and share knowledge.