Sample code for 30+ languages & platforms
Objective-C

Send an Email S/MIME Encrypted

See more Email Object Examples

Demonstrates the Chilkat Email.SendEncrypted property. Set it to true to have the email sent S/MIME encrypted (the default is false). Setting this property alone is not enough — encryption requires one or more recipient certificates, supplied with AddEncryptCert or SetEncryptCert. This example loads a recipient certificate, registers it, and enables encrypted sending.

Background: To encrypt a message for someone, you need their public certificate — which is why only a .cer file (no private key) is required here. The message is scrambled so that only the holder of the matching private key, i.e. the intended recipient, can read it. When there are multiple recipients, you add each one's certificate, and Chilkat encrypts the one-time content key separately for each so they can all decrypt the same message.

Chilkat Objective-C Downloads

Objective-C
#import <CkoEmail.h>
#import <CkoCert.h>

BOOL success = NO;

//  Demonstrates the Email.SendEncrypted property.  Set to true to have the email sent
//  S/MIME encrypted.  Encryption also requires one or more recipient certificates,
//  supplied via AddEncryptCert or SetEncryptCert.  The default is false.

CkoEmail *email = [[CkoEmail alloc] init];
email.Subject = @"Encrypted email";
email.Body = @"This message will be sent S/MIME encrypted.";
email.From = @"alice@example.com";
[email AddTo: @"Bob" emailAddress: @"bob@example.com"];

//  Load the recipient's certificate (only the public key is needed to encrypt).
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadFromFile: @"qa_data/certs/recipient.cer"];
if (success == NO) {
    NSLog(@"%@",cert.LastErrorText);
    return true;
}

//  Supply the recipient certificate used for encryption.
success = [email AddEncryptCert: cert];
if (success == NO) {
    NSLog(@"%@",email.LastErrorText);
    return true;
}

//  Request that the email be sent encrypted.
email.SendEncrypted = YES;

NSLog(@"%@%d",@"SendEncrypted = ",email.SendEncrypted);

//  Note: Paths such as "qa_data/..." are relative local filesystem paths,
//  relative to the current working directory of the running application.