Download a message as .EML file
Downloads a single message as an .EML file (RFC 822 / MIME). Supply Message Id, User Id, and MailFolder Id; the message is saved to the path given in TargetFilePath. The file can be opened in most email clients and includes attachments.
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.
SELECT * FROM
download_message
WITH(
MessageId='AAMkADliN2RmZGM4..........3JFHGAAf2n2J-AAA=',
SaveFolder='c:\temp',
OverwriteFile='True',
--UserId='me', --or use "user-id" or use "email"
--UserId='someuser@mycompany.onmicrosoft.com',
--UserId='1487fe8b-f09f-4015-a817-b8b9fe2a3edc',
MailFolderId='INBOX' --or use mailbox ID
)
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY], 'SELECT * FROM
download_message
WITH(
MessageId=''AAMkADliN2RmZGM4..........3JFHGAAf2n2J-AAA='',
SaveFolder=''c:\temp'',
OverwriteFile=''True'',
--UserId=''me'', --or use "user-id" or use "email"
--UserId=''someuser@mycompany.onmicrosoft.com'',
--UserId=''1487fe8b-f09f-4015-a817-b8b9fe2a3edc'',
MailFolderId=''INBOX'' --or use mailbox ID
)')
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) = 'SELECT * FROM
download_message
WITH(
MessageId=''AAMkADliN2RmZGM4..........3JFHGAAf2n2J-AAA='',
SaveFolder=''c:\temp'',
OverwriteFile=''True'',
--UserId=''me'', --or use "user-id" or use "email"
--UserId=''someuser@mycompany.onmicrosoft.com'',
--UserId=''1487fe8b-f09f-4015-a817-b8b9fe2a3edc'',
MailFolderId=''INBOX'' --or use mailbox ID
)'
EXEC (@MyQuery) AT [LS_TO_OUTLOOK_MAIL_OFFICE_365_IN_GATEWAY]