4D Blog

Home Product Microsoft 365: Receive Emails with the Microsoft Graph API

Microsoft 365: Receive Emails with the Microsoft Graph API

January 15, 2023

Product

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.

4D v19 R3 brought the 4D Netkit component to manage the OAuth 2.0 connection and the mail sending through the Graph API.

In the v19 R8, we add new commands to receive emails and list your mailbox folders.

HDI Office365 Get Mails

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"))

mail Folders

You can get the mail folder collection directly under the root folder by using the .getFolderList() function:

$folderList:=$office365.mail.getFolderList()
// Collection with all the mail folder directly under the root folder
$folders:=$folderList.folders

You get a collection that contains, for each folder, helpful information such as the folder name, id, number of emails, and so :

If you have some folders with children folders, you need to call the .getFolderList() function using the folder id as a parameter to have the details of the folders:

$childrenList:=$office365.mail.getFolderList($folders[8].id)

Download emails

You can get the mails of your mailbox by using the .getMails() function. For example, if you want to retrieve all the mails present in the Inbox folder:

$param:=New object
$param.folderId:=$folder[1].id  // inbox folder

$mails:=$office365.mail.getMails($param)

When the selected folder is a folder created by default for users, instead of using the corresponding folder id value, for convenience, you can use the well-known folder names (archive,  drafts, inbox, junkemail,  sentitems, and so… ). So you can write the example below:

$param:=New object 
$param.folderId:="inbox"

$mails:=$office365.mail.getMails($param)

If you just need to download a specific email, you can use the .getMail() function and pass it the id of the email:

$mail:=$office.mail.getMail($mailId)

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

Discuss

Tags 4D NetKit, Mail, Microsoft 365, v19 R8, v20

Latest related posts

  • June 16, 2025

    Generate, share and use web sessions One-Time Passcodes (OTP)

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

Fabrice Mainguené
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.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy