Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wattanar
Last active January 3, 2023 13:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wattanar/e8cee974456fd80d9ce649d951f36608 to your computer and use it in GitHub Desktop.
Save wattanar/e8cee974456fd80d9ce649d951f36608 to your computer and use it in GitHub Desktop.
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", "from_email@example.com"));
// to
message.To.Add(new MailboxAddress("to_name", "to_email@example.com"));
// reply to
message.ReplyTo.Add(new MailboxAddress("reply_name", "reply_email@example.com"));
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");
client.Send(message);
client.Disconnect(true);
}
}
}
@kunal-tawde
Copy link

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

@vmakwana
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.`

@tleaha
Copy link

tleaha commented Jul 18, 2019

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

@wattanar
Copy link
Author

Update 2019 !! :)

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