Send Email via Microsoft EWS in C#
Implement Microsoft Exchange messaging in your C# solution using a high-level client API
Download Free TrialSend Email Using EWS in C#
Aspose.Email for .NET is a powerful email processing library designed for developers building C# applications that send, receive, and manage messages. It offers support for Microsoft Exchange through the IEWSClient interface, allowing you to send emails via Exchange Web Services (EWS). This is an ideal solution for enterprise-level tasks. The API supports modern authentication with OAuth 2.0 and asynchronous message sending. It also integrates perfectly with Exchange Server or hybrid Microsoft 365 environments.
What is EWS and Why Use It?
Exchange Web Services (EWS) is a Microsoft SOAP-based API used to interact with Exchange Server mailboxes programmatically.
It supports:
Sending and receiving messages
Managing calendars, contacts, and tasks
Integrating with Exchange Server (2007–2016) and Microsoft 365 hybrid setups
Although Microsoft now recommends Microsoft Graph for modern cloud-based solutions, EWS remains supported for legacy and on-prem environments.
EWS vs Microsoft Graph: Which Should You Use?
Choosing between EWS and Microsoft Graph depends on your infrastructure setup and the version of Exchange you target.
Recommendation:
- Use EWS for legacy or on-premises Exchange environments.
- Use Microsoft Graph for new cloud-based applications targeting Microsoft 365.
Learn how to send emails using Microsoft Graph.
Basic Email Sending via EWS in C#
The following code sample demonstrates how to send an email using Exchange Web Services (EWS) through the IEWSClient interface in Aspose.Email for .NET.
- First, the code connects to an Exchange Server by initializing an IEWSClient instance using the EWS server endpoint and user credentials.
- Then, it creates a simple MailMessage object containing the sender, recipient, subject, and body.
- Finally, the message is sent using the Send method.
C# code sample to send email via exchange server
using Aspose.Email
using Aspose.Email.Clients.Exchange.WebService
// Connect to Exchange server using EWS
IEWSClient client = EWSClient.GetEWSClient("https://exchangeserver/ews/exchange.asmx", "username", "password", "domain");
// Create a new MailMessage
var eml = new MailMessage(fromAddress, toAddress, subject, body);
// Send the message
client.Send(eml);
Modern Authentication with OAuth 2.0
OAuth 2.0 is a secure, token-based authentication method that eliminates the need for storing or transmitting user credentials when accessing resources and APIs, including email services. Instead, it relies on access tokens to authorize access, adding an extra layer of protection. The IEWSClient supports OAuth 2.0 authentication for connecting to Microsoft Exchange servers.
To integrate IEWSClient with OAuth 2.0, follow these steps:
- Register your application with Azure AD (Active Directory) to obtain the client ID and client secret.
- Once the user grants consent, Azure AD will provide an authorization code. Use this code to request an access token and a refresh token.
- Create an instance of IEWSClient, providing the necessary connection details, including the Exchange server URL and the access token obtained from Azure AD.
- Use the IEWSClient to interact with the Exchange server. You can send emails, retrieve mailbox information, manage calendar events, and perform various other tasks with ease.
For more information and code samples, please refer to our documentation.
An Asynchronous Method to Send Emails
Aspose.Email IAsyncEwsClient interface allows for sending messages via Microsoft Exchange Server using asynchronous operations - fully described in Asynchronous Operations with OAuth Authentication & EWS Client.
Message sending, in this case, doesn’t block the main application thread, which helps maintain optimal performance and responsiveness. It is particularly important when managing large volumes of data or operating in a multi-threaded environment.
Use the code sample below to send a message without interruption.
Asynchronous sending - C# example
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);
Why use IEWSClient?
With IEWSClient, you can also:
Filter and search messages
Manage conversations and threads
Work with calendar items, tasks, contacts, and distribution lists
Use secure protocols (TLS/SSL)
Support Exchange versions from 2007 to Office 365
About the API
Aspose.Email for .NET is a powerful, feature-rich API that enables developers to create, send, receive, and manage email messages and related data. Its intuitive design, extensive documentation, and support for multiple email protocols make it ideal for building secure and scalable solutions in .NET applications.
Explore the full API overview.