PowerTCP Mail for .NET
CertificateReceived Event
See Also  Example Send comments on this topic.
Dart.PowerTCP.Mail Namespace > Imap Class : CertificateReceived Event




This event is raised when a certificate has been received to be authenticated.

Syntax

Visual Basic (Declaration) 
<CategoryAttribute("Security")>
<DescriptionAttribute("Raised when a remote certificate has been received for verification.")>
Public Event CertificateReceived As CertificateReceivedEventHandler
Visual Basic (Usage)Copy Code
Dim instance As Imap
Dim handler As CertificateReceivedEventHandler
 
AddHandler instance.CertificateReceived, handler
C# 
[CategoryAttribute("Security")]
[DescriptionAttribute("Raised when a remote certificate has been received for verification.")]
public event CertificateReceivedEventHandler CertificateReceived
Managed Extensions for C++ 
[CategoryAttribute("Security")]
[DescriptionAttribute("Raised when a remote certificate has been received for verification.")]
public: __event CertificateReceivedEventHandler* CertificateReceived
C++/CLI 
[CategoryAttribute("Security")]
[DescriptionAttribute("Raised when a remote certificate has been received for verification.")]
public:
event CertificateReceivedEventHandler^ CertificateReceived

Event Data

The event handler receives an argument of type CertificateReceivedEventArgs containing data related to this event. The following CertificateReceivedEventArgs properties provide information specific to this event.

PropertyDescription
Accept Controls whether or not the certificate is accepted.
Certificate The certificate received for authentication.
RemoteEndPoint Returns the endpoint of the remote host.
TrustedRoot Returns whether or not the certificate is from a Trusted Root Authority.
ValidDate Returns whether or not the current time-date is within the certificate's life span.
ValidName Returns whether or not the name is valid.

Example

The following example demonstrates using IMAP over SSL.
Visual BasicCopy Code
' Be sure to import the namespace by putting "Imports Dart.PowerTCP.Mail"
' at the top of your class.

Private Sub ImapSSLTest()

   ' Use explicit SSL
   Imap1.Security = Dart.PowerTCP.Mail.Security.Explicit
   
   ' Login to the mail server
   Imap1.Login("MyImapServer", "username", "password")

   ' Send a NOOP command 
   Dim Response As ImapResponse = Imap1.Noop()

   ' Check the response
   If Response.Status = ImapStatus.Ok Then
      Debug.WriteLine("Good response from server")
   Else
      Debug.WriteLine("Response from server: " + Response.Status)
   End

   ' Logout
   Imap1.Logout()
End Sub

Private Sub Imap1_CertificateReceived(ByVal sender As Object, e As Dart.PowerTCP.Mail.CertificateReceivedEventArgs) Handles Imap1.CertificateReceived
   Dim msg As String = ""

   ' Check to see if the certificate is from a trusted root.
   If Not e.TrustedRoot Then
      msg += "This certificate is not from a trusted root" + vbCrLf
   End If

   ' Check to see if the certificate has a valid date.
   If Not e.ValidDate Then
      msg += "This certificate does not have a valid date" + vbCrLf
   End If

   ' Check to see if the certificate has a valid name.
   If Not e.ValidName Then
      msg += "This certificate does not have a valid name" + vbCrLf
   End If
   
   If msg <> "" Then
      msg += "Would you like to accept this certificate anyway?"
      If MessageBox.Show(msg, "Invalid Cert Received", MessageBoxButtons.YesNo) = DialogResult.Yes Then
         ' User wants to accept the invalid cert. Accept it.
         e.Accept = True
      End If
   End If
End Sub

Private Sub Imap1_CertificateRequested(ByVal sender As Object, e As EventArgs) Handles Imap1.CertificateRequested
   ' Create a new instance of CertificateListForm
   Dim certForm as new CertificateListForm(True, True)
   certForm.Text = "The Server Requested A Client Certificate"

   ' Show the CertificateListForm to the user and let them choose a certificate
   If certForm.ShowDialog() = DialogResult.OK Then
      ' Set the Certificate property to the certificate the user selected.
      Imap1.Certificate = certForm.SelectedCertificate
   Else
      MessageBox.Show("You must select a cert. SSL communication will fail.")
   End If
End Sub
C#Copy Code
// Be sure to import the namespace by putting "using Dart.PowerTCP.Mail;"
// at the top of your class.

private void ImapSSLTest()
{
   // Use explicit SSL
   imap1.Security = Dart.PowerTCP.Mail.Security.Explicit;
   
   // Login to the mail server
   imap1.Login(server, user, pass);
 
   // Send a NOOP command
   ImapResponse response = imap1.Noop();

   // Check the response
   if(response.Status == ImapStatus.Ok)
      Debug.WriteLine("Good response from server");
   else
      Debug.WriteLine("Response from server: " + response.Status);

   // Logout
   imap1.Logout();
}

private void imap1_CertificateReceived(object sender, Dart.PowerTCP.Mail.CertificateReceivedEventArgs e)
{
   string msg = "";

   // Check to see if the certificate is from a trusted root.
   if(!e.TrustedRoot)
      msg+= "This certificate is not from a trusted root\n";

   // Check to see if the certificate has a valid date.
   if(!e.ValidDate)
      msg+= "This certificate does not have a valid date\n";

   // Check to see if the certificate has a valid name.
   if(!e.ValidName)
      msg+= "This certificate does not have a valid name\n";

   if(msg != "")
   {
      msg += "Would you like to accept this certificate anyway?";
      if(MessageBox.Show(msg, "Invalid Cert Received", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
         // User wants to accept the invalid cert. Accept it.
         e.Accept = true;
      }
   }
}

private void imap1_CertificateRequested(object sender, System.EventArgs e)
{
   // Create a new instance of CertificateListForm
   CertificateListForm certForm = new CertificateListForm(true, true);
   certForm.Text = "The Server Requested A Client Certificate";

   // Show the CertificateListForm to the user and let them choose a certificate
   if(certForm.ShowDialog() == DialogResult.OK)
   {
      // Set the Certificate property to the certificate the user selected.
      imap1.Certificate = certForm.SelectedCertificate;
   }
   else
   {
      MessageBox.Show("You must select a cert. SSL communication will fail.");
   }
}

Remarks

The CertificateReceived event is raised when a certificate has been received from a secure server. A CertificateReceivedEventArgs object is passed into the event handler, containing information required to determine the validity of the certificate. If the certificate is determined to be invalid (if any of the properties CertificateReceivedEventArgs.ValidDate, CertificateReceivedEventArgs.ValidName, or CertificateReceivedEventArgs.TrustedRoot are false) the certificate is rejected. CertificateReceivedEventArgs.Accept is false to signify this. Set CertificateReceivedEventArgs.Accept to true to override this and accept the certificate.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.