Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkEmailW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkBinDataW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW jsonToken;
HCkEmailW email;
const wchar_t *contentIdStarfish;
HCkStringBuilderW sbHtml;
int numReplacements;
const wchar_t *content;
HCkBinDataW bdMime;
HCkStringBuilderW sbBase64;
HCkHttpResponseW resp;
HCkJsonObjectW json;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// 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).
jsonToken = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/microsoftGraph.json");
if (success == FALSE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(jsonToken));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(jsonToken);
return;
}
CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"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
email = CkEmailW_Create();
CkEmailW_putSubject(email,L"Test Outlook API to Send HTML Email with Attachments");
CkEmailW_putFrom(email,L"Mary <mary@somewhere.com>");
CkEmailW_AddTo(email,L"Joe",L"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.)
CkEmailW_AddPlainTextAlternativeBody(email,L"This is a plain-text alternative body...");
// Our HTML will include an image, so add the related image here.
contentIdStarfish = CkEmailW_addRelatedFile(email,L"qa_data/jpg/starfish.jpg");
if (CkEmailW_getLastMethodSuccess(email) == FALSE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(jsonToken);
CkEmailW_Dispose(email);
return;
}
// The src attribute for the image tag is set to the contentIdStarfish:
sbHtml = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbHtml,L"<html><body><p>This is an HTML email with an embedded image.</p>");
CkStringBuilderW_Append(sbHtml,L"<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>");
numReplacements = CkStringBuilderW_Replace(sbHtml,L"CONTENT_ID_STARFISH",contentIdStarfish);
CkEmailW_AddHtmlAlternativeBody(email,CkStringBuilderW_getAsString(sbHtml));
// Finally, add some attachments to the email.
// Add a file attachment.
success = CkEmailW_AddFileAttachment2(email,L"qa_data/pdf/fishing.pdf",L"application/pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(jsonToken);
CkEmailW_Dispose(email);
CkStringBuilderW_Dispose(sbHtml);
return;
}
// Add an attachment where the content is contained in a string.
content = L"This is the content of the 2nd attached file.";
CkEmailW_AddStringAttachment(email,L"someText.txt",content);
// Get the email as multi-line base64..
bdMime = CkBinDataW_Create();
CkEmailW_GetMimeBd(email,bdMime);
// Now get it as multi-line base64
sbBase64 = CkStringBuilderW_Create();
CkBinDataW_GetEncodedSb(bdMime,L"base64_mime",sbBase64);
// Send the HTTP POST (i.e. send the email)
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpSb(http,L"POST",L"https://graph.microsoft.com/v1.0/me/sendMail",sbBase64,L"utf-8",L"text/plain",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(jsonToken);
CkEmailW_Dispose(email);
CkStringBuilderW_Dispose(sbHtml);
CkBinDataW_Dispose(bdMime);
CkStringBuilderW_Dispose(sbBase64);
CkHttpResponseW_Dispose(resp);
return;
}
// 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 (CkHttpResponseW_getStatusCode(resp) != 202) {
json = CkJsonObjectW_Create();
CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp));
CkJsonObjectW_putEmitCompact(json,FALSE);
wprintf(L"%s\n",CkJsonObjectW_emit(json));
wprintf(L"Failed, response status code = %d\n",CkHttpResponseW_getStatusCode(resp));
}
else {
wprintf(L"Outlook Mail Sent.\n");
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(jsonToken);
CkEmailW_Dispose(email);
CkStringBuilderW_Dispose(sbHtml);
CkBinDataW_Dispose(bdMime);
CkStringBuilderW_Dispose(sbBase64);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(json);
}