Mail.Decrypt Method


Decrypts the encrypted email by digital certificate.

[Visual Basic 6.0]
Public Function Decrypt( _
    Cert As Certitifcate _
) As Mail
[Visual C++]
public: HRESULT Decrypt(
    ICertitifcate * Cert,
    IMail** pVal
);

Parameters

Cert
The certificate to decrypt the email, if set NULL (Nothing in Visual Basic), this method searches certificate from current user certificate storage automatically.

Return Value

The decrypted Mail instance.

Example

[Visual Basic 6.0, VBScript, Visual C++, Delphi] The following example demonstrates how to verify signed email and decrypt encrypted email with EAGetMail POP3 & IMAP4 Component. To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic 6.0]
Const CRYPT_MACHINE_KEYSET = 32
Const CRYPT_USER_KEYSET = 4096
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072

Sub VerifyAndDecryptEmail()

On Error Resume Next
    Dim oMail As New EAGetMailObjLib.Mail
    oMail.LicenseCode = "TryIt"
    oMail.LoadFile "c:\test.eml", False
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Exit Sub
    End If

    If (oMail.IsEncrypted) Then
            ' this email is encrypted, we decrypt it by user default certificate.
            ' you can also use specified certificate like this
            ' Dim oCert As New EAGetMailObjLib.Certificate
            ' oCert.LoadFromFile "c:\test.pfx", "pfxpassword", CRYPT_USER_KEYSET 
            ' Set oMail = oMail.Decrypt(oCert)
            Set oMail = oMail.Decrypt(Nothing)
            If Err.Number <> 0 Then
                MsgBox Err.Description
                Exit Sub
            End If
    End If

    If (oMail.IsSigned) Then
            'this email is digital signed.
            Dim oCert As EAGetMailObjLib.Certificate
            Set oCert = oMail.VerifySignature()
        
            If Err.Number <> 0 Then
                MsgBox Err.Description
                Exit Sub
            End If
            MsgBox "This email contains a valid digital signature."
            'you can add the certificate to your certificate storage like this
            'oCert.AddToStore CERT_SYSTEM_STORE_CURRENT_USER,"addressbook"
            'then you can use send the encrypted email back to this sender.        
    End If
End Sub


[VBScript] Const CRYPT_MACHINE_KEYSET = 32 Const CRYPT_USER_KEYSET = 4096 Const CERT_SYSTEM_STORE_CURRENT_USER = 65536 Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072 On Error Resume Next Dim oMail Set oMail = CreateObject("EAGetMailObj.Mail") oMail.LicenseCode = "TryIt" oMail.LoadFile "c:\test.eml", False If Err.Number <> 0 Then MsgBox Err.Description Exit Sub End If If (oMail.IsEncrypted) Then ' this email is encrypted, we decrypt it by user default certificate. ' you can also use specified certificate like this ' Dim oCert ' Set oCert = CreateObject("EAGetMailObj.Certificate") ' oCert.LoadFromFile "c:\test.pfx", "pfxpassword", CRYPT_USER_KEYSET ' Set oMail = oMail.Decrypt(oCert) Set oMail = oMail.Decrypt(Nothing) If Err.Number <> 0 Then MsgBox Err.Description Exit Sub End If End If If (oMail.IsSigned) Then 'this email is digital signed. Dim oCert Set oCert = oMail.VerifySignature() If Err.Number <> 0 Then MsgBox Err.Description Exit Sub End If MsgBox "This email contains a valid digital signature." 'you can add the certificate to your certificate storage like this 'oCert.AddToStore CERT_SYSTEM_STORE_CURRENT_USER,"addressbook" 'then you can use send the encrypted email back to this sender. End If
[Visual C++] #include "stdafx.h" #include <windows.h> #include "eagetmailobj.tlh" using namespace EAGetMailObjLib; vod VerifyAndDecryptEmail() { ::CoInitialize(NULL); try { IMailPtr oMail = NULL; oMail.CreateInstance(__uuidof(EAGetMailObjLib::Mail)); oMail->LicenseCode = _T("TryIt"); oMail->LoadFile(_T("d:\\test.eml"), VARIANT_FALSE); if(oMail->IsEncrypted == VARIANT_TRUE) { //this email is encrypted, we decrypt it by user default certificate. // you can also use specified certificate like this //ICertificatePtr oCert; //oCert.CreateInstance(__uuidof(EAGetMailObjLib::Certificate)); //oCert->LoadFromFile(_T("c:\\test.pfx"), _T("pfxpassword"), CRYPT_USER_KEYSET); //oMail = oMail->Decrypt(oCert); oMail = oMail->Decrypt(NULL); } if(oMail->IsSigned == VARIANT_TRUE) { ICertificatePtr oCert = oMail->VerifySignature(); MessageBox(NULL, _T("This email contains a valid digital signature."), NULL, MB_OK); //you can add the certificate to your certificate storage like this //oCert->AddToStore(CERT_SYSTEM_STORE_CURRENT_USER, _T("addressbook")); // then you can use send the encrypted email back to this sender. } } catch(_com_error &ep) { MessageBox(NULL, (TCHAR*)ep.Description(), NULL, MB_OK); } ::CoUninitialize(); }
[Delphi] const CRYPT_MACHINE_KEYSET = 32; CRYPT_USER_KEYSET = 4096; CERT_SYSTEM_STORE_CURRENT_USER = 65536; CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072; procedure VerifyAndDecryptEmail(); Var oMail: TMail; oCert: TCertificate; oSignerCert: ICertificate; begin oMail := TMail.Create(Application); oMail.LicenseCode := 'TryIt'; oMail.LoadFile('d:\test.eml', false); if oMail.IsEncrypted then try // this email is encrypted, decrypt it by default user certificate oMail.ConnectTo(oMail.Decrypt(nil)); // You can also use specified certificate like this // oCert := TCertificate.Create(Application); // oCert.LoadFromFile('c:\test.pfx', 'pfxpassword', CRYPT_USER_KEYSET); // oMail.Load(oMail.Decrypt(oCert.DefaultInterface).Content); except on ep: exception do ShowMessage('Decrypt Error: ' + ep.Message); end; if oMail.IsSigned then try // this email is digital signed, verify signature oSignerCert := oMail.VerifySignature(); ShowMessage('This email contains a valid digital signature.'); // You can add the certificate to your certificate storage like this // oSignerCert.AddToStore(CERT_SYSTEM_STORE_CURRENT_USER, // 'addressbook'); // Then you can use send the encrypted email back to this sender. except on ep: Exception do ShowMessage('Verify signature Error: ' + ep.Message); end; end;

Remarks

To learn more about email digital signature and encryption, please refer to Digital Signature and E-mail Encryption/Decryption section.

See Also

Mail.IsSigned Property
Mail.VerifySignature Method
Mail.IsEncrypted Property

Online Tutorials

Verify Digital Signature and Decrypt Email in VB6 - Tutorial
Verify Digital Signature and Decrypt Email in Delphi - Tutorial
Verify Digital Signature and Decrypt Email in VC++ - Tutorial