Sample code for 30+ languages & platforms
CkJava Requires Chilkat v11.0.0+

Sign an EML using RSA-PSS and SHA-256

See more Email Object Examples

Load the intended signing certificate and its RSA private key from a PFX. Set the signature options on Email and render the final S/MIME message through MailMan. The resulting multipart/signed file can be verified independently. Supply the PFX password through your application’s credential store.

Chilkat CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmail email = new CkEmail();
    success = email.LoadEml("message.eml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    CkCert cert = new CkCert();
    success = cert.LoadPfxFile("signer.pfx","PFX_PASSWORD");
    if (success != true) {
        System.out.println(cert.lastErrorText());
        return;
        }

    success = email.SetSigningCert(cert);
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    email.put_SigningAlg("pss");
    email.put_SigningHashAlg("sha256");
    email.put_SendSigned(true);
    CkMailMan mail = new CkMailMan();
    mail.put_OpaqueSigning(false);
    CkBinData rendered = new CkBinData();
    success = mail.RenderToMimeBd(email,rendered);
    if (success != true) {
        System.out.println(mail.lastErrorText());
        return;
        }

    success = rendered.WriteFile("signed.eml");
    if (success != true) {
        System.out.println("Failed: rendered.WriteFile.");
        return;
        }

    System.out.println("Saved signed.eml.");
  }
}