Sample code for 30+ languages & platforms
Java 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 Java Downloads

Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.MailMan;

public class ChilkatExample {

    public static void main(String[] args) {
        // Assumes Chilkat is unlocked. See Global Unlock Sample.

        Email email = new Email();
        try {
            email.loadEml("message.eml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Cert cert = new Cert();
        try {
            cert.loadPfxFile("signer.pfx", "PFX_PASSWORD");

            email.setSigningCert(cert);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        email.setSigningAlg("pss");
        email.setSigningHashAlg("sha256");
        email.setSendSigned(true);
        MailMan mail = new MailMan();
        mail.setOpaqueSigning(false);
        BinData rendered = new BinData();
        try {
            mail.renderToMimeBd(email, rendered);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        try {
            rendered.writeFile("signed.eml");
        } catch (ChilkatException e) {
            System.out.println("Failed: rendered.WriteFile.");
            return;
        }

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