Send an email
Sends an email via the Office 365 API. You can set headers, HTML or text body, recipients, and attach local files. Use BodyContentType set to Text for plain text instead of HTML.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
INSERT INTO MyMessages
(Subject, BodyContentType, BodyContent
, ToRecipients, CcRecipients, BccRecipients
, InternetMessageHeaders
, Attachments, Importance, IsDeliveryReceiptRequested, IsReadReceiptRequested
, SaveToSentItems)
VALUES
('Employee Reviews Scheduled', 'HTML', '<b>Hi All,</b> employee reviews have been scheduled. <span style="text-decoration: underline;">Please reflect this in your notes.</span>',
'[{ "emailAddress": { "address": "john.doe@domain.com" }}, { "emailAddress": { "address": "jane.doe@domain.com" }}]',
'[{ "emailAddress": { "address": "mary.dawson@domain.com" }}]',
'[{ "emailAddress": { "address": "ryan.connor@domain.com" }}]',
'[{ "name": "x-custom-header-group-name", "value": "Managers" }, { "name": "x-custom-header-group-id", "value":"MGR001" }]',
'[
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "file1.txt",
"contentType": "text/plain",
"contentBytes": "<<c:\file1.txt,FUN_FILE_BASE64ENC>>"
},
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "profile-picture.png",
"contentType": "image/png",
"contentBytes": "<<c:\profile-picture.png,FUN_FILE_BASE64ENC>>"
}
]',
'normal', 'false', 'false', 'true')
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY], 'INSERT INTO MyMessages
(Subject, BodyContentType, BodyContent
, ToRecipients, CcRecipients, BccRecipients
, InternetMessageHeaders
, Attachments, Importance, IsDeliveryReceiptRequested, IsReadReceiptRequested
, SaveToSentItems)
VALUES
(''Employee Reviews Scheduled'', ''HTML'', ''<b>Hi All,</b> employee reviews have been scheduled. <span style="text-decoration: underline;">Please reflect this in your notes.</span>'',
''[{ "emailAddress": { "address": "john.doe@domain.com" }}, { "emailAddress": { "address": "jane.doe@domain.com" }}]'',
''[{ "emailAddress": { "address": "mary.dawson@domain.com" }}]'',
''[{ "emailAddress": { "address": "ryan.connor@domain.com" }}]'',
''[{ "name": "x-custom-header-group-name", "value": "Managers" }, { "name": "x-custom-header-group-id", "value":"MGR001" }]'',
''[
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "file1.txt",
"contentType": "text/plain",
"contentBytes": "<<c:\file1.txt,FUN_FILE_BASE64ENC>>"
},
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "profile-picture.png",
"contentType": "image/png",
"contentBytes": "<<c:\profile-picture.png,FUN_FILE_BASE64ENC>>"
}
]'',
''normal'', ''false'', ''false'', ''true'')')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO MyMessages
(Subject, BodyContentType, BodyContent
, ToRecipients, CcRecipients, BccRecipients
, InternetMessageHeaders
, Attachments, Importance, IsDeliveryReceiptRequested, IsReadReceiptRequested
, SaveToSentItems)
VALUES
(''Employee Reviews Scheduled'', ''HTML'', ''<b>Hi All,</b> employee reviews have been scheduled. <span style="text-decoration: underline;">Please reflect this in your notes.</span>'',
''[{ "emailAddress": { "address": "john.doe@domain.com" }}, { "emailAddress": { "address": "jane.doe@domain.com" }}]'',
''[{ "emailAddress": { "address": "mary.dawson@domain.com" }}]'',
''[{ "emailAddress": { "address": "ryan.connor@domain.com" }}]'',
''[{ "name": "x-custom-header-group-name", "value": "Managers" }, { "name": "x-custom-header-group-id", "value":"MGR001" }]'',
''[
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "file1.txt",
"contentType": "text/plain",
"contentBytes": "<<c:\file1.txt,FUN_FILE_BASE64ENC>>"
},
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "profile-picture.png",
"contentType": "image/png",
"contentBytes": "<<c:\profile-picture.png,FUN_FILE_BASE64ENC>>"
}
]'',
''normal'', ''false'', ''false'', ''true'')'
EXEC (@MyQuery) AT [LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY]