Lua
Lua
Outlook Send Email using MIME Format
See more Outlook Examples
This example sends an email using MIME format via the Microsoft Graph Outlook REST API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Use your previously obtained access token here:
-- See the following examples for getting an access token:
-- Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
-- Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
-- Refresh Access Token (Azure AD v2.0 Endpoint).
-- Refresh Access Token (Azure AD Endpoint).
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/microsoftGraph.json")
if not success then
print(jsonToken.last_error_text)
return
end
http.auth_token = jsonToken:string_of("access_token")
-- To send email, we'll POST to the following endpoint:
--
-- POST /users/{id | userPrincipalName}/sendMail
--
-- (The special keyword "me" may be used in place of a principal name.)
--
-- The body of the POST request will contain the MIME source of the email in base64 format.
-- Create a new email object
local email = chilkat.Email.new()
email.subject = "Test Outlook API to Send HTML Email with Attachments"
email.from = "Mary <mary@somewhere.com>"
email:add_to("Joe", "joe@example.com")
-- Add a plain-text alternative body, which will likely never be seen.
-- (It is shown if the receiving email client is incapable of displaying HTML email.)
email:add_plain_text_alternative_body("This is a plain-text alternative body...")
-- Our HTML will include an image, so add the related image here.
local contentIdStarfish = email:add_related_file("qa_data/jpg/starfish.jpg")
if not email.last_method_success then
print(email.last_error_text)
return
end
-- The src attribute for the image tag is set to the contentIdStarfish:
local sbHtml = chilkat.StringBuilder.new()
sbHtml:append("<html><body><p>This is an HTML email with an embedded image.</p>")
sbHtml:append("<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>")
local numReplacements = sbHtml:replace("CONTENT_ID_STARFISH", contentIdStarfish)
email:add_html_alternative_body(sbHtml:get_as_string())
-- Finally, add some attachments to the email.
-- Add a file attachment.
success = email:add_file_attachment2("qa_data/pdf/fishing.pdf", "application/pdf")
if not success then
print(email.last_error_text)
return
end
-- Add an attachment where the content is contained in a string.
local content = "This is the content of the 2nd attached file."
email:add_string_attachment("someText.txt", content)
-- Get the email as multi-line base64..
local bdMime = chilkat.BinData.new()
email:get_mime_bd(bdMime)
-- Now get it as multi-line base64
local sbBase64 = chilkat.StringBuilder.new()
bdMime:get_encoded_sb("base64_mime", sbBase64)
-- Send the HTTP POST (i.e. send the email)
local resp = chilkat.HttpResponse.new()
success = http:http_sb("POST", "https://graph.microsoft.com/v1.0/me/sendMail", sbBase64, "utf-8", "text/plain", resp)
if not success then
print(http.last_error_text)
return
end
-- The send succeeded if the response status code = 202.
-- In the success case, there is no response body. (We just get the response code to know that it succeeded.)
if resp.status_code ~= 202 then
local json = chilkat.JsonObject.new()
json:load(resp.body_str)
json.emit_compact = false
print(json:emit())
print("Failed, response status code = " .. resp.status_code)
else
print("Outlook Mail Sent.")
end