Office365 Class
Properties Methods Events Configuration Settings Errors
The Office365 class provides an easy way to manage sending and receiving mail in Microsoft 365.
Syntax
class cloudmail.Office365
Remarks
This class provides an easy to use interface for Office365 using the Microsoft Graph API v1.0. To use the class, first set the authorization property to a valid OAuth token. The Office365 class can be used for sending or creating new messages; retrieving, moving, or copying existing messages; creating, deleting, or copying folders; and several other functionalities supported by the Microsoft Graph API.
This class requires authentication via OAuth 2.0. First, perform OAuth authentication using the OAuth class or a separate process. Once complete you should have an authorization string which looks like:
Bearer ya29.AHES6ZSZEJzATdZYjeihDn5W-VrXSsxEZu5p0pclxGdKKQAssign this value to the authorization property before attempting any operations. For Example:
Oauth oauth = new Oauth();
oauth.ClientId = "3c65828c-6376-4286-91b5-2381c3904a97";
oauth.ClientSecret = "fMI7Q~SmDkDGujXXQkKtaNE5hBQID0SIBmmwP";
oauth.ServerAuthURL = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
oauth.ServerTokenURL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
oauth.AuthorizationScope = "user.read mail.readwrite mail.send mailboxsettings.readwrite";
oauth.GrantType = OauthGrantTypes.ogtAuthorizationCode;
office365.Authorization = oauth.GetAuthorization();
Consult the documentation for the service for more information about supported scope values and more details on OAuth authentication.
Sending Messages
There are two methods for sending new messages using the Office365 component. The send_mail method will send a message directly. Alternatively, you can create a message draft and then send an existing draft using the send_draft method. In both cases the properties of the new message are assigned through the Message properties (message_subject, message_body_content, message_cc, etc.).
Sending a Message with SendDraft:
office365.MessageSubject = "Subject Text";
office365.MessageImportance = "Low";
office365.MessageBodyContentType = "TEXT";
office365.MessageBodyContent = "Body Text.";
office365.MessageTo = "email@example.com";
office365.CreateDraft(0, "");
string messageId = office365.MessageInfo[0].Id;
office365.SendDraft(messageId);
There are also methods for replying or forwarding messages using the Office365 component. The reply, reply_all, and forward method will send a reply or forward directly. Similarly, you can create a reply or forward draft and then send an existing draft using the send_draft method. Unlike creating a new message, only the direct methods use the Message properties (message_subject, message_body_content, message_cc, etc.). When using create_draft, the draft must first be made then updated using the MessageInfo* properties and update method.
Sending a Reply with SendDraft:
//Create the reply draft
string originalMessageId = "Message ID";
office365.CreateDraft(1, oringialMessageId);
//Set the new draft MessageInfo fields with desired options
office365.MessageInfo[0].To = "email@example.com";
office365.MessageInfo[0].Subject = "Subject Text";
office365.MessageInfo[0].BodyContentType = "TEXT";
office365.MessageInfo[0].BodyContent = "Body Text";
//Update the draft
office365.Update(office365.MessageInfo[0].Id);
//Send the draft
office365.SendDraft(office365.MessageInfo[0].Id);
Receiving Messages
Information about messages fetched by the component can be accessed through the MessageInfo* properties. MessageInfo* properties is populated when the list_messages, fetch_message, search, or list_changes methods are called.
The list_messages and list_changes methods will respectively list the messages or changed messages in a folder specified by a folderId. To get the ID of a folder, folders can be traversed and read using the list_folders method and the Folder* properties.
Listing Messages in a Folder:
// Get the folder ID
string folderId = "";
office365.ListFolders(""); // Lists the root child folders.
for (int i = 0; i < office365.Folders.Count; i++)
{
if (office365.Folders[i].DisplayName.Equals("SpecificFolderName"))
{
folderId = office365.Folders[i].Id;
break;
}
}
// List folder messages
office365.ListMessages(folderId, "");
By default, the component will fetch one page of 100 messages when list_messages is called.
If additional messages remain in the folder, the list_messages_marker property will be populated.
If list_messages is then called again on the same folder the next page of messages will be fetched.
The example below populates MessageInfo* properties with all the messages in a particular folder.
do {
office365.ListMessages(folderId);
} while (office365.ListMessagesMarker.Length > 0);
The message page size may also be changed by using the MessagePageSize configuration setting.
Property List
The following is the full list of the properties of the class with short descriptions. Click on the links for further details.
attachment_count | The number of records in the Attachment arrays. |
attachment_attachment_type | This property contains the attachment type of the attachment. |
attachment_content_id | This property contains the value of the unique content identifier of the attachment. |
attachment_content_location | This property contains the content location of the attachment. |
attachment_content_type | This property contains the content type of the attachment. |
attachment_data | This property contains the raw data of the attachment. |
attachment_file | This property contains the local file name associated with the attachment. |
attachment_id | This property contains the attachment identifier of the attachment. |
attachment_is_inline | This property is true if the attachment is an inline attachment. |
attachment_last_modified_date | This property contains the date and time when the attachment was last modified. |
attachment_name | This property contains the name of the attachment. |
attachment_size | This property contains the size in bytes of the attachment. |
authorization | An OAuth Authorization String. |
category_count | The number of records in the Category arrays. |
category_color | This property contains the color of the category. |
category_display_name | This property contains the display name of the category. |
category_id | This property contains the unique identifier of the category. |
change_marker | The page marker for listing changed messages. |
firewall_auto_detect | This property tells the class whether or not to automatically detect and use firewall system settings, if available. |
firewall_type | This property determines the type of firewall to connect through. |
firewall_host | This property contains the name or IP address of firewall (optional). |
firewall_password | This property contains a password if authentication is to be used when connecting through the firewall. |
firewall_port | This property contains the TCP port for the firewall Host . |
firewall_user | This property contains a user name if authentication is to be used connecting through a firewall. |
folder_count | The number of records in the Folder arrays. |
folder_child_folder_count | The number of child folders the folder has. |
folder_child_folders | The child folders of the folder. |
folder_display_name | The display name of the folder. |
folder_id | The unique identifier of the folder. |
folder_message_rules | The message rules of the folder. |
folder_messages | The messages contained in the folder. |
folder_multi_value_extended_properties | The multi-value extended properties defined for the folder. |
folder_parent_folder_id | The unique identifier for the folder's parent. |
folder_single_value_extended_properties | The single-value extended properties defined for the folder. |
folder_total_item_count | The number of total items in the folder. |
folder_unread_item_count | The number of unread items in the folder. |
list_folders_marker | The page marker for listing folders. |
list_messages_marker | The page marker for listing folders. |
message | Provides the raw message content. |
message_attachment_count | The number of records in the MessageAttachment arrays. |
message_attachment_attachment_type | This property contains the attachment type of the attachment. |
message_attachment_content_id | This property contains the value of the unique content identifier of the attachment. |
message_attachment_content_location | This property contains the content location of the attachment. |
message_attachment_content_type | This property contains the content type of the attachment. |
message_attachment_data | This property contains the raw data of the attachment. |
message_attachment_file | This property contains the local file name associated with the attachment. |
message_attachment_id | This property contains the attachment identifier of the attachment. |
message_attachment_is_inline | This property is true if the attachment is an inline attachment. |
message_attachment_last_modified_date | This property contains the date and time when the attachment was last modified. |
message_attachment_name | This property contains the name of the attachment. |
message_attachment_size | This property contains the size in bytes of the attachment. |
message_bcc | A comma separated list of recipients for blind carbon copies for a message. |
message_body_content | The body content for a message. |
message_body_content_type | The body content type for a message. |
message_cc | A comma separated list of recipients for carbon copies for a message. |
message_delivery_receipt | Whether or not a message will request a Delivery Receipt. |
message_from | The author of a message. |
message_importance | The importance of a message. |
message_info_count | The number of records in the MessageInfo arrays. |
message_info_bcc | The BCc recipients of a message in a message info listing. |
message_info_body_content | The body content of a message in a message info listing. |
message_info_body_content_type | The body content type (e. |
message_info_body_preview | The body preview of a message in a message info listing. |
message_info_categories | The categories of a message in a message info listing. |
message_info_cc | The Cc recipients of a message in a message info listing. |
message_info_conversation_id | The conversation unique identifier of a message in a message info listing. |
message_info_conversation_index | The conversation index of a message in a message info listing. |
message_info_created_date | The date created of a message in a message info listing. |
message_info_delivery_receipt_requested | Whether or not a delivery receipt was requested for a message in a message info listing. |
message_info_flag_status | Message flag in a message info listing. |
message_info_from | The sender of a message in a message info listing. |
message_info_has_attachments | Whether or not a message in a message info listing has attachments. |
message_info_id | The unique identifier of a message in a message info listing set by Microsoft. |
message_info_importance | The importance of a message in a message info listing. |
message_info_inference_classification | The inference classification of a message in a message info listing. |
message_info_is_draft | Whether or not a message in a message info listing is a draft. |
message_info_is_read | Whether or not a message in a message info listing has been read. |
message_info_json | The full JSON content of a message in a message info listing. |
message_info_last_modified_date | The last modified date of a message in a message info listing. |
message_info_message_headers | The message headers of a message in a message info listing. |
message_info_message_id | The internet message id for the message as described by rfc2822. |
message_info_parent_folder_id | The unique identifier of the parent folder of a message in a message info listing. |
message_info_read_receipt_requested | Whether or not a read receipt was requested for a message in a message info listing. |
message_info_received_date | The received date of a message in a message info listing. |
message_info_reply_to | Where to send replies for a message in a message info listing. |
message_info_sender | The sender of a message in a message info listing. |
message_info_sent_date | The date a message was sent for a message in a message info listing. |
message_info_subject | The subject of a message in a message info listing. |
message_info_to | The recipients of a message in a message info listing. |
message_info_web_link | The URL to open a message in a message info listing in Outlook on the web. |
message_other_headers | The additional message headers for a message. |
message_read_receipt | Whether or not a message will request a Read Receipt. |
message_reply_to | A mail address to reply to. |
message_subject | The subject of a message. |
message_to | A comma separated list of recipients for a message. |
next_change_marker | A marker indicating which page of changes to return in the future. |
proxy_auth_scheme | This property is used to tell the class which type of authorization to perform when connecting to the proxy. |
proxy_auto_detect | This property tells the class whether or not to automatically detect and use proxy system settings, if available. |
proxy_password | This property contains a password if authentication is to be used for the proxy. |
proxy_port | This property contains the TCP port for the proxy Server (default 80). |
proxy_server | If a proxy Server is given, then the HTTP request is sent to the proxy instead of the server otherwise specified. |
proxy_ssl | This property determines when to use SSL for the connection to the proxy. |
proxy_user | This property contains a user name, if authentication is to be used for the proxy. |
select | The parts of a message that should be retrieved. |
Method List
The following is the full list of the methods of the class with short descriptions. Click on the links for further details.
add_attachment | Adds an attachment to an existing message. |
config | Sets or retrieves a configuration setting. |
copy | Creates a copy of a message. |
copy_folder | Copies a folder. |
create_category | Creates a new category. |
create_draft | Creates a new email draft. |
create_folder | Creates a new folder. |
delete | Deletes a message. |
delete_attachment | Deletes an attachment. |
delete_category | Deletes a mail category. |
delete_folder | Deletes a folder. |
fetch_attachment | Retrieves a message attachment. |
fetch_message | Retrieves a message. |
fetch_message_raw | Retrieves the raw message of the specified message ID. |
forward | Forward a message. |
get_category | Retrieves a mail category. |
get_folder | Retrieves a folder. |
list_attachments | Lists all of a message's attachments. |
list_categories | Lists all mail categories. |
list_changes | Lists messages that have been changed within a specified folder. |
list_folders | Lists the folders found in the parent folder. |
list_messages | Lists the messages in a folder. |
move_folder | Moves a folder. |
move_message | Moves a message. |
rename_folder | Renames a folder. |
reply | Reply to a message. |
reply_all | ReplyAll to a message. |
reset | Reset the class. |
search | Search for messages. |
send_custom_request | Send a custom HTTP request. |
send_draft | Sends an existing draft. |
send_mail | Sends a new email. |
update | Updates a message. |
update_category | Updates a category. |
Event List
The following is the full list of the events fired by the class with short descriptions. Click on the links for further details.
on_attachment_list | Fired when an attachment is retrieved from the server. |
on_category_list | Fired when an attachment is retrieved from the server. |
on_error | Information about errors during data delivery. |
on_folder_list | Fired when a folder is retrieved by the server. |
on_log | Fires once for each log message. |
on_message_list | Fired when a message is retrieved from the server. |
on_transfer | Fired while a document transfers (delivers document). |
Configuration Settings
The following is a list of configuration settings for the class with short descriptions. Click on the links for further details.
AttachmentFragmentSize | Size of fragments when uploading large attachments. |
AttachmentSimpleUploadLimit | The threshold to use simple uploads. |
FetchMIMEMessage | Fetch MIME encoded message. |
FolderPageSize | Page size for fetching folders. |
MessagePageSize | Page size for fetching messages. |
MIMEMessage | MIME encoded message to send. |
Prefer | Specifies a preferred content header type. |
QueryParamCount | The number of custom OData Query Parameters. |
QueryParamName[i] | The name of a custom OData Query Parameter. |
QueryParamValue[i] | The value of a custom OData Query Parameter. |
UserId | Sets the Id of a shared mailbox to connect to. |
XPath | Provides a way to point to a specific element in the returned XML or JSON response. |
XText | The text of the current element. |
AcceptEncoding | Used to tell the server which types of content encodings the client supports. |
AllowHTTPCompression | This property enables HTTP compression for receiving data. |
AllowHTTPFallback | Whether HTTP/2 connections are permitted to fallback to HTTP/1.1. |
Append | Whether to append data to LocalFile. |
Authorization | The Authorization string to be sent to the server. |
BytesTransferred | Contains the number of bytes transferred in the response data. |
ChunkSize | Specifies the chunk size in bytes when using chunked encoding. |
CompressHTTPRequest | Set to true to compress the body of a PUT or POST request. |
EncodeURL | If set to true the URL will be encoded by the class. |
FollowRedirects | Determines what happens when the server issues a redirect. |
GetOn302Redirect | If set to true the class will perform a GET on the new location. |
HTTP2HeadersWithoutIndexing | HTTP2 headers that should not update the dynamic header table with incremental indexing. |
HTTPVersion | The version of HTTP used by the class. |
IfModifiedSince | A date determining the maximum age of the desired document. |
KeepAlive | Determines whether the HTTP connection is closed after completion of the request. |
KerberosSPN | The Service Principal Name for the Kerberos Domain Controller. |
LogLevel | The level of detail that is logged. |
MaxRedirectAttempts | Limits the number of redirects that are followed in a request. |
NegotiatedHTTPVersion | The negotiated HTTP version. |
OtherHeaders | Other headers as determined by the user (optional). |
ProxyAuthorization | The authorization string to be sent to the proxy server. |
ProxyAuthScheme | The authorization scheme to be used for the proxy. |
ProxyPassword | A password if authentication is to be used for the proxy. |
ProxyPort | Port for the proxy server (default 80). |
ProxyServer | Name or IP address of a proxy server (optional). |
ProxyUser | A user name if authentication is to be used for the proxy. |
SentHeaders | The full set of headers as sent by the client. |
StatusLine | The first line of the last response from the server. |
TransferredData | The contents of the last response from the server. |
TransferredDataLimit | The maximum number of incoming bytes to be stored by the class. |
TransferredHeaders | The full set of headers as received from the server. |
TransferredRequest | The full request as sent by the client. |
UseChunkedEncoding | Enables or Disables HTTP chunked encoding for transfers. |
UseIDNs | Whether to encode hostnames to internationalized domain names. |
UsePlatformHTTPClient | Whether or not to use the platform HTTP client. |
UserAgent | Information about the user agent (browser). |
ConnectionTimeout | Sets a separate timeout value for establishing a connection. |
FirewallAutoDetect | Tells the class whether or not to automatically detect and use firewall system settings, if available. |
FirewallHost | Name or IP address of firewall (optional). |
FirewallPassword | Password to be used if authentication is to be used when connecting through the firewall. |
FirewallPort | The TCP port for the FirewallHost;. |
FirewallType | Determines the type of firewall to connect through. |
FirewallUser | A user name if authentication is to be used connecting through a firewall. |
KeepAliveInterval | The retry interval, in milliseconds, to be used when a TCP keep-alive packet is sent and no response is received. |
KeepAliveTime | The inactivity time in milliseconds before a TCP keep-alive packet is sent. |
Linger | When set to True, connections are terminated gracefully. |
LingerTime | Time in seconds to have the connection linger. |
LocalHost | The name of the local host through which connections are initiated or accepted. |
LocalPort | The port in the local host where the class binds. |
MaxLineLength | The maximum amount of data to accumulate when no EOL is found. |
MaxTransferRate | The transfer rate limit in bytes per second. |
ProxyExceptionsList | A semicolon separated list of hosts and IPs to bypass when using a proxy. |
TCPKeepAlive | Determines whether or not the keep alive socket option is enabled. |
TcpNoDelay | Whether or not to delay when sending packets. |
UseIPv6 | Whether to use IPv6. |
LogSSLPackets | Controls whether SSL packets are logged when using the internal security API. |
OpenSSLCADir | The path to a directory containing CA certificates. |
OpenSSLCAFile | Name of the file containing the list of CA's trusted by your application. |
OpenSSLCipherList | A string that controls the ciphers to be used by SSL. |
OpenSSLPrngSeedData | The data to seed the pseudo random number generator (PRNG). |
ReuseSSLSession | Determines if the SSL session is reused. |
SSLCACertFilePaths | The paths to CA certificate files on Unix/Linux. |
SSLCACerts | A newline separated list of CA certificate to use during SSL client authentication. |
SSLCheckCRL | Whether to check the Certificate Revocation List for the server certificate. |
SSLCipherStrength | The minimum cipher strength used for bulk encryption. |
SSLEnabledCipherSuites | The cipher suite to be used in an SSL negotiation. |
SSLEnabledProtocols | Used to enable/disable the supported security protocols. |
SSLEnableRenegotiation | Whether the renegotiation_info SSL extension is supported. |
SSLIncludeCertChain | Whether the entire certificate chain is included in the SSLServerAuthentication event. |
SSLNegotiatedCipher | Returns the negotiated ciphersuite. |
SSLNegotiatedCipherStrength | Returns the negotiated ciphersuite strength. |
SSLNegotiatedCipherSuite | Returns the negotiated ciphersuite. |
SSLNegotiatedKeyExchange | Returns the negotiated key exchange algorithm. |
SSLNegotiatedKeyExchangeStrength | Returns the negotiated key exchange algorithm strength. |
SSLNegotiatedVersion | Returns the negotiated protocol version. |
SSLProvider | The name of the security provider to use. |
SSLSecurityFlags | Flags that control certificate verification. |
SSLServerCACerts | A newline separated list of CA certificate to use during SSL server certificate validation. |
TLS12SignatureAlgorithms | Defines the allowed TLS 1.2 signature algorithms when UseInternalSecurityAPI is True. |
TLS12SupportedGroups | The supported groups for ECC. |
TLS13KeyShareGroups | The groups for which to pregenerate key shares. |
TLS13SignatureAlgorithms | The allowed certificate signature algorithms. |
TLS13SupportedGroups | The supported groups for (EC)DHE key exchange. |
AbsoluteTimeout | Determines whether timeouts are inactivity timeouts or absolute timeouts. |
FirewallData | Used to send extra data to the firewall. |
InBufferSize | The size in bytes of the incoming queue of the socket. |
OutBufferSize | The size in bytes of the outgoing queue of the socket. |