Managing Digital Signatures with Aspose.Email
The library provides the LoadOptions class, an abstract base class that allows the user to specify additional options when loading a MailMessage from a particular format. The signature preserving option is set by default.
Convert EML to MSG While Preserving Signatures
The code sample below demonstrates how to load a message, convert it to MSG format and save as MSG (the signature is preserved by default):
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
message = ae.MailMessage.load("Message.eml", loadOptions)
# Save as MSG
message.save("ConvertEMLToMSG_out.msg", ae.SaveOptions.default_msg_unicode)
Convert S/MIME Messages from MSG to EML
Aspose.Email allows you to convert from MSG to EML preserving a digital signature as shown in the following code snippet.
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
smime_eml = ae.MailMessage.load("smime.eml", loadOptions)
conversion_options = ae.mapi.MapiConversionOptions(ae.mapi.OutlookMessageFormat.UNICODE)
msg = ae.mapi.MapiMessage.from_mail_message(smime_eml, conversion_options)
# Save File to disk
msg.save("ConvertMIMEMessagesFromMSGToEML_out.msg")
Check Signatures of Secure Emails
The following features are available to check the signature of MapiMessage objects:
- the SecureEmailManager class for checking the signature of secure emails;
- the SmimeResult class that stores the result of the check in its
is_successproperty; - the
check_signature(msg)andcheck_signature(msg, certificate)methods of theSecureEmailManagerclass.
bytes object.
Check the signature of a message without providing a certificate:
import aspose.email as ae
from aspose.email.mapi import MapiMessage
msg = MapiMessage.load(data_dir + "signed.msg")
result = ae.SecureEmailManager().check_signature(msg)
print("Signature is valid: " + str(result.is_success))
Check the signature of an encrypted message by providing the certificate used for decryption:
import aspose.email as ae
from aspose.email.mapi import MapiMessage
with open(data_dir + "cert.pfx", "rb") as f:
certificate = f.read()
msg = MapiMessage.load(data_dir + "signed_and_encrypted.msg")
result = ae.SecureEmailManager().check_signature(msg, certificate)
print("Signature is valid: " + str(result.is_success))
Remove Signatures from MapiMessages
For better compatibility, the remove_signature method and the is_signed property of the MapiMessage class are used to remove a digital signature from a message:
from aspose.email.mapi import MapiMessage
msg = MapiMessage.load(data_dir + "signed.msg")
if msg.is_signed:
unsigned_msg = msg.remove_signature()
unsigned_msg.save(data_dir + "unsigned.msg")
Decrypt MapiMessages with Certificates
If you have encrypted MAPI messages and need to decrypt them with the private key stored in a certificate, the following members of the MapiMessage class are useful:
is_encrypted– gets a value indicating whether the message is encrypted.decrypt()– decrypts the message. The method searches the current user and computer My stores for the appropriate certificate and private key.decrypt(certificate)– decrypts the message with the given certificate. The certificate is passed as abytesobject and must contain the private key.
The following code snippet shows how to work with encrypted MAPI messages:
from aspose.email.mapi import MapiMessage
with open(data_dir + "private_cert.pfx", "rb") as f:
private_cert = f.read()
msg = MapiMessage.load(data_dir + "encrypted.msg")
if msg.is_encrypted:
decrypted_msg = msg.decrypt(private_cert)
print(decrypted_msg.subject)