Unicode C
Unicode C
SMTP Login Secure
Demonstrates how to set the SMTP password using a secure string.This example requires Chilkat v9.5.0.71 or greater.
Chilkat Unicode C Downloads
#include <C_CkMailManW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkCrypt2W.h>
#include <C_CkSecureStringW.h>
void ChilkatSample(void)
{
BOOL success;
HCkMailManW mailman;
HCkJsonObjectW json;
HCkCrypt2W crypt;
HCkSecureStringW ssLogin;
HCkSecureStringW ssPassword;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
mailman = CkMailManW_Create();
// Imagine we've previously saved our encrypted login and password within a JSON config file
// that contains this:
// {
// "smtp_login": "D7lnlWY291LKMeChlvQq7N/Tg7ieuSB15a+9Ekyd0R4=",
// "smtp_password": "5neIq9Jmn0E3p71N6Yc8TA=="
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_LoadFile(json,L"qa_data/passwords/smtp.json");
crypt = CkCrypt2W_Create();
// These are the encryption settings we previously used to encrypt the credentials within the JSON config file.
CkCrypt2W_putCryptAlgorithm(crypt,L"aes");
CkCrypt2W_putCipherMode(crypt,L"cbc");
CkCrypt2W_putKeyLength(crypt,128);
CkCrypt2W_SetEncodedKey(crypt,L"000102030405060708090A0B0C0D0E0F",L"hex");
CkCrypt2W_SetEncodedIV(crypt,L"000102030405060708090A0B0C0D0E0F",L"hex");
CkCrypt2W_putEncodingMode(crypt,L"base64");
ssLogin = CkSecureStringW_Create();
ssPassword = CkSecureStringW_Create();
// Decrypt to the secure string. (the strings will still held in memory encrypted, but are now encrypted using
// a randomly generated session key.)
CkCrypt2W_DecryptSecureENC(crypt,CkJsonObjectW_stringOf(json,L"smtp_login"),ssLogin);
CkCrypt2W_DecryptSecureENC(crypt,CkJsonObjectW_stringOf(json,L"smtp_password"),ssPassword);
// Set the SMTP domain, login, and password
CkMailManW_putSmtpHost(mailman,L"smtp.gmail.com");
CkMailManW_putSmtpSsl(mailman,TRUE);
CkMailManW_putSmtpPort(mailman,465);
CkMailManW_putSmtpUsername(mailman,CkSecureStringW_access(ssLogin));
// Set the SmtpPassword property via the SetPassword method using the secure string.
CkMailManW_SetPassword(mailman,L"smtp",ssPassword);
// Connect and authenticate..
success = CkMailManW_SmtpConnect(mailman);
if (success != TRUE) {
wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
CkMailManW_Dispose(mailman);
CkJsonObjectW_Dispose(json);
CkCrypt2W_Dispose(crypt);
CkSecureStringW_Dispose(ssLogin);
CkSecureStringW_Dispose(ssPassword);
return;
}
success = CkMailManW_SmtpAuthenticate(mailman);
if (success != TRUE) {
wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
CkMailManW_Dispose(mailman);
CkJsonObjectW_Dispose(json);
CkCrypt2W_Dispose(crypt);
CkSecureStringW_Dispose(ssLogin);
CkSecureStringW_Dispose(ssPassword);
return;
}
wprintf(L"SMTP login successful.\n");
// ...
CkMailManW_CloseSmtpConnection(mailman);
CkMailManW_Dispose(mailman);
CkJsonObjectW_Dispose(json);
CkCrypt2W_Dispose(crypt);
CkSecureStringW_Dispose(ssLogin);
CkSecureStringW_Dispose(ssPassword);
}