我正在使用mailkit在asp.net core中发送邮件这是我的代码
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);
}
在行中
await client.ConnectAsync("smtp.gmail.com", 465, false)
调试器播种以下错误 SMTP服务器意外断开
这是2021年对我有用的
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);
}
试图发送消息时在'smtpclient'上收到此错误的原因之一是每当某些收件人'MailboxAddress'(to,cc,bcc(设置为一个空字符串。请参阅详细信息https://github.com/jstedfast/mailkit/issues/866