0
votes

I'm using a test email server to test out my code in SSL environment. I used the following command to create a self signed root and a signed certificate.

enter image description here

enter image description here

I wrote a simple test program using SmtpClient to send test email and the code is as follow:

                System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(tFrom.Text, tTo.Text);

                mm.Subject = tSubject.Text;
                mm.Body = tBody.Text;

                SmtpClient client = new SmtpClient(tSmtp.Text, Convert.ToInt32(tPort.Text));
                client.UseDefaultCredentials = false;

                if (!String.IsNullOrEmpty(tPass.Text) && !String.IsNullOrEmpty(tUser.Text))
                {
                    client.EnableSsl = true;
                    client.Credentials = new NetworkCredential(tUser.Text, tPass.Text);
                }
                client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
                client.SendAsync(mm, null);

On my server (Smtp4Dev) I have it set to accept TLS connection

enter image description here

On my client machine I have the following:

enter image description here

Yet, whenever I send an email I keep getting the error as follow on the server log:

 220 Ready to start TLS
 Session ended abnormally.
 System.NotSupportedException: The server mode SSL must use a certificate with the associated private key.

If I tried this without SSL then I can receive my email fine. I tried to add the certificate under Personal and Trusted Root Certification but same error still occur. I am very new to SSL so I hope you can point me in the right direction.

1

1 Answers

0
votes

Look at implementing the ServerCertificateValidationCallback delegate. Try something like this before you use SmtpClient:

System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };

Returning true and thus accepting all certificates is only a good idea in a dev environment, but you can expand on the delegate implementation to check for other properties and not just blindly return true for everything.