Skip to content

Instantly share code, notes, and snippets.

@augustoproiete
Created December 6, 2019 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augustoproiete/86015cf64043fd0ee43b83dc38353c2d to your computer and use it in GitHub Desktop.
Save augustoproiete/86015cf64043fd0ee43b83dc38353c2d to your computer and use it in GitHub Desktop.
Send e-mail validating self-signed SSL certificate
// Copyright 2019 Caio Proiete & Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.using System;
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace ValidateSelfSignedSslCertificate
{
class Program
{
static void Main(string[] args)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
using (var smtpClient = new SmtpClient("mail.caioproiete.net", 25))
{
var msg = new MailMessage(
from: "do-not-spam-me@caioproiete.net",
to: "do-not-spam-me-either@caioproiete.net",
subject: "Test",
body: "This is a test :)");
smtpClient.EnableSsl = true;
smtpClient.Send(msg);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ToString());
}
}
private static bool CertificateValidationCallBack(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
// Uncomment the below to debug errors (better yet, you should be logging these errors):
//Console.WriteLine("Subject: " + certificate.Subject);
//Console.WriteLine("Issuer: " + certificate.Issuer);
//Console.WriteLine("---");
//
//if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) != 0 && chain != null)
//{
// foreach (var status in chain.ChainStatus)
// {
// Console.WriteLine("Subject: " + certificate.Subject);
// Console.WriteLine("Issuer: " + certificate.Issuer);
// Console.WriteLine("Status: " + status.Status);
// Console.WriteLine("StatusInformation: " + status.StatusInformation);
// Console.WriteLine("---");
// }
//}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment