Sample code for 30+ languages & platforms
Java

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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Mime;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Mime mime = new Mime();

        // Load a PFX file into a certificate object.
        Cert cert = new Cert();
        String pfxFilepath = "pfxFiles/something.pfx";
        String pfxPassword = "secret";
        try {
            cert.loadPfxFile(pfxFilepath, pfxPassword);

            mime.setBodyFromPlainText("This is the plain-text MIME body.");

            mime.setCharset("utf-8");
            mime.setEncoding("quoted-printable");

            // Sign the MIME (adds a PKCS7 detached signature)
            mime.addDetachedSignature(cert);

            // Save the S/MIME to a file.
            mime.saveMime("/temp/signedMime.txt");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("success!");
    }
}