C
C
Send Signed and Encrypted EDI Email (EDIFACT)
Demonstrates how to send a signed and encrypted EDI email (EDIFACT email).Chilkat C Downloads
#include <C_CkMailMan.h>
#include <C_CkEmail.h>
#include <C_CkCert.h>
void ChilkatSample(void)
{
BOOL success;
HCkMailMan mailman;
HCkEmail email;
const char *ediMsg;
const char *name;
const char *filename;
const char *charset;
HCkCert 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 = 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 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_AddPfxSourceFile(mailman,"/pfx_files/myCertAndPrivateKey.pfx","secret");
if (success != TRUE) {
printf("%s\n",CkMailMan_lastErrorText(mailman));
CkMailMan_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 = CkCert_LoadFromFile(cert,"/certs/recipientCert.cer");
if (success != TRUE) {
printf("%s\n",CkCert_lastErrorText(cert));
CkMailMan_Dispose(mailman);
return;
}
// 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) {
printf("%s\n",CkMailMan_lastErrorText(mailman));
}
else {
printf("Mail Sent!\n");
}
CkMailMan_Dispose(mailman);
}