Microsoft 365: Copy, Move, Reply and Delete emails with the Microsoft Graph API

To access the data in Microsoft 365 like emails, Microsoft is deprecating Basic authentication for IMAP and POP3 and is increasingly promoting the Graph API.

From v19 R8, you now have new built-in commands in 4D NetKit to natively automate Microsoft 365 emails operations, such as copy, move, reply and delete emails.

HDI email management

Account connection

Before starting, we will create the OAuth2 connection object and the Office 365 object.

var $oAuth2: cs.NetKit.OAuth2Provider
var $office365 : cs.NetKit.Office365

$oAuth2:=cs.NetKit.OAuth2Provider.new($param)
$office365:=cs.NetKit.Office365.new($oAuth2;\
 New object("mailType"; "Microsoft"))

Move or copy emails

To move an email from one folder to another, you need to pass your mailId and folderId of the targeted folder to the .move() function:

$status:=$office365.mail.move($mailId; $folderId)

In the same way, to copy an email from one folder to another, you need to pass your mailId and folderId to the .copy() function:

$status:=$office365.mail.copy($mailId; $folderId)

Reply to an email

To reply to an email and create a conversation, you need to pass the text you want to send and the mailId of the original mail to the .reply() function:

$reply:=New object
// Text that will be sent as reply
$reply.comment:="Thank you for your message"
$status:=$office.mail.reply($reply; $mails.mailId)

Append an email

You can create a draft email and save it in the folder of your choice with the .append() function:

// Send the email
$status:=$office365.mail.append($draft; $folder.id)

Delete an email

To delete an email, you need to pass its id to the .delete() function:

$office365.mail.delete($mailId)

Check out this feature with the HDI and the documentation for more details!

To find more commands on email management, let’s read this blog post.

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.