Rebex
Products Downloads Buy Support Contact
Show / Hide Table of Contents

GraphClient Class

Namespace: Rebex.Net
Assembly: Rebex.Graph.dll (version 8.0.9673)

Provides methods for communication with Microsoft 365 or Exchange Online mail servers using Microsoft Graph API.

See https://www.rebex.net/graph/features/ for code snippets of common use cases.

Syntax
public class GraphClient : NetworkSession, ILogWriterProvider, IDisposable, IAsyncDisposable
Inheritance
Object
NetworkSession
GraphClient
Implements
ILogWriterProvider
IDisposable
IAsyncDisposable
Inherited Members
NetworkSession.DefaultLogWriter
NetworkSession.GetHashCode()
NetworkSession.InstanceId
NetworkSession.LogWriter
NetworkSession.ServerName
NetworkSession.ServerPort
NetworkSession.UserName
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Examples

See https://www.rebex.net/graph/samples.aspx for ready-to-use samples.

Microsoft Graph API uses OAuth 2.0 authentication. Visit our blog to see how to configure and retrieve OAuth token for Delegated access (signed-in user mode) or Unattended access (app-only mode).

Connecting and authenticating

// initialize new Graph client instance
var client = new Rebex.Net.GraphClient();

// attach logger for diagnostic purposes
client.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\graph.log", Rebex.LogLevel.Debug);

// connect and authenticate
client.Connect();
client.Login(OAuthToken);

Listing and searching messages

// get top 25 messages from INBOX
var page = new GraphPageView(0, 25);
var list = client.GetMessageList(GraphFolderId.Inbox, GraphMessageFields.Envelope, page);

// get top 25 messages from Alice
var searchParam = GraphMessageSearchParameter.From(aliceEmail);
list = client.Search(GraphFolderId.Inbox, GraphMessageFields.Envelope, page, searchParam);

// print info about retrieved messages
foreach (var item in list)
{
    Console.WriteLine($"[{item.ReceivedDate}] {item.From}: {item.Subject}");
}

Getting message info

// get message info
var info = client.GetMessageInfo(itemId,
    GraphMessageFields.Info |
    GraphMessageFields.Body);

// print the info
Console.WriteLine(
    $"Graph ID: {info.Id}\n" +
    $"MIME ID: {info.MessageId}\n" +
    $"From: {info.From}\n" +
    $"To: {info.To}\n" +
    $"Attachments: {info.HasAttachments}\n" +
    $"Subject: {info.Subject}\n" +
    $"Body: {(info.BodyText ?? info.BodyPreview ?? info.BodyHtml)}\n");

Downloading messages

// download the message (its MIME content) and parse it into MailMessage class
var mail = client.GetMailMessage(itemId);

// print the info
Console.WriteLine(
    $"MIME ID: {mail.MessageId}\n" +
    $"From: {mail.From}\n" +
    $"To: {mail.To}\n" +
    $"Importance: {mail.Importance}\n" +
    $"Attachments: {mail.Attachments.Count}\n" +
    $"Subject: {mail.Subject}\n" +
    $"Body: {(mail.HasBodyText ? mail.BodyText : mail.BodyHtml)}\n");

// save the MIME message
mail.Save(@"c:\MyData\mail.eml");

// download the message directly without parsing it into memory
client.GetMessage(itemId, @"c:\MyData\mail.eml");

Constructors

Name Description
GraphClient()

Initializes Microsoft Graph client.

See https://www.rebex.net/graph/features/ for code snippets of common use cases.

Properties

Name Description
DefaultLogWriter

Gets or sets a default log writer used by all new instances of NetworkSession object. Inherited from NetworkSession.

InstanceId

Gets instance ID (intended for logging purposes). Inherited from NetworkSession.

IsAuthenticated

Gets a value indicating whether the GraphClient object is authenticated to a server.

IsBusy

Gets a value indicating whether there is any operation (method) in progress.

IsConnected

Gets a value indicating whether the GraphClient object is connected to a server.

LogWriter

Gets or sets the LogWriter used by this object. Inherited from NetworkSession.

ProtocolVersion

Gets or sets the Microsoft Graph API protocol version to be used for sending Graph queries.

Proxy

Gets or sets the network proxy to use to access a remote server.

ServerName

Gets the server name, if available. Inherited from NetworkSession.

ServerPort

Gets the server port, if available. Inherited from NetworkSession.

Settings

Gets or sets an GraphSettings object.

Timeout

Gets or sets the length of time in milliseconds before the operation times out.

TlsSocket

Returns the TLS/SSL socket of secured connection.

UserName

Gets the authenticated user name, if available. Inherited from NetworkSession.

Methods

Name Description
AddAttachment(GraphMessageId, Stream, GraphAddAttachmentOptions)

Adds an attachment to the specified message.

AddAttachment(GraphMessageId, String, GraphAddAttachmentOptions)

Adds an attachment to the specified message.

AddAttachmentAsync(GraphMessageId, Stream, GraphAddAttachmentOptions)

Adds an attachment to the specified message.

AddAttachmentAsync(GraphMessageId, String, GraphAddAttachmentOptions)

Adds an attachment to the specified message.

Connect()

Connects to the Microsoft 365 or Exchange Online server at https://graph.microsoft.com.

Connect(String, Int32)

Connects to a Graph server using specified port via HTTPS.

ConnectAsync()

Connects to the Microsoft 365 or Exchange Online server at https://graph.microsoft.com.

ConnectAsync(String, Int32)

Connects to a Graph server using specified port via HTTPS.

CreateFolder(GraphFolderId, String)

Creates a new subfolder within the specified folder.

CreateFolderAsync(GraphFolderId, String)

Creates a new subfolder within the specified folder.

DeleteFolder(GraphFolderId)

Deletes a Graph folder.

DeleteFolderAsync(GraphFolderId)

Deletes a Graph folder.

DeleteMessage(GraphMessageId)

Deletes a message with the specified ID. The message is soft-deleted (moved into the 'Deletions' subfolder of the 'Recoverable Items' folder).

DeleteMessage(GraphMessageId, Boolean)

Deletes a message with the specified ID.

DeleteMessageAsync(GraphMessageId)

Deletes a message with the specified ID. The message is soft-deleted (moved into the 'Deletions' subfolder of the 'Recoverable Items' folder).

DeleteMessageAsync(GraphMessageId, Boolean)

Deletes a message with the specified ID.

Disconnect()

Disconnects from the server.

DisconnectAsync()

Disconnects from the server.

Dispose()

Disposes this GraphClient object.

DisposeAsync()

Disposes this GraphClient object.

GetAttachment(GraphAttachmentId)

Gets an attachment identified by its ID.

GetAttachmentAsync(GraphAttachmentId)

Gets an attachment identified by its ID.

GetFolderInfo(GraphFolderId)

Gets information about specified Graph folder.

GetFolderInfoAsync(GraphFolderId)

Gets information about specified Graph folder.

GetFolderList(GraphFolderId, GraphPageView)

Gets a list of subfolders of the specified folder. Operation is shallow, which means that only first level of subfolders is returned.

GetFolderList(GraphPageView)

Gets a list of folders in the user's mailbox root. Operation is shallow, which means subfolders are not returned.

GetFolderListAsync(GraphFolderId, GraphPageView)

Gets a list of subfolders of the specified folder. Operation is shallow, which means that only first level of subfolders is returned.

GetFolderListAsync(GraphPageView)

Gets a list of folders in the user's mailbox root. Operation is shallow, which means subfolders are not returned.

GetHashCode()

Gets hash code for this object. Inherited from NetworkSession.

GetMailMessage(GraphMessageId)

Downloads a MailMessage with the specified ID.

GetMailMessageAsync(GraphMessageId)

Downloads a MailMessage with the specified ID.

GetMessage(GraphMessageId, Stream)

Downloads a message with the specified ID and writes its MIME data into the supplied stream.

GetMessage(GraphMessageId, String)

Downloads a message with the specified ID and writes its MIME data into a local file.

GetMessageAsync(GraphMessageId, Stream)

Downloads a message with the specified ID and writes its MIME data into the supplied stream.

GetMessageAsync(GraphMessageId, String)

Downloads a message with the specified ID and writes its MIME data into a local file.

GetMessageInfo(GraphMessageId, GraphMessageFields)

Gets information about a message with the specified ID.

GetMessageInfo(GraphMessageId, GraphMessageInfoOptions)

Gets information about a message with the specified ID.

GetMessageInfoAsync(GraphMessageId, GraphMessageFields)

Gets information about a message with the specified ID.

GetMessageInfoAsync(GraphMessageId, GraphMessageInfoOptions)

Gets information about a message with the specified ID.

GetMessageList(GraphFolderId, GraphMessageFields, GraphPageView)

Gets a list of messages in the specified folder.

GetMessageList(GraphFolderId, GraphPageView)

Gets a list of messages in the specified folder.

GetMessageListAsync(GraphFolderId, GraphMessageFields, GraphPageView)

Gets a list of messages in the specified folder.

GetMessageListAsync(GraphFolderId, GraphPageView)

Gets a list of messages in the specified folder.

GetMimeMessage(GraphMessageId)

Downloads a MimeMessage with the specified ID.

GetMimeMessageAsync(GraphMessageId)

Downloads a MimeMessage with the specified ID.

Login(String)

Authenticates the user to Microsoft 365 or Exchange Online server using an OAuth access token. For detailed instructions on registering your app and obtaining the token, visit https://blog.rebex.net/oauth2-office365-rebex-mail (delegated mode) or https://blog.rebex.net/office365-graph-oauth-unattended (unattended mode).

LoginAsync(String)

Authenticates the user to Microsoft 365 or Exchange Online server using an OAuth access token. For detailed instructions on registering your app and obtaining the token, visit https://blog.rebex.net/oauth2-office365-rebex-mail (delegated mode) or https://blog.rebex.net/office365-graph-oauth-unattended (unattended mode).

MoveMessage(GraphMessageId, GraphFolderId)

Moves an existing message into the specified folder.

MoveMessageAsync(GraphMessageId, GraphFolderId)

Moves an existing message into the specified folder.

Search(GraphFolderId, GraphMessageFields, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

Search(GraphFolderId, GraphMessageFields, GraphPageView, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

Search(GraphFolderId, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

Search(GraphFolderId, GraphMessageSearchQuery)

Searches a folder for messages that match the specified searching criteria.

Search(GraphFolderId, GraphPageView, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

SearchAsync(GraphFolderId, GraphMessageFields, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

SearchAsync(GraphFolderId, GraphMessageFields, GraphPageView, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

SearchAsync(GraphFolderId, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

SearchAsync(GraphFolderId, GraphMessageSearchQuery)

Searches a folder for messages that match the specified searching criteria.

SearchAsync(GraphFolderId, GraphPageView, GraphMessageSearchParameter[])

Searches a folder for messages that match the specified searching criteria.

SendMessage(MailMessage)

Sends a mail message (and saves it to the 'Sent Items' folder).

SendMessage(MimeMessage)

Sends a MIME message and saves it to the 'Sent Items' folder.

SendMessage(GraphMessageId)

Sends an existing draft message and saves it to the 'Sent Items' folder.

SendMessageAsync(MailMessage)

Sends a mail message (and saves it to the 'Sent Items' folder).

SendMessageAsync(MimeMessage)

Sends a MIME message and saves it to the 'Sent Items' folder.

SendMessageAsync(GraphMessageId)

Sends an existing draft message and saves it to the 'Sent Items' folder.

SetSocketFactory(ISocketFactory)

Sets the socket factory to be used to create communication sockets.

StoreMessage(GraphFolderId, MailMessage)

Stores a mail message into the specified folder. Only storing into Drafts is currently supported by Microsoft Graph API.

StoreMessage(GraphFolderId, MimeMessage)

Stores a MIME message into the specified folder. Only storing into Drafts is currently supported by Microsoft Graph API.

StoreMessageAsync(GraphFolderId, MailMessage)

Stores a mail message into the specified folder. Only storing into Drafts is currently supported by Microsoft Graph API.

StoreMessageAsync(GraphFolderId, MimeMessage)

Stores a MIME message into the specified folder. Only storing into Drafts is currently supported by Microsoft Graph API.

ToString()

Returns a string representation of this object.

UpdateMessage(GraphMessageId, GraphMessageData)

Updates an existing message.

UpdateMessageAsync(GraphMessageId, GraphMessageData)

Updates an existing message.

Events

Name Description
ValidatingCertificate

Occurs when a server certificate needs to be validated.

In This Article
© REBEX ČR s.r.o. Back to top
Privacy policy
Manage cookies