Certificate.Load Method


Loads the certificate from .pfx or .cer file.

[Visual Basic]
Public Sub Load( _
    certFile As String _
)

Public Sub Load( _
    pfxFile As String, _
    password As String, _
    keyLocation As Certificate.CertificateKeyLocation _
)

Public Sub Load( _
    encodedCertificate() As Byte _
)
[C#]
public void Load(
    string certFile
);

public void Load(
     string pfxFile,
     string password,
     Certificate.CertificateKeyLocation keyLocation
);

public void Load(
    byte[] encodedCertificate
);
[C++]
public: void Load(
    String^ certFile
);

public: void Load(
     String^ pfxFile,
     String^ password,
     Certificate.CertificateKeyLocation keyLocation
);

public: void Load(
    unsigned char encodedCertificate __gc[],
);
[JScript]
public function Load( 
    certFile : String
);

public function Load( 
    pfxFile : String,
    password : String,
    keyLocation : Certificate.CertificateKeyLocation
);

public function Load( 
    encodedCertificate : Byte[]
);

Parameters

certFile
A full name of *.cer file.
encodedCertificate
The encoded data of certificate.
pfxFile
A full name of *.pfx file.
passowrd
The password of the *.pfx file.
keyLocation
The private keys's storage location, it can be Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET or Certificate.CertificateKeyLocation.CRYPT_MACHINE_KEYSET

Remarks

Load( string certFile ) method is unable to load the certificate for digital signature. To learn more about email digital signature and encryption, please refer to Digital Signature and E-mail Encryption section.
By default, Personal digital certificate is stored at Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER "my". Encryption digital certificates are stored at Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER "Address Book". If you want to search certificate in Windows Active Directory, please use Certificate.CertificateStoreLocation.CERT_STORE_PROV_LDAP_STORE and input LDAP query statement in storeName parameter.
To learn more about email digital signature and encryption, please refer to Digital Signature and E-mail Encryption section.

Example

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

[VB - Sign Email with Certificate]
Dim oMail As SmtpMail = New SmtpMail("TryIt")
oMail.From = New MailAddress("test@adminsystem.com")

Try
    'Find certificate by email adddress in My Personal Store. 
    'The certificate can be imported by *.pfx file like this: 
    'oMail.From.Certificate.Load("c:\test.pfx", "pfxpassword", Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET) 
    'Once the certificate is loaded to From, the email content will be signed automatically
    oMail.From.Certificate.FindSubject(oMail.From.Address, _
        Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, _
        "My")
Catch exp As Exception
    MessageBox.Show("No sign certificate found for <" + oMail.From.Address + ">:" + exp.Message)
End Try

[C# - Sign Email with Certificate]
SmtpMail oMail = new SmtpMail("TryIt");
oMail.From = "test@adminsystem.com";

try
{
//Find certificate by email adddress in My Personal Store. 
    //The certificate can be also imported by *.pfx file like this: 
    //oMail.From.Certificate.Load("c:\\test.pfx", "pfxpassword", Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET); 
    //Once the certificate is loaded to From, the email content will be signed automatically
    
    oMail.From.Certificate.FindSubject( oMail.From.Address,
        Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER,
        "My" );
}
catch( Exception exp )
{
    MessageBox.Show( "No sign certificate found for <" + oMail.From.Address + ">:" + exp.Message );
}


[VB - Encrypt Email]
Dim oMail As SmtpMail = New SmtpMail("TryIt")
oMail.From = New MailAddress("test@adminsystem.com")
oMail.To = New AddressCollection( "encrypt1@adminsystem.com, encrypt2@adminsystem.com" )

Dim count As Integer = oMail.To.Count
For i As Integer = 0 To count - 1
    Dim oAddress As MailAddress = oMail.To(i)
    Try
//Find certificate by email adddress in My Other Peoples Store.
    //The certificate can be also imported by *.cer file like this: 
    //oMail.From.Certificate.Load("c:\encrypt1.cer")
    //Once the certificate is loaded to MailAddress, the email content will be encrypted
    automatically
        oAddress.Certificate.FindSubject(oAddress.Address, _
            Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, _
            "AddressBook")
    Catch ep As Exception
        Try

            oAddress.Certificate.FindSubject(oAddress.Address, _
                Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, _
                "My")
        Catch exp As Exception
            MessageBox.Show("No encryption certificate found for <" + oAddress.Address + ">:" + exp.Message)

        End Try
    End Try
Next

[C# - Encrypt Email]
SmtpMail oMail = new SmtpMail("TryIt");
oMail.From = "test@adminsystem.com";
oMail.To = "encrypt1@adminsystem.com, encrypt2@adminsystem.com";

int count = oMail.To.Count;
for( int i = 0; i < count; i++ )
{
    MailAddress oAddress = oMail.To[i] as MailAddress;
    try
    {
//Find certificate by email adddress in My Other Peoples Store.
    //The certificate can be also imported by *.cer file like this: 
    //oMail.From.Certificate.Load("c:\\encrypt1.cer");
    //Once the certificate is loaded to MailAddress, the email content will be encrypted
    automatically
    
        oAddress.Certificate.FindSubject( oAddress.Address,
            Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER,
            "AddressBook" );
    }
    catch( Exception ep )
    {
        try
        {
            oAddress.Certificate.FindSubject( oAddress.Address,
                Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER,
                "My" );
        }
        catch( Exception exp )
        {
            MessageBox.Show( "No encryption certificate found for <" + oAddress.Address + ">:" + exp.Message );
        }
    }
}


[VB - Find Certificate in Active Directory by LDAP]
Dim oMail As SmtpMail = New SmtpMail("TryIt")
oMail.From = New MailAddress("test@adminsystem.com")
oMail.To = New AddressCollection( "Encryptor <encrypt@adminsystem.com>" )

Dim count As Integer = oMail.To.Count
For i As Integer = 0 To count - 1
    Dim oAddress As MailAddress = oMail.To(i)
    Try
        ' Please change the ldap path as your environment.
        oAddress.Certificate.FindSubject(oAddress.Address, _
            Certificate.CertificateStoreLocation.CERT_STORE_PROV_LDAP_STORE, _
            String.Format("ldap:///CN={0},CN=USERS,DC=my,DC=server?userCertificate", oAddress.Name))
    Catch exp As Exception
        MessageBox.Show("No encryption certificate found for <" + oAddress.Address + ">:" + exp.Message)

    End Try
Next

[C# - Find Certificate in Active Directory by LDAP]
SmtpMail oMail = new SmtpMail("TryIt");
oMail.From = "test@adminsystem.com";
oMail.To = ""Encryptor <encrypt@adminsystem.com>"";

int count = oMail.To.Count;
for( int i = 0; i < count; i++ )
{
    MailAddress oAddress = oMail.To[i] as MailAddress;

    try
    {
        // Please change the ldap path as your environment.
        oAddress.Certificate.FindSubject( oAddress.Address,
            Certificate.CertificateStoreLocation.CERT_STORE_PROV_LDAP_STORE,
            String.Format("ldap:///CN={0},CN=USERS,DC=my,DC=server?userCertificate", oAddress.Name));
    }
    catch( Exception exp )
    {
        MessageBox.Show( "No encryption certificate found for <" + oAddress.Address + ">:" + exp.Message );
    }
}

Online Examples

Sign Email - VB
Encrypt Email - VB
Sign Email - C#
Encrypt Email - C#
Sign Email - C++/CLI
Encrypt Email - C++/CLI