Microsoft 365: Send Emails

Automatically translated from English

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

 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

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

$addressCC:=New object
$addressCC.emailAddress:=New object

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

or HTML:

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

You can add additional information to your email like:

  • a request for a delivery receipt:
  • a request for a read receipt:
  • a “low”, “normal” or “important” importance:

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.contentId:=Generate UUID

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:


And so, your email is sent!

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

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.