Sample code for 30+ languages & platforms
Unicode C

POP3 Login Secure

Demonstrates how to set the POP3 password using a secure string.

This example requires Chilkat v9.5.0.71 or greater.

Chilkat Unicode C Downloads

Unicode C
#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;
    int numMessages;

    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:

    // {
    //   "pop3_login": "D7lnlWY291LKMeChlvQq7N/Tg7ieuSB15a+9Ekyd0R4=",
    //   "pop3_password": "5neIq9Jmn0E3p71N6Yc8TA=="
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_LoadFile(json,L"qa_data/passwords/pop3.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"pop3_login"),ssLogin);
    CkCrypt2W_DecryptSecureENC(crypt,CkJsonObjectW_stringOf(json,L"pop3_password"),ssPassword);

    // Set the POP3 domain, login, and password
    CkMailManW_putMailHost(mailman,L"pop.gmail.com");
    CkMailManW_putPopSsl(mailman,TRUE);
    CkMailManW_putMailPort(mailman,995);

    CkMailManW_putPopUsername(mailman,CkSecureStringW_access(ssLogin));
    // Set the PopPassword property via the SetPassword method using the secure string.
    CkMailManW_SetPassword(mailman,L"pop3",ssPassword);

    // Connect and authenticate..
    success = CkMailManW_Pop3Connect(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_Pop3Authenticate(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"POP3 login successful.\n");

    numMessages = CkMailManW_GetMailboxCount(mailman);
    if (numMessages < 0) {
        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"Num messages in Inbox = %d\n",numMessages);

    CkMailManW_Pop3EndSession(mailman);


    CkMailManW_Dispose(mailman);
    CkJsonObjectW_Dispose(json);
    CkCrypt2W_Dispose(crypt);
    CkSecureStringW_Dispose(ssLogin);
    CkSecureStringW_Dispose(ssPassword);

    }