Sample code for 30+ languages & platforms
Objective-C

IMAP Read Encrypted Email

See more IMAP Examples

Demonstrates how to read encrypted email from an IMAP mailbox.

Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

Information about the original encrypted state of the email is available after it has been downloaded and decrypted.

Chilkat Objective-C Downloads

Objective-C
#import <CkoImap.h>
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoMessageSet.h>
#import <CkoEmail.h>
#import <CkoCert.h>

BOOL success = NO;

CkoImap *imap = [[CkoImap alloc] init];

imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];
success = [imap Connect: @"imap.example2.com"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

// We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager.
// See how we originally saved the email credentials to the Keychain here:
// Save Email Credentials in Apple Keychain or Windows Credentials Manager
CkoSecrets *secrets = [[CkoSecrets alloc] init];

// On Windows, this is the Windows Credentials Manager
// On MacOS/iOS, it is the Apple Keychain
secrets.Location = @"local_manager";

// Specify the name of the secret.
// service and username are required.
// appName and domain are optional.
// Note: The values are arbitrary and can be anything you want.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"appName" value: @"MyEmailApp"];
[json UpdateString: @"service" value: @"IMAP"];
[json UpdateString: @"domain" value: @"example2.com"];
[json UpdateString: @"username" value: @"jane@example2.com"];

NSString *password = [secrets GetSecretStr: json];
if (secrets.LastMethodSuccess == NO) {
    NSLog(@"%@",secrets.LastErrorText);
    return;
}

success = [imap Login: @"jane@example2.com" password: password];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

// Select an IMAP mailbox
success = [imap SelectMailbox: @"Inbox"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

// This example: Send Encrypted Email using Certificate in Apple Keychain
// sent an email with the subject "This email is encrypted".
// Let's download an email with the word "encrypted" in the subject.

CkoMessageSet *messageSet = [[CkoMessageSet alloc] init];
success = [imap QueryMbx: @"SUBJECT encrypted" bUid: YES msgSet: messageSet];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

if ([messageSet.Count intValue] == 0) {
    NSLog(@"%@",@"No messages found.");
    return;
}

// Reading encrypted email works the same as reading non-encrypted email. 
// If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), 
// Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly.

unsigned long uid = [messageSet GetId: [NSNumber numberWithInt: 0]];

CkoEmail *email = [[CkoEmail alloc] init];
success = [imap FetchEmail: NO msgId: uid bUid: YES email: email];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

// Here we can show if the email was received encrypted, if it was successfully decrypted, and
// which certificate was used to decrypt.
NSLog(@"%@%d",@"Email received encrypted: ",email.ReceivedEncrypted);

// Was it successfully decrypted?
NSLog(@"%@%d",@"Successfully decrypted: ",email.Decrypted);

// What cert was used to decrypt?
NSLog(@"%@%@",@"Encrypted by: ",email.EncryptedBy);
CkoCert *cert = [[CkoCert alloc] init];
success = [email LastDecryptCert: cert];
if (success != NO) {
    NSLog(@"%@%@",@"Certificate DN: ",cert.SubjectDN);
}

// Show the decrypted email body.
NSLog(@"%@",email.Body);

[imap Disconnect];