Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using mailkit to send a mail in ASP.NET Core Here is my code

var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("Gorrolo", "gorollobikes@gmail.com"));
emailMessage.To.Add(new MailboxAddress("", email));
emailMessage.Subject = subject;
emailMessage.Body = new TextPart("HTML") { Text = message };
using (var client = new SmtpClient())
    client.LocalDomain = "http://localhost:54850";
    await client.ConnectAsync("smtp.gmail.com", 465, false);
    client.Authenticate("Myemail", "Mypassword");
    client.Send(emailMessage);
    //await client.emailMessage(emailMessage).ConfigureAwait(false);
    //await client.DisconnectAsync(true).ConfigureAwait(false);

in the line

await client.ConnectAsync("smtp.gmail.com", 465, false)

Debugger sowing the following error The SMTP server has unexpectedly disconnected

You are using the SSL port (465) but you have useSSL = false. Try changing that to true. If it still fails, keep the true and move to port 587 (TLS) – Mad Myche Jun 1, 2017 at 11:19 Don't set LocalDomain. It shouldn't be a URL anyway, it's supposed to be a fully qualified domain name. – jstedfast Jun 1, 2017 at 20:27
void SendGmail(MimeMessage mm)
    mm.From.Add(new MailboxAddress("The Email Service", "user@email.com"));
    using SmtpClient client = new(new ProtocolLogger("smtp.log"));
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
    client.Authenticate("user@email.com", "My$uperSecr3tPa55w0rd"); // app password
    client.Send(mm);
    client.Disconnect(true);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.