4D Blog

Home Product Microsoft 365: Send Emails

Microsoft 365: Send Emails

July 18, 2022

Product

We know the importance of sending emails, and the fact that it’s a common task in today’s applications. Many people working with Office 365 have asked us for an API to send emails via the Microsoft Graph API. The OAuth 2.0 feature was a prerequisite for connecting to the Microsoft server and for introducing commands to retrieve user information. We are glad to proceed with email management, starting with the send email command.

HDI Office365 Send Mail

Account connection

Before creating the email, 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"))

PREPARE an email

You need first to create your email object. 4D supports now the message resource type of Microsoft Graph. The code examples in this post and the HDI are done with this new object. But of course, you can use a mail object with a JMAP format if you prefer.

Let’s begin by specifying the author of the email. The author must be the email address of the user connected, so we use $office365.user.getCurrent() to fill this attribute automatically:

$email:=New object
$email.from:=New object
$email.from.emailAddress:=New object
$email.from.emailAddress.address:=$office365.user.getCurrent().userPrincipalName

If you want to specify a reply-to address different from the address

Then the recipients (using different syntaxes):

$addressTo:=New object
$addressTo.emailAddress:=New object
$addressTo.emailAddress.email:="address1@mail.com"

$addressCC:=New object
$addressCC.emailAddress:=New object
$addressCC.emailAddress.email:="address2@mail.com"
$addressCC.emailAddress.name:="Stephen"

// Originating addresses
$email.toRecipients:=new collection($addressTo)
// Carbon Copy
$email.ccRecipients:=new collection($addressCC)

Next, let’s add a subject:

$email.subject:="Hello world"

And the body … You can specify two types, a text:

$email.body:=New object
$email.body.content:="Test content mail"
$email.body.contentType:="text"

or HTML:

$email.body:=New object
$email.body.content:="<html><body><h1>Test content mail </h1></body></html>"
$email.body.contentType:="html"

You can add additional information to your email like:

  • a request for a delivery receipt:
$email.isDeliveryReceiptRequested:=True
  • a request for a read receipt:
$email.isReadReceiptRequested:=True
  • a “low”, “normal” or “important” importance:
$email.importance:="high"

and others that you’ll find in the documentation!

Create an attachment

To add an attachment, you need to create an attachment object:

var $attachmentText : Text
$attachmentText:="Simple text attachement content"

var $attachment : Object
BASE64 ENCODE($attachmentText)
$attachment:=New object
$attachment["@odata.type"]:="#microsoft.graph.fileAttachment"
$attachment.contentId:=Generate UUID
$attachment.isInline:=False
$attachment.name:="attachment.txt"
$attachment.contentType:="text/plain"
$attachment.contentBytes:=$attachmentText
$attachment.size:=Length($attachmentText)

And add it to your email like this:

$email.attachments:=New collection($attachment)

Send email

Now that the email is ready, we can send it using the mail.send() function of the $office365 object we create before:

$status:=$office365.mail.send($email)

And so, your email is sent!

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

Discuss

Tags 4D NetKit, Mail, Microsoft 365, v19 R6, 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 Google Listbox Logs Mail 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 Google Listbox Logs Mail 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