Skip to content

Instantly share code, notes, and snippets.

Embed
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", "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

kunal-tawde commented Apr 25, 2019

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

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