Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.8.0+

Add a PEM Source of Certificates and Private Keys for S/MIME

See more MIME Examples

Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.

AddPemSource adds the certificates and private keys of a Pem object to the sources that are searched for certificates and private keys by S/MIME operations such as decryption.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. A Pem object is a convenient container for one or more certificates and private keys. AddPemSource can be called multiple times. Each call adds more certificates to the ones already added. AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
mime: TChilkatMime;
pem: TChilkatPem;
pemPassword: WideString;

begin
success := 0;

mime := TChilkatMime.Create(Self);
success := mime.LoadMimeFile('qa_data/encrypted.eml');
if (success = 0) then
  begin
    Memo1.Lines.Add(mime.LastErrorText);
    Exit;
  end;

//  Use a Pem object as a source of certificates and private keys for S/MIME operations.
//  Here the Pem object is loaded from a PEM file.  The 2nd argument of LoadPemFile is the
//  password of the private key.
pem := TChilkatPem.Create(Self);
pemPassword := 'myPemPassword';
success := pem.LoadPemFile('qa_data/recipient.pem',pemPassword);
if (success = 0) then
  begin
    Memo1.Lines.Add(pem.LastErrorText);
    Exit;
  end;

//  The only argument is the Pem object.  AddPemSource can be called multiple times.  Each call
//  adds more certificates to the ones already added.
success := mime.AddPemSource(pem.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(mime.LastErrorText);
    Exit;
  end;

//  Decryption now draws credentials from the added certificates and private keys.
success := mime.Decrypt();
if (success = 0) then
  begin
    Memo1.Lines.Add(mime.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('Decrypted using the certificate and private key of the PEM source.');