Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
C# Send Email With MailKit
namespace ConsoleApp
class Program
static void Main(string[] args)
var message = new MimeMessage();
var bodyBuilder = new BodyBuilder();
// from
message.From.Add(new MailboxAddress("from_name", ""));
// to
message.To.Add(new MailboxAddress("to_name", ""));
// reply to
message.ReplyTo.Add(new MailboxAddress("reply_name", ""));
message.Subject = "subject";
bodyBuilder.HtmlBody = "html body";
message.Body = bodyBuilder.ToMessageBody();
var client = new SmtpClient();
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("MAIL_SERVER", 465, SecureSocketOptions.SslOnConnect);
client.Authenticate("USERNAME", "PASSWORD");
Copy link

kunal-tawde commented Apr 25, 2019

A very simple and a crisp example.
Thanks a ton Wattanar.

Copy link

vmakwana commented Jun 3, 2019

Using false for useSsl in client.connect, still expected SSL/TLS certificate, this is an unexpected behavior, how can I use non-SSL???
`An error occurred while attempting to establish an SSL or TLS connection.

The SSL certificate presented by the server is not trusted by the system for one or more of the following reasons:

  1. The server is using a self-signed certificate which cannot be verified.
  2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
  3. The certificate presented by the server is expired or invalid.`

Copy link

tleaha commented Jul 18, 2019

@vmakwana you can use non-SSL by set only host parameter like this client.Connect("");

Copy link

wattanar commented Aug 20, 2019

Update 2019 !! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment