Sample code for 30+ languages & platforms
PureBasic

Send Signed and Encrypted EDI Email (EDIFACT)

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ; 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.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Create a new email object
    email.i

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

    CkEmail::setCkSubject(email, "This is the subject")
    CkEmail::setCkFrom(email, "Chilkat Admin <admin@chilkatsoft.com>")
    success = CkEmail::ckAddTo(email,"Chilkat Support","support@chilkatsoft.com")

    ;  Indicate that the email should be sent signed.
    CkEmail::setCkSendSigned(email, 1)

    ;  Tell the mailman to use a PFX file as a source for locating
    ;  the certificate and private key required for signing.
    ;  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.
    success = CkMailMan::ckAddPfxSourceFile(mailman,"/pfx_files/myCertAndPrivateKey.pfx","secret")
    If success <> 1
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    ;  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.
    cert.i
    success = CkCert::ckLoadFromFile(cert,"/certs/recipientCert.cer")
    If success <> 1
        Debug CkCert::ckLastErrorText(cert)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    ;  Indicate that the email is to be sent encrypted.
    CkEmail::setCkSendEncrypted(email, 1)

    ;  Indicate that we want 192-bit 3DES encryption:
    CkEmail::setCkPkcs7CryptAlg(email, "3des")
    CkEmail::setCkPkcs7KeyLength(email, 192)

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

    ;  Tell the mailman to use an opaque signature (as opposed to a detached signature)
    CkMailMan::setCkOpaqueSigning(mailman, 1)

    ;  SMTP server (use your settings and refer to the online
    ;  reference  documentation for more options)
    CkMailMan::setCkSmtpHost(mailman, "smtp.mymailserver.com")
    CkMailMan::setCkSmtpUsername(mailman, "myLogin")
    CkMailMan::setCkSmtpPassword(mailman, "myPassword")
    CkMailMan::setCkSmtpPort(mailman, 587)
    CkMailMan::setCkStartTLS(mailman, 1)

    ;  Send the signed/encrypted EDI email
    success = CkMailMan::ckSendEmail(mailman,email)
    If success <> 1
        Debug CkMailMan::ckLastErrorText(mailman)
    Else
        Debug "Mail Sent!"
    EndIf



    CkMailMan::ckDispose(mailman)


    ProcedureReturn
EndProcedure