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

Send Signed and Encrypted EDI Email (EDIFACT)

Demonstrates how to send a signed and encrypted EDI email (EDIFACT email).

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
mailman: HCkMailMan;
email: HCkEmail;
ediMsg: PWideChar;
name: PWideChar;
filename: PWideChar;
charset: PWideChar;
pem: HCkPem;
cert: HCkCert;

begin
success := False;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//   The mailman object is used for sending and receiving email.
mailman := CkMailMan_Create();

//   Create a new email object

//   Build the basic EDI email where the body is the EDIFACT message:
ediMsg := 'UNB+IATB:1+6XPPC ...';
name := 'something';
filename := 'something';
charset := 'iso-8859-1';
CkEmail_SetEdifactBody(email,ediMsg,name,filename,charset);

CkEmail_putSubject(email,'This is the subject');
CkEmail_putFrom(email,'Chilkat Admin <admin@chilkatsoft.com>');
success := CkEmail_AddTo(email,'Chilkat Support','support@chilkatsoft.com');

//   Indicate that the email should be sent signed.
CkEmail_putSendSigned(email,True);

//   Tell the mailman to use a PEM file as a source for locating
//   the certificate and private key required for signing.
//   The PEM file is loaded into a Pem object.  The 2nd argument of
//   LoadPemFile is the password of the private key.
//   The certificate chosen for signing will be the one that
//   matches the sender's email address, which also has
//   a private key.  All intermediate certs in the chain of
//   authentication, up to and including the root, will
//   be included in the signature.
pem := CkPem_Create();
success := CkPem_LoadPemFile(pem,'/pem_files/myCertAndPrivateKey.pem','secret');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkPem__lastErrorText(pem));
    Exit;
  end;
success := CkMailMan_AddPemSource(mailman,pem);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

//   Load the .cer file into a certificate object.
//   When sending S/MIME encrypted email, it is the recipient's
//   certificate that is used for encryption.  Only the public key
//   is needed to encrypt.  The recipient is the only
//   one possessing the private key, and therefore is the only
//   one able to decrypt.

success := CkCert_LoadFromFile(cert,'/certs/recipientCert.cer');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkCert__lastErrorText(cert));
    Exit;
  end;

//   Indicate that the email is to be sent encrypted.
CkEmail_putSendEncrypted(email,True);

//   Indicate that we want 192-bit 3DES encryption:
CkEmail_putPkcs7CryptAlg(email,'3des');
CkEmail_putPkcs7KeyLength(email,192);

//   Specify the certificate to be used for encryption.
success := CkEmail_SetEncryptCert(email,cert);

//   Tell the mailman to use an opaque signature (as opposed to a detached signature)
CkMailMan_putOpaqueSigning(mailman,True);

//   SMTP server (use your settings and refer to the online
//   reference  documentation for more options)
CkMailMan_putSmtpHost(mailman,'smtp.mymailserver.com');
CkMailMan_putSmtpUsername(mailman,'myLogin');
CkMailMan_putSmtpPassword(mailman,'myPassword');
CkMailMan_putSmtpPort(mailman,587);
CkMailMan_putStartTLS(mailman,True);

//   Send the signed/encrypted EDI email
success := CkMailMan_SendEmail(mailman,email);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
  end
else
  begin
    Memo1.Lines.Add('Mail Sent!');
  end;

CkMailMan_Dispose(mailman);
CkPem_Dispose(pem);