Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
Sign a saved MIME message with DKIM
See more DKIM Examples
Sign the complete bytes of message.eml and save the result as signed.eml. Finish the message’s From, To, Date, Message-ID, MIME parts and transfer encodings before signing. Existing signatures stay in place and the new signature is inserted first.
The example signs the full body and includes From in the signed headers. Relaxed canonicalization tolerates defined whitespace and header folding changes. Editing a signed field or changing the body text can still break verification. Prepare the matching DNS record.
Chilkat Delphi DLL Downloads
var
success: Boolean;
message: HCkBinData;
privateKey: HCkPrivateKey;
signer: HCkDkim;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
message := CkBinData_Create();
success := CkBinData_LoadFile(message,'message.eml');
if (success <> True) then
begin
Memo1.Lines.Add('Could not read message.eml.');
Exit;
end;
// Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
privateKey := CkPrivateKey_Create();
success := CkPrivateKey_LoadAnyFormatFile(privateKey,'dkim-private.pem','');
if (success <> True) then
begin
Memo1.Lines.Add(CkPrivateKey__lastErrorText(privateKey));
Exit;
end;
signer := CkDkim_Create();
CkDkim_putDkimDomain(signer,'example.com');
CkDkim_putDkimSelector(signer,'mail2026');
CkDkim_putDkimAlg(signer,'rsa-sha256');
success := CkDkim_SetDkimPrivateKey(signer,privateKey);
if (success <> True) then
begin
Memo1.Lines.Add(CkDkim__lastErrorText(signer));
Exit;
end;
// DkimCanon takes a single setting and applies it to both headers and body.
CkDkim_putDkimCanon(signer,'relaxed');
CkDkim_putDkimHeaders(signer,'from:to:subject:date:message-id:mime-version:content-type:content-transfer-encoding');
// Zero signs the entire canonicalized body and omits the optional l= tag.
CkDkim_putDkimBodyLengthCount(signer,0);
success := CkDkim_DkimSign(signer,message);
if (success <> True) then
begin
Memo1.Lines.Add(CkDkim__lastErrorText(signer));
Exit;
end;
success := CkBinData_WriteFile(message,'signed.eml');
if (success <> True) then
begin
Memo1.Lines.Add('Could not write signed.eml.');
Exit;
end;
Memo1.Lines.Add('Saved signed.eml.');
CkBinData_Dispose(message);
CkPrivateKey_Dispose(privateKey);
CkDkim_Dispose(signer);