Objective-C Requires Chilkat v11.0.0+
Objective-C
Sign an email and send the signed bytes over SMTP
See more DKIM Examples
Compose the email, render it to MIME once, add its DKIM signature, and submit those same bytes to an SMTP server. Put blind-copy recipients in the SMTP envelope only so their addresses stay out of the visible headers.
Use an authorized SMTP account and your own sender domain. Publish the selector’s matching public key first. If your relay rewrites signed content, have it apply the signature after its final edits. A successful SMTP submission means the relay accepted the message. A recipient can then verify it with the public key in DNS.
Chilkat Objective-C Downloads
#import <CkoMailMan.h>
#import <CkoEmail.h>
#import <CkoBinData.h>
#import <CkoPrivateKey.h>
#import <CkoDkim.h>
#import <NSString.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoMailMan *mail = [[CkoMailMan alloc] init];
mail.SmtpHost = @"smtp.example.com";
mail.SmtpPort = [NSNumber numberWithInt:465];
mail.SmtpSsl = YES;
mail.RequireSslCertVerify = YES;
mail.SmtpUsername = @"SMTP_USERNAME";
mail.SmtpPassword = @"SMTP_PASSWORD";
CkoEmail *email = [[CkoEmail alloc] init];
email.From = @"Workshop team <updates@example.com>";
email.Subject = @"Workshop registration";
email.Body = @"Registration opens on Monday.";
success = [email AddTo: @"Newsletter editor" emailAddress: @"editor@example.net"];
if (success != YES) {
NSLog(@"%@",email.LastErrorText);
return;
}
// Render after all headers, recipients, and content are final.
CkoBinData *message = [[CkoBinData alloc] init];
success = [mail RenderToMimeBd: email renderedMime: message];
if (success != YES) {
NSLog(@"%@",mail.LastErrorText);
return;
}
// Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
CkoPrivateKey *privateKey = [[CkoPrivateKey alloc] init];
success = [privateKey LoadAnyFormatFile: @"dkim-private.pem" password: @""];
if (success != YES) {
NSLog(@"%@",privateKey.LastErrorText);
return;
}
CkoDkim *signer = [[CkoDkim alloc] init];
signer.DkimDomain = @"example.com";
signer.DkimSelector = @"mail2026";
signer.DkimAlg = @"rsa-sha256";
success = [signer SetDkimPrivateKey: privateKey];
if (success != YES) {
NSLog(@"%@",signer.LastErrorText);
return;
}
success = [signer DkimSign: message];
if (success != YES) {
NSLog(@"%@",signer.LastErrorText);
return;
}
// archive@example.com is an envelope-only blind copy.
NSString *recipients = @"editor@example.net,archive@example.com";
success = [mail SendMimeBd: @"updates@example.com" recipients: recipients mimeData: message];
if (success != YES) {
NSLog(@"%@",mail.LastErrorText);
return;
}
NSLog(@"%@",@"SMTP relay accepted the signed message.");
success = [mail CloseSmtpConnection];
if (success != YES) {
NSLog(@"%@",@"The SMTP session did not close cleanly.");
}