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




This event is raised when a certificate has been requested.

Syntax

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

Example

The following example demonstrates using SMTP 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 SmtpSSLTest()

   ' Use explicit SSL
   Smtp1.Security = Dart.PowerTCP.Mail.Security.Explicit

   ' Send a message, it will initiate an SSL connection
   Smtp1.Server = "MyMailServer"
   Smtp1.Send("you@dart.com", "me@dart.com", "SSL Test", "Hello")

   MessageBox.Show("Message Sent")
End Sub

Private Sub Smtp1_CertificateReceived(ByVal sender As Object, e As Dart.PowerTCP.Mail.CertificateReceivedEventArgs) Handles Smtp1.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 Smtp1_CertificateRequested(ByVal sender As Object, e As EventArgs) Handles Smtp1.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.
      Smtp1.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 SmtpSSLTest()
{
   // Use explicit SSL
   smtp1.Security = Dart.PowerTCP.Mail.Security.Explicit;

   // Send a message, it will initiate an SSL connection
   smtp1.Server = "MySmtpServer";
   smtp1.Send("you@dart.com", "me@dart.com", "SSL Test", "Hello");

   MessageBox.Show("Message Sent");
}

private void smtp1_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 smtp1_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.
      smtp1.Certificate = certForm.SelectedCertificate;
   }
   else
   {
      MessageBox.Show("You must select a cert. SSL communication will fail.");
   }
}

Remarks

The CertificateRequested event is raised when a secure server requests a certificate from the client for client authentication. The SslStream.Certificate property of the current Tcp component (or, if using a different component, the Tcp component used for the underlying connection) must be set to a valid certificate. The easiest way to do this is to cast the sender parameter to an SslStream, then set the SslStream.Certificate property to a valid certificate.

Many objects provide a high-level Object.Certificate property, which represents the certificate used by the object. If the object you are using has such a high-level property it is recommended to use this property instead of the implementation described above.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

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