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.
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!