Creating a Mail client is quite simple. Simply set the Security property to the type of security/authentication desired, then simply connect to your Mail server, and handle all certificate-related issues in the CertificateReceived or CertificateRequested events.
The Simplest Secure Example: Disabling All Security Checks.
This example demonstrates the simplest possible secure example. In this example, the UseAuthentication property is set to false, "disabling" standard security checks. (NOTE: This does NOT disable security). This means that even if an invalid certificate is received from the server, it is accepted and SSL communication is established. This has obvious security shortcomings, which will be addressed later in the topic.
This example demonstrates security using the Smtp component. The Pop and Imap components work similarly.
| C# | Copy Code |
|---|---|
private void SecureTest() { // "Disable" security checks...just accept any certificate the server presents smtp1.UseAuthentication = false; // 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"); } | |
| Visual Basic | Copy Code |
|---|---|
Private Sub SecureTest() ' "Disable" security checks...just accept any certificate the server presents Smtp1.UseAuthentication = False ' 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 | |
A More Advanced Secure Client: Validating certificates.
The example above demonstrates communicating securely but is flawed, mainly because all certificates are automatically accepted. Typically, your secure client will need to perform some basic "filtering" on the certificates, checking for a valid name, a valid date, etc. To do this, set UseAuthentication to true and handle which certificates are accepted/rejected in the CertificateReceived event.
| C# | Copy Code |
|---|---|
private void SecureTest() { // "Disable" security checks...just accept any certificate the server presents smtp1.UseAuthentication = false; // 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"); } 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; } } } | |
| Visual Basic | Copy Code |
|---|---|
Private Sub SecureTest() ' "Disable" security checks...just accept any certificate the server presents Smtp1.UseAuthentication = False ' 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 | |