Unicode C
Unicode C
IMAP Download and Verify Signed (S/MIME) Email
See more IMAP Examples
Demonstrates how to download and verify digitally signed S/MIME email.Chilkat Unicode C Downloads
#include <C_CkImapW.h>
#include <C_CkMessageSetW.h>
#include <C_CkEmailW.h>
#include <C_CkCertW.h>
void ChilkatSample(void)
{
BOOL success;
HCkImapW imap;
BOOL fetchUids;
HCkMessageSetW messageSet;
HCkEmailW email;
HCkCertW cert;
int i;
const wchar_t *uid;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap = CkImapW_Create();
// Connect to an IMAP server.
// Use TLS
CkImapW_putSsl(imap,TRUE);
CkImapW_putPort(imap,993);
success = CkImapW_Connect(imap,L"imap.example.com");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
success = CkImapW_Login(imap,L"myLogin",L"myPassword");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// Select an IMAP mailbox
success = CkImapW_SelectMailbox(imap,L"Inbox");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
return;
}
// We can choose to fetch UIDs or sequence numbers.
fetchUids = TRUE;
// Get the message IDs of all the emails in the mailbox
messageSet = CkMessageSetW_Create();
success = CkImapW_QueryMbx(imap,L"ALL",fetchUids,messageSet);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(messageSet);
return;
}
email = CkEmailW_Create();
cert = CkCertW_Create();
i = 0;
while (i < CkMessageSetW_getCount(messageSet)) {
uid = CkMessageSetW_GetId(messageSet,i);
wprintf(L"uid: %s\n",uid);
success = CkImapW_FetchEmail(imap,FALSE,uid,TRUE,email);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(messageSet);
CkEmailW_Dispose(email);
CkCertW_Dispose(cert);
return;
}
// The security layers of signed and/or encrypted emails
// are automatically "unwrapped" when loaded into
// a Chilkat email object.
// An application only needs to check to see if an email
// was received signed or encrypted, and then examine
// the success/failure. For example:
if (CkEmailW_getReceivedSigned(email) == TRUE) {
wprintf(L"This email was signed.\n");
// Check to see if the signatures were verified.
if (CkEmailW_getSignaturesValid(email) == TRUE) {
wprintf(L"Digital signature(s) verified.\n");
wprintf(L"Signer: %s\n",CkEmailW_signedBy(email));
// Get the certificate used for signing.
success = CkEmailW_LastSignerCert(email,0,cert);
if (success == FALSE) {
wprintf(L"Failed to get signing certificate object.\n");
}
else {
wprintf(L"Signing cert: %s\n",CkCertW_subjectCN(cert));
}
}
else {
wprintf(L"Digital signature verification failed.\n");
}
}
i = i + 1;
}
// Disconnect from the IMAP server.
success = CkImapW_Disconnect(imap);
CkImapW_Dispose(imap);
CkMessageSetW_Dispose(messageSet);
CkEmailW_Dispose(email);
CkCertW_Dispose(cert);
}