DataFlex
DataFlex
Office365 Send Email from Shared Mailbox
See more Office365 Examples
Demonstrates how to send email from a shared mailbox, from smtp.office365.com using OAuth2 authentication.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoJsonToken
Handle hoMailman
Variant vEmail
Handle hoEmail
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// An Office365 OAuth2 access token must first be obtained prior
// to running this code.
// Getting the OAuth2 access token for the 1st time requires the O365 account owner's
// interactive authorizaition via a web browser. Afterwards, the access token
// can be repeatedly refreshed automatically.
// See the following examples for getting and refreshing an OAuth2 access token
// Get Office365 SMTP/IMAP/POP3 OAuth2 Access Token
// Refresh Office365 SMTP/IMAP/POP3 OAuth2 Access Token
// First get our previously obtained OAuth2 access token.
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
If (Not(IsComObjectCreated(hoJsonToken))) Begin
Send CreateComObject of hoJsonToken
End
Get ComLoadFile Of hoJsonToken "qa_data/tokens/office365.json" To iSuccess
Get Create (RefClass(cComChilkatMailMan)) To hoMailman
If (Not(IsComObjectCreated(hoMailman))) Begin
Send CreateComObject of hoMailman
End
Set ComSmtpHost Of hoMailman To "smtp.office365.com"
Set ComSmtpPort Of hoMailman To 587
Set ComStartTLS Of hoMailman To True
// In case of shared mailbox access using OAuth, the application needs to obtain the access token on behalf of a user
// but replace the SMTP username with the email address of the shared mailbox.
// Login using the shared mailbox email address for the username, and the access token for the password.
// See Office 365 SASL XOAUTH2 format
// Use your Office365 email address for the SmtpUsername.
Set ComSmtpUsername Of hoMailman To "SHARED_MAILBOX_EMAIL_ADDRESS"
Get ComStringOf Of hoJsonToken "access_token" To sTemp1
Set ComOAuth2AccessToken Of hoMailman To sTemp1
// Create a new email object
Get Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Set ComSubject Of hoEmail To "This is a test"
Set ComBody Of hoEmail To "This is a test"
Set ComFrom Of hoEmail To "MY_NAME <SHARED_MAILBOX_EMAIL_ADDRESS>"
Get ComAddTo Of hoEmail "John Doe" "somebody@example.com" To iSuccess
// Call SendEmail to connect to the SMTP server and send.
// The connection (i.e. session) to the SMTP server remains
// open so that subsequent SendEmail calls may use the
// same connection.
Get pvComObject of hoEmail to vEmail
Get ComSendEmail Of hoMailman vEmail To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoMailman To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComCloseSmtpConnection Of hoMailman To iSuccess
If (iSuccess <> True) Begin
Showln "Connection to SMTP server not closed cleanly."
End
Showln "Mail Sent!"
End_Procedure