Aspose.Email  for .NET

Send Emails via EWS in C#

Try Aspose.Email IEWSClient to send emails via Microsoft Exchange server in C# applications

  Download Free Trial
Sending emails is a fundamental aspect of modern communication, and integrating email functionality into your .NET applications can vastly improve productivity. With the Aspose.Email for .NET library and its IEWSClient, developers can seamlessly connect to a Microsoft Exchange Server and send messages effortlessly. Check the power and ease of using the .NET client for sending messages via the Exchange server.

Why Do I Send Emails with Aspose.Email for .NET IEWSClient?

  • Simple and straightforward integration process.

  • A wide range of capabilities such as adding attachments, specifying recipients and carbon copy (CC) recipients, setting subject and body, etc.

  • Support for various security protocols, including Transport Layer Security (TLS) and Secure Sockets Layer (SSL).

  • Fast email transmissions and a smooth user experience.

  • Compatibility with different versions of Microsoft Exchange Server, including Exchange Server 2007, 2010, 2013, 2016, and Office 365.

Getting Started with .NET API

  • Incorporate Aspose.Email for .NET to your C# project. It is easy to do by installing it via NuGet or downloading its DLL.
  • Start exploiting the simple and comprehensive functionality of the .NET API by initializing the IEWSClient.

A Synchronous Method to Send Emails

Let’s take a look at a simple code snippet that demonstrates how to use Aspose.Email for .NET IEWSClient to send a message via a Microsoft Exchange Server synchronously. It creates an instance of IEWSClient, configures a MailMessage object, and sends the email using the IEWSClient.

using Aspose.Email
using Aspose.Email.Clients.Exchange.WebService

// Create an instance of IEWSClient
IEWSClient client = EWSClient.GetEWSClient("https://exchangeserver/ews/exchange.asmx", "username", "password", "domain");

// Create and configure a MailMessage object
var eml = new MailMessage(fromAddress, toAddress, subject, body);
    
// Send the email using IEWSClient
client.Send(eml);

Using OAuth 2.0 with Aspose.Email IEWSClient

Modern authentication using OAuth 2.0 has become a standard method for securely accessing resources and APIs, including email services. Aspose.Email IEWSClient provides support for OAuth 2.0 authentication when connecting to a Microsoft Exchange server. This allows for secure and seamless integration of email functionality into your applications.

OAuth 2.0 authentication offers several advantages over traditional username-password authentication methods. It eliminates the need to store and transmit users’ passwords, reducing potential security risks. Instead, it relies on access tokens to authorize access to resources, providing an additional layer of security.

To use Aspose.Email IEWSClient with OAuth 2.0, follow these steps:

  1. Register your application with Azure AD (Active Directory) and obtain the client ID and client secret.
  2. Once the user grants consent, Azure AD will provide an authorization code. Exchange this authorization code with Azure AD to obtain an access token and a refresh token.
  3. Create an instance of IEWSClient and provide the necessary connection details, including the Exchange server URL and the access token obtained from Azure AD.
  4. Use the IEWSClient as usual to interact with the Exchange server. You can send emails, retrieve mailbox information, manage calendar events, and perform various other operations with ease.

By leveraging OAuth 2.0 authentication and the IEWSClient, you can enhance the security and convenience of your application’s integration with Microsoft Exchange servers. This modern authentication approach ensures that your application can securely send, receive, and manage emails, providing a seamless user experience while maintaining robust security practices.

An Asynchronous Method to Send Emails

One of the key functionalities offered by the Aspose.Email for .NET is the ability to send messages via the Exchange Server asynchronously.

The asynchronous method allows developers to send emails in a non-blocking manner, ensuring that the application’s performance and responsiveness are not affected. This is especially crucial when dealing with large volumes of emails or when sending emails in a multi-threaded environment. By leveraging the asynchronous method of the library, developers can enhance the efficiency and performance of their email sending functionality.

To send messages using the asynchronous method can be done with a few simple steps. You can try the following code sample to send a message asynchronously.

using Aspose.Email;
using Aspose.Email.Clients.Exchange.WebService;
   
//Create IAsyncEwsClientInstance
var ewsClient = await EWSClient.GetEwsClientAsync(mailboxUri, new OAuthNetworkCredential(tokenProvider),
    cancellationToken: cancellationToken);
   	
var eml = new MailMessage(fromAddress, toAddress, subject, body);
await ewsClient.SendAsync(eml, cancellationToken: cancellationToken);

Mailbox Items Handling

Apart from reading, creating and sending messages, IEWSClient allows to perform a wide range of tasks:

  • Message Filtering
  • Conversation Items Management
  • Activity Logging
  • Working with Calendar Items, Contacts, Distribution Lists, Tasks, etc.
  • Working with Appointments, Archives, and more.

About a powerful .NET API

Whether you are a beginner or an experienced developer, our multifunctional and feature-rich library is designed to cater to your needs. It offers a wide range of functionalities that can greatly assist in various aspects of software development. It simplifies the coding process by providing well-structured and intuitive classes, along with methods and properties that allow you to focus on the core logic of your code, rather than spending excessive time on repetitive tasks. With an extensive collection of classes, methods, and properties, you will find everything you require to enhance your coding experience. The collection is well-documented and provides clear guidelines, making it easier for developers of all proficiency levels to comprehend and utilize the library effectively.