Sample code for 30+ languages & platforms
Swift

Add S/MIME Signature using PFX

See more MIME Examples

Add a digital signature to a MIME message using the certificate + private key from a PFX file.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let mime = CkoMime()!

    // Load a PFX file into a certificate object.
    let cert = CkoCert()!
    var pfxFilepath: String? = "pfxFiles/something.pfx"
    var pfxPassword: String? = "secret"
    success = cert.loadPfxFile(path: pfxFilepath, password: pfxPassword)
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    success = mime.setBody(fromPlainText: "This is the plain-text MIME body.")

    mime.charset = "utf-8"
    mime.encoding = "quoted-printable"

    // Sign the MIME (adds a PKCS7 detached signature)
    success = mime.addDetachedSignature(cert: cert)
    if success == false {
        print("\(mime.lastErrorText!)")
        return
    }

    // Save the S/MIME to a file.
    success = mime.save(path: "/temp/signedMime.txt")
    if success == false {
        print("\(mime.lastErrorText!)")
        return
    }

    print("success!")

}