Reputation: 1020
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", "[email protected]"));
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
Upvotes: 3
Views: 12249
Reputation: 1
One of the reasons that this error is received on the 'SmtpClient' while trying to send a message is whenever some of the recipient 'MailboxAddress' (To, Cc, Bcc) is set to an empty string. See details https://github.com/jstedfast/MailKit/issues/866
Upvotes: 0
Reputation: 999
This is what works for me in 2021
void SendGmail(MimeMessage mm)
{
mm.From.Add(new MailboxAddress("The Email Service", "[email protected]"));
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("[email protected]", "My$uperSecr3tPa55w0rd"); // app password
client.Send(mm);
client.Disconnect(true);
}
Upvotes: 1