C
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 C Downloads
#include <C_CkImap.h>
#include <C_CkMessageSet.h>
#include <C_CkEmail.h>
#include <C_CkCert.h>
void ChilkatSample(void)
{
BOOL success;
HCkImap imap;
BOOL fetchUids;
HCkMessageSet messageSet;
HCkEmail email;
HCkCert cert;
int i;
const char *uid;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap = CkImap_Create();
// Connect to an IMAP server.
// Use TLS
CkImap_putSsl(imap,TRUE);
CkImap_putPort(imap,993);
success = CkImap_Connect(imap,"imap.example.com");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
success = CkImap_Login(imap,"myLogin","myPassword");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
return;
}
// Select an IMAP mailbox
success = CkImap_SelectMailbox(imap,"Inbox");
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_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 = CkMessageSet_Create();
success = CkImap_QueryMbx(imap,"ALL",fetchUids,messageSet);
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
CkMessageSet_Dispose(messageSet);
return;
}
email = CkEmail_Create();
cert = CkCert_Create();
i = 0;
while (i < CkMessageSet_getCount(messageSet)) {
uid = CkMessageSet_GetId(messageSet,i);
printf("uid: %s\n",uid);
success = CkImap_FetchEmail(imap,FALSE,uid,TRUE,email);
if (success == FALSE) {
printf("%s\n",CkImap_lastErrorText(imap));
CkImap_Dispose(imap);
CkMessageSet_Dispose(messageSet);
CkEmail_Dispose(email);
CkCert_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 (CkEmail_getReceivedSigned(email) == TRUE) {
printf("This email was signed.\n");
// Check to see if the signatures were verified.
if (CkEmail_getSignaturesValid(email) == TRUE) {
printf("Digital signature(s) verified.\n");
printf("Signer: %s\n",CkEmail_signedBy(email));
// Get the certificate used for signing.
success = CkEmail_LastSignerCert(email,0,cert);
if (success == FALSE) {
printf("Failed to get signing certificate object.\n");
}
else {
printf("Signing cert: %s\n",CkCert_subjectCN(cert));
}
}
else {
printf("Digital signature verification failed.\n");
}
}
i = i + 1;
}
// Disconnect from the IMAP server.
success = CkImap_Disconnect(imap);
CkImap_Dispose(imap);
CkMessageSet_Dispose(messageSet);
CkEmail_Dispose(email);
CkCert_Dispose(cert);
}