RecipientsCerts Property


This property specifies the certificates of recipients to encrypt current email.

Data Type: Object

Remarks

EASendMail uses built-in Certificate and CertificateCollection object to sign and encrypt email.

Encrypting email doesn't require sender certificate but the certificate with public key for every recipient. For example, from@adminsystem.com sends an email to rcpt@adminsystem.com with digital signature. The digital signature contains the public key certificate for from@adminsystem.com, then rcpt@adminsystem.com can send an encrypted email with this certificate back to from@adminsystem.com. Only from@adminsystem can read this email, because this email MUST be decrypted by private key of from@adminsystem.com. Therefore, you MUST receive an digital signed email from other people (Most email clients such as outlook, outlook express will add the certificate to the Other People Storage automatically once an digital signed email is received) before you can send encrypted email to this people. To encrypt email with EASendMail, the certificate for recipient should be loaded to RecipientsCerts property.

By default, Personal digital certificate is stored at CERT_SYSTEM_STORE_CURRENT_USER "my". Encryption digital certificates are stored at CERT_SYSTEM_STORE_CURRENT_USER "Address Book". If you want to search certificate in Windows Active Directory, please use CERT_STORE_PROV_LDAP_STORE and input LDAP query statement in storeName parameter.

EASendMail always uses the maximum key length available with the indicated encryption algorithm.

You must assign each certificate for each recipient.

Example

[Visual Basic, Visual C++, Delphi] The following example demonstrates how to load certificate to encrypt email with EASendMail SMTP Component. To get the full samples of EASendMail, please refer to Samples section.

[VB6, VBA - Encrypt Email]  
Const CRYPT_MACHINE_KEYSET = 32
Const CRYPT_USER_KEYSET = 4096
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072
Const CERT_STORE_PROV_LDAP_STORE = 16
            
Private Sub SendEmail()
  Dim oSmtp As EASendMailObjLib.Mail
  Set oSmtp = New EASendMailObjLib.Mail
  'The license code for EASendMail ActiveX Object, 
  'for evaluation usage, please use "TryIt" as the license code.
  oSmtp.LicenseCode = "TryIt"
  
  Dim Sender As String
  Sender = "test@emailarchitect.net"
  
  oSmtp.ServerAddr = "mail.emailarchitect.net"
  oSmtp.FromAddr = Sender
  
  'clear certificate
  oSmtp.SignerCert.Unload
     
  'find certificate in current user certificate store
  If Not oSmtp.SignerCert.FindSubject("test@emailarchitect.net", CERT_SYSTEM_STORE_CURRENT_USER, "my") Then
     MsgBox oSmtp.SignerCert.GetLastError() 
  End If
  
  'you can also load signer cerificate from a cer file
  'If Not oSmtp.SignerCert.LoadPFXFromFile("c:\cert\my.pfx", "password of this certificate", _
       CRYPT_USER_KEYSET ) Then
     'MsgBox oSmtp.SignerCert.GetLastError() 
  'End If
     
     
  If Not oSmtp.SignerCert.HasPrivateKey Then
     MsgBox "Signer certificate has not private key, this certificate can not be used to sign email!"
  End If
  
  Dim arRecipients(3) As String
  arRecipients(0) = "support@emailarchitect.net"
  arRecipients(1) = "support1@emailarchitect.net"
  arRecipients(2) = "support2@emailarchitect.net"
  
  Dim i, nCount As Long
  nCount = 3
  
  'clear certificate
  oSmtp.RecipientsCerts.Clear
  For i = 0 To nCount - 1
    Dim oEncryptCert As New EASendMailObjLib.Certificate
     'find certificate by recipient email address
    If Not oEncryptCert.FindSubject(arRecipients(i), CERT_SYSTEM_STORE_CURRENT_USER, "AddressBook") Then
      If Not oEncryptCert.FindSubject(arRecipients(i), CERT_SYSTEM_STORE_CURRENT_USER, "my") Then
        MsgBox oEncryptCert.GetLastError() 
        Exit Sub 'no certificate foud, exit subroutine
      End If
    End If
 
    'you can also load encrypting cerificate from a pfx file
    'If Not oEncryptCert.LoadCertFromFile("c:\cert\my.cer" ) Then
       'MsgBox oEncryptCert.GetLastError() 
       'Exit Sub 'no certificate foud, exit subroutine
    'End If
    
    'add recipient certificate
    oSmtp.RecipientsCerts.Add oEncryptCert    
    oSmtp.AddRecipient arRecipients(i), arRecipients(i), 0
  Next
  
  oSmtp.Subject = "Test"
  oSmtp.BodyText = "Hello, this is a test...."
 
  If oSmtp.SendMail() = 0 Then
    MsgBox "Message delivered!"
  Else
    MsgBox oSmtp.GetLastErrDescription()
  End If

End Sub

[VC++- Encrypt Email]
#include "stdafx.h" 

#include "easendmailobj.tlh" 
using namespace EASendMailObjLib; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    ::CoInitialize( NULL ); 

    IMailPtr oSmtp = NULL; 
    oSmtp.CreateInstance( __uuidof(EASendMailObjLib::Mail)); 
    oSmtp->LicenseCode = _T("TryIt"); 

    // Set your sender email address
    oSmtp->FromAddr = _T("test@emailarchitect.net"); 

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 ); 

    // Set email subject
    oSmtp->Subject = _T("Encrypted email from Visual C++ (S/MIME)"); 

    // Set email body
    oSmtp->BodyText = _T("this is a test encrypted email sent from Visual C++"); 

    // Your SMTP server address
    oSmtp->ServerAddr = _T("smtp.emailarchitect.net"); 

    // User and password for ESMTP authentication, if your server doesn't
    // require User authentication, please remove the following codes.
    oSmtp->UserName = _T("test@emailarchitect.net"); 
    oSmtp->Password = _T("testpassword"); 

    // If your smtp server requires SSL connection, please add this line
    // oSmtp->SSL_init();

    //add signer digital signature
    if( oSmtp->SignerCert->FindSubject(_T("test@emailarchitect.net"), 
        CERT_SYSTEM_STORE_CURRENT_USER , _T("my")) == VARIANT_FALSE ) 
    { 
        _tprintf(_T("Error with signer certificate; %s\r\n"), 
            (const TCHAR*)oSmtp->SignerCert->GetLastError()); 
        return 0; 
    } 

    if( oSmtp->SignerCert->HasPrivateKey == VARIANT_FALSE ) 
    { 
        _tprintf(_T("certificate does not have a private key, it can not sign email.\r\n" )); 
        return 0; 
    } 

    // Find the encrypting certificate for every recipients
    ICertificatePtr oCert = NULL; 
    oCert.CreateInstance(__uuidof(EASendMailObjLib::Certificate)); 
    if( oCert->FindSubject(_T("support@emailarchitect.net"), 
            CERT_SYSTEM_STORE_CURRENT_USER, _T("AddressBook")) == VARIANT_FALSE ) 
    { 
        if(oCert->FindSubject(_T("support@emailarchitect.net"), 
                CERT_SYSTEM_STORE_CURRENT_USER, _T("my")) == VARIANT_FALSE ) 
        { 
            _tprintf( _T("Encrypting certificate not found; %s\r\n"), 
                    (const TCHAR*)oCert->GetLastError()); 
            oCert.Release(); 
            return 0; 
        } 
    } 

    // Add encrypting certificate
    oSmtp->RecipientsCerts->Add( oCert ); 
    oCert.Release(); 

    _tprintf(_T("Start to send email ...\r\n" )); 

    if( oSmtp->SendMail() == 0 ) 
    { 
        _tprintf( _T("email was sent successfully!\r\n")); 
    } 
    else 
    { 
        _tprintf( _T("failed to send email with the following error: %s\r\n"), 
            (const TCHAR*)oSmtp->GetLastErrDescription()); 
    } 

    if( oSmtp != NULL ) 
        oSmtp.Release(); 

    return 0; 
} 

[Delphi - Encrypt Email]
Unit Unit1; 

Interface 

Uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail Unit 

Type 
  TForm1 = Class(TForm) 
    Button1: TButton; 
    Procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  End; 

Const 
  CRYPT_MACHINE_KEYSET = 32; 
  CRYPT_USER_KEYSET = 4096; 
  CERT_SYSTEM_STORE_CURRENT_USER = 65536; 
  CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072; 
  CERT_STORE_PROV_LDAP_STORE = 16;

Var 
  Form1: TForm1; 

Implementation 

{$R *.dfm} 

Procedure TForm1.Button1Click(Sender: TObject); 
Var 
  oSmtp : TMail; 
  oEncryptCert : TCertificate; 
Begin 
  oSmtp := TMail.Create(Application); 
  oSmtp.LicenseCode := 'TryIt'; 

  // Set your sender email address
  oSmtp.FromAddr := 'test@emailarchitect.net'; 

  // Add recipient email address
  oSmtp.AddRecipientEx( 'support@emailarchitect.net', 0); 

  // Set email subject
  oSmtp.Subject := 'test encrypted email from Delphi with digital signature'; 

  // Set body text
  oSmtp.BodyText := 'this is a test encrypted email sent from Delphi with digital signature'; 

  // Add digital signature
  If Not oSmtp.SignerCert.FindSubject('test@emailarchitect.net', 
    CERT_SYSTEM_STORE_CURRENT_USER, 'my' ) Then 
    Begin 
      ShowMessage( oSmtp.SignerCert.GetLastError()); 
      exit; 
    End; 

  If Not oSmtp.SignerCert.HasCertificate Then 
    Begin 
      ShowMessage( 'Signer certificate has no private key, ' + 
      'this certificate can not be used to sign email'); 
    End; 

  // Find the encrypting certificate for every recipients
  oEncryptCert := TCertificate.Create(Application); 
  If Not oEncryptCert.FindSubject('support@emailarchitect.net', 
    CERT_SYSTEM_STORE_CURRENT_USER, 'AddressBook' ) Then 
    If Not oEncryptCert.FindSubject('support@emailarchitect.net', 
    CERT_SYSTEM_STORE_CURRENT_USER, 'my' ) Then 
    Begin 
      ShowMessage( oEncryptCert.GetLastError()); 
      exit; 
    End; 

  // Add encrypting certificate
  oSmtp.RecipientsCerts.Add(oEncryptCert.DefaultInterface); 

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net'; 

  // User and password for ESMTP authentication, if your server doesn't require
  // user authentication, please remove the following codes
  oSmtp.UserName := 'test@emailarchitect.net'; 
  oSmtp.Password := 'testpassword'; 

  // If your SMTP server requires SSL connection, please add this line
  // oSmtp.SSL_init();

  ShowMessage( 'start to send email ...' ); 

  If oSmtp.SendMail() = 0 Then 
    ShowMessage( 'email was sent successfully!' ) 
  Else 
    ShowMessage( 'failed to send email with the following error: ' 
    + oSmtp.GetLastErrDescription()); 

End; 

End. 

[VB6, VBA - Find Certificate in Active Directory by LDAP]
Const CRYPT_MACHINE_KEYSET = 32 
Const CRYPT_USER_KEYSET = 4096 
Const CERT_SYSTEM_STORE_CURRENT_USER = 65536 
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = 131072 
Const CERT_STORE_PROV_LDAP_STORE = 16

Private Sub btnEncryptEmail_Click() 

    Dim oSmtp As New EASendMailObjLib.Mail 
    oSmtp.LicenseCode = "TryIt" 

    ' Set your sender email address
    oSmtp.FromAddr = "test@emailarchitect.net" 

    ' Add recipient email address
    oSmtp.AddRecipientEx "Support <support@emailarchitect.net>", 0 

    ' Set email subject
    oSmtp.Subject = "test encrypted email from VB 6.0 project" 

    ' Set email body
    oSmtp.BodyText = "this is a test encrypted email sent from VB 6.0 project" 

    ' Your SMTP server address
    oSmtp.ServerAddr = "smtp.emailarchitect.net" 

    ' User and password for ESMTP authentication, if your server doesn't require
    ' User authentication, please remove the following codes.
    oSmtp.UserName = "test@emailarchitect.net" 
    oSmtp.Password = "testpassword" 

    ' If your smtp server requires SSL connection, please add this line
    ' oSmtp.SSL_init

    ' Add digital signature
    If Not oSmtp.SignerCert.FindSubject("test@emailarchitect.net", _ 
        CERT_SYSTEM_STORE_CURRENT_USER, "my") Then 
        MsgBox oSmtp.SignerCert.GetLastError() 
        Exit Sub 
    End If 

    If Not oSmtp.SignerCert.HasPrivateKey Then 
        MsgBox "Signer certificate has not private key, " & _ 
             " this certificate can not be used to sign email!" 
        Exit Sub 
    End If 

    ' Please change the ldap path as your environment.
    Dim oEncryptCert As New EASendMailObjLib.Certificate 
    If Not oEncryptCert.FindSubject("support@emailarchitect.net", _ 
        CERT_STORE_PROV_LDAP_STORE, "ldap:///CN=Support,CN=USERS,DC=my,DC=server?userCertificate") Then 
            MsgBox oEncryptCert.GetLastError() 
            Exit Sub 
    End If 

    ' Add encrypting certificate
    oSmtp.RecipientsCerts.Add oEncryptCert 

    MsgBox "start to send email ..." 

    If oSmtp.SendMail() = 0 Then 
        MsgBox "email was sent successfully!" 
    Else 
        MsgBox "failed to send email with the following error:" & oSmtp.GetLastErrDescription() 
    End If 

End Sub 

Online Examples

Sign Email - VB 6.0
Encrypt Email - VB 6.0
Sign Email - Visual C++
Encrypt Email - Visual C++
Sign Email - Delphi
Encrypt Email - Delphi

See Also

EncryptionAlgorithm Property
SignerCert Property
Digital Signature and E-mail Encryption