Archived Forum Post

Index of archived forum posts

Question:

Create s/mime encrypted message issue

Dec 07 '12 at 14:49

Hi, We are evaluating Chilkat .NET component to purchase the license. I am trying to create s/mime encrypted message using this example. It looks like "encrypted.txt" file doesn't get encrytped. Could you please let me know if I am doing something wrong? Thank you.

        Chilkat.Mime mime = new Chilkat.Mime();
        bool success;
        //Any string passed to UnlockComponent begins the 30-day trial
        //mime.UnlockComponent("30-day trial");
        success = mime.UnlockComponent("Anything for 30-day trial");
        if (success == false)
        {
            return;
        }
       // success = mime.NewMultipartMixed();
        mime.SetBodyFromXml("This is a message containing a XML file.");
        //  Now insert some files:
        mime.AppendPartFromFile("sampleXml.xml");
        if (success == false)
        {
            return;
        }
         mime.SaveMime("unencrypted.txt");
        Chilkat.CreateCS ccs = new Chilkat.CreateCS();
        Chilkat.CertStore certStore = ccs.OpenCurrentUserStore();
        Chilkat.Cert cert = certStore.FindCertForEmail("matt@chilkatsoft.com");

        // Use the certificate for encryption.
        // This creates an x-pkcs7-mime enveloped message.
        mime.Encrypt(cert);
        mime.SaveMime("encrypted.txt");
        Chilkat.Mime part2 = null;
        part2 = mime.GetPart(1);
        byte[] binaryData = null;
        binaryData = part2.GetBodyBinary();

Accepted Answer

There are many things wrong with the code fragment.

  1. First, make sure you understand some basic things about MIME: A MIME message is basically a number of header fields, one per line, followed by a double CRLF, followed by the MIME body. The SetBodyFromXml method is setting the body part of the MIME object (i.e. what comes after the double CRLF after the headers). Also, as the name of the function implies, you should be passing an XML string to it -- but you're not. So that doesn't make sense.
  2. There are many Chilkat methods calls in your code snippet that return a success/failure status. You need to check the return value of each method that could fail. If your code misses a failed returns status and blindly continues onward, you can't expect anything good -- and you'll be wondering what went wrong. Check the error where it FIRST occurs, then look at the LastErrorText property on the object instance w/ the failed method call.