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);
}
}
}
@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