Sample code for 30+ languages & platforms
Unicode C

Send Signed and Encrypted EDI Email (EDIFACT)

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkMailManW.h>
#include <C_CkEmailW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMailManW mailman;
    HCkEmailW email;
    const wchar_t *ediMsg;
    const wchar_t *name;
    const wchar_t *filename;
    const wchar_t *charset;
    HCkCertW cert;

    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 = CkMailManW_Create();

    //  Create a new email object

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

    CkEmailW_putSubject(email,L"This is the subject");
    CkEmailW_putFrom(email,L"Chilkat Admin <admin@chilkatsoft.com>");
    success = CkEmailW_AddTo(email,L"Chilkat Support",L"support@chilkatsoft.com");

    //  Indicate that the email should be sent signed.
    CkEmailW_putSendSigned(email,TRUE);

    //  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 = CkMailManW_AddPfxSourceFile(mailman,L"/pfx_files/myCertAndPrivateKey.pfx",L"secret");
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkMailManW_Dispose(mailman);
        return;
    }

    //  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 = CkCertW_LoadFromFile(cert,L"/certs/recipientCert.cer");
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkMailManW_Dispose(mailman);
        return;
    }

    //  Indicate that the email is to be sent encrypted.
    CkEmailW_putSendEncrypted(email,TRUE);

    //  Indicate that we want 192-bit 3DES encryption:
    CkEmailW_putPkcs7CryptAlg(email,L"3des");
    CkEmailW_putPkcs7KeyLength(email,192);

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

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

    //  SMTP server (use your settings and refer to the online
    //  reference  documentation for more options)
    CkMailManW_putSmtpHost(mailman,L"smtp.mymailserver.com");
    CkMailManW_putSmtpUsername(mailman,L"myLogin");
    CkMailManW_putSmtpPassword(mailman,L"myPassword");
    CkMailManW_putSmtpPort(mailman,587);
    CkMailManW_putStartTLS(mailman,TRUE);

    //  Send the signed/encrypted EDI email
    success = CkMailManW_SendEmail(mailman,email);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
    }
    else {
        wprintf(L"Mail Sent!\n");
    }



    CkMailManW_Dispose(mailman);

    }