Add MailKit
through NuGet Package Manager, then use the following code:
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using MimeKit.Text;
using (SmtpClient smtpClient = new SmtpClient())
{
using (MimeMessage message = new MimeMessage())
{
try
{
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(@"<sender email>"));
email.To.Add(MailboxAddress.Parse("<recipient email>"));
email.Subject = "<subject here>";
email.Body = new TextPart(TextFormat.Html) { Text = "<h1>your message body</h1>" };
var smtp = new SmtpClient();
await smtp.ConnectAsync("smtp.<provider>.com", 587, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(@"<sender email>", @"<password>");
await smtp.SendAsync(email);
await smtp.DisconnectAsync(true);
}
catch (Exception ex)
{
//Error, could not send the message
// Do something
}
}
}
The method smtp.AuthenticateAsync(user, password)
is where you pass a username/email and a password/key
Replace the values in angle brackets with the suitable values from your side