Rebex Graph
.NET client library for MS Graph API (Exchange Online)
Download 30-day free trial Buy from $199More .NET libraries
-
Rebex Mail Pack
IMAP, MS Graph, EWS, POP3, SMTP, MIME, S/MIME, MSG
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Easy-to-use API
On this page:
A typical Graph session goes through the following steps:
- Connect to Exchange Online (Microsoft 365) server.
- Authenticate using an OAuth access token.
- Find e-mail messages, download e-mails, send e-mails, or more.
- Disconnect.
Getting list of unread e-mails
The following code searches for unread e-mail messages and shows information about them:
// create Graph client instance
using (var client = new Rebex.Net.GraphClient())
{
// connect to Exchange Online (Microsoft 365) server
client.Connect();
// authenticate with your OAuth access token
client.Login(token);
// get first page of a list of unread messages from the 'Inbox' folder
var unread = GraphMessageSearchParameter.IsRead(false);
GraphMessageCollection messages = client.Search(GraphFolderId.Inbox, unread);
// print some info
foreach (GraphMessageInfo info in messages)
{
Console.WriteLine("Subject: {0}", info.Subject);
Console.WriteLine("From: {0}", info.From);
Console.WriteLine("To: {0}", info.To);
}
// disconnect (not required, but polite)
client.Disconnect();
}
Note: This code returns the first page of the results. See Paging results for more information about paging.
Downloading e-mails
To download and parse a whole e-mail message into the memory-based MailMessage
object,
use GetMailMessage()
method:
// create Graph client instance
using (var client = new Rebex.Net.GraphClient())
{
// connect to Exchange Online (Microsoft 365) server
client.Connect();
// authenticate with your OAuth access token
client.Login(token);
// get first page of the message list of the 'Inbox' folder
GraphMessageCollection messages = client.GetMessageList(GraphFolderId.Inbox);
// print some info
Console.WriteLine("{0} message(s) found.", messages.Count);
// download those messages
foreach (GraphMessageInfo info in messages)
{
MailMessage mail = client.GetMailMessage(info.Id);
// do something with the mail
// ...
}
// disconnect (not required, but polite)
client.Disconnect();
}
Sending e-mails
Sending an e-mail message is as simple as creating a MailMessage
object and sending it using SendMessage
method:
// create Graph client instance
using (var client = new Rebex.Net.GraphClient())
{
// connect to Exchange Online (Microsoft 365) server
client.Connect();
// authenticate with your OAuth access token
client.Login(token);
// prepare e-mail
MailMessage mail = new MailMessage();
mail.To = "bob@example.org";
mail.Subject = "Sent using Rebex Graph";
mail.BodyText = "Hello World!";
// send e-mail
client.SendMessage(mail);
// disconnect (not required, but polite)
client.Disconnect();
}
Tip: The 'From' field does not have to be filled by the client - it's supplied by the server based on the session's account.
Back to feature list...