Managing Digitally Signed Messages

Aspose.Email implements the complete S/MIME email object algorithm. This gives the API complete power to add digital signatures to email messages, preserve them while converting messages between formats, remove them, etc.

Attach a Signature to an Email

The SecureEmailManager.attachSignature method allows you to attach digital signatures to email messages. After attaching the signature, verify the results through properties like IsSigned, MessageClass, and attachment details.

You can provide a MailMessage or MapiMessage, a private key loaded as an SmimeKey, and signature options to customize the signature attachment process with the SignatureOptions class which enables users to specify various options for the signature attachment, including detached or non-detached signatures.

The following code sample will show you how to load a message from a file, attach a detached and non-detached digital signature using a private certificate, and then check if the signatures were attached successfully.

String dataDir = "data/";

// Load the private key and its certificate; the password is a char array
SmimeKey privateCert = SmimeKey.loadPFX(dataDir + "certFile.pfx", "password".toCharArray());

MapiMessage msg = MapiMessage.load(dataDir + "message.msg");

SignatureOptions opt = new SignatureOptions();
opt.setDetached(true);
MapiMessage signedDetached = new SecureEmailManager().attachSignature(msg, privateCert, opt);

if (signedDetached.isSigned()) {
    System.out.println("Detached Signature Attached Successfully.");
}

opt.setDetached(false);
MapiMessage signedNonDetached = new SecureEmailManager().attachSignature(msg, privateCert, opt);

if (signedNonDetached.isSigned()) {
    System.out.println("Non-Detached Signature Attached Successfully.");
}

Preserving Signature when Converting from EML to MSG

Aspose.Email preserves the digital signature when converting from EML to MSG. The following code snippet shows you how to convert from EML to MSG.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Load mail message
MailMessage message = MailMessage.load(dataDir + "Message.eml", new EmlLoadOptions());
// Save as MSG
message.save(dataDir + "ConvertEMLToMSG_out.msg", SaveOptions.getDefaultMsgUnicode());

Remove Signature from an Outlook Message File

If you’re facing the necessity to remove the signature from a message in a MAPI format, for example for compatibility purposes, Aspose.Email offers the MapiMessage.removeSignature method and the MapiMessage.isSigned property.

The following code snippet shows how to load a MAPI message from a file, check if it is digitally signed, and if so, create a new message without the digital signature:

MapiMessage msg = MapiMessage.load("data/signed.msg");

if (msg.isSigned()) {
    MapiMessage unsignedMsg = msg.removeSignature();
}

Converting S/MIME Messages from MSG to EML

Aspose.Email preserves the digital signature when converting from MSG to EML as shown in the following code snippet.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

MapiMessage mapi = MapiMessage.load(dataDir + "message.msg");

// The signature is kept when the message is converted
MailMessage eml = mapi.toMailMessage(new MailConversionOptions());
eml.save(dataDir + "ConvertMIMEMessagesFromMSGToEML_out.eml", SaveOptions.getDefaultEml());

Decrypt an MSG File with Certificate

If you have encrypted MAPI messages and need to decrypt them using the private key stored in a certificate, the following features of Aspose.Email can be useful:

Unlike the .NET API, the Java API has no parameterless decrypt() that searches the Windows certificate stores: the key always has to be passed in.

The following code snippet shows how to work with encrypted MAPI messages:

String dataDir = "data/";

SmimeKey privateCert = SmimeKey.loadPFX(dataDir + "privateCertFile.pfx", "password".toCharArray());
MapiMessage msg = MapiMessage.load(dataDir + "encrypted.msg");

if (msg.isEncrypted()) {
    MapiMessage decryptedMsg = msg.decrypt(privateCert);

    // or, with the raw content of the PFX file:
    // MapiMessage decryptedMsg = msg.decrypt(Files.readAllBytes(Paths.get(dataDir + "privateCertFile.pfx")), "password");
}

Check Signatures of Secure Emails

The following API members are available to check the signature of MapiMessage and MailMessage objects:

  • the SecureEmailManager class for checking the signature of secure emails;
  • the SmimeResult class, which stores the results of the check;
  • the checkSignature(MapiMessage msg) and checkSignature(MapiMessage msg, SmimeKey key) methods.

Check the signature of a message without providing a certificate:

MapiMessage msg = MapiMessage.load("signed.msg");

SmimeResult result = new SecureEmailManager().checkSignature(msg);

System.out.println("Signature valid: " + result.isSuccess());
if (!result.isSuccess()) {
    System.out.println("Error: " + result.getError());
}

Check the signature of an encrypted message by providing the certificate used for decryption:

MapiMessage msg = MapiMessage.load("signed-and-encrypted.msg");

SmimeKey key = SmimeKey.loadPFX("cert.pfx", "pass".toCharArray());

SmimeResult result = new SecureEmailManager().checkSignature(msg, key);

System.out.println("Signature valid: " + result.isSuccess());
for (java.security.cert.X509Certificate certificate : result.getSigningCertificates()) {
    System.out.println("Signed by: " + certificate.getSubjectX500Principal());
}