【问题标题】:The SMTP server has unexpectedly disconnected in mailkitSMTP 服务器在 mailkit 中意外断开连接
【发布时间】:2017-11-02 11:07:36
【问题描述】:

我正在使用 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 服务器意外断开

请提出一些可能的解决方案

【问题讨论】:

  • 您正在使用 SSL 端口 (465) 但您有 useSSL = false。尝试将其更改为 true。如果仍然失败,请保持 true 并移动到端口 587 (TLS)
  • 不要设置LocalDomain。无论如何,它不应该是一个 URL,它应该是一个完全限定的域名。

标签: c# email asp.net-core smtp mailkit


【解决方案1】:

这就是 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);
}

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 2022-11-30
    • 2015-02-21
    • 1970-01-01
    相关资源
    最近更新 更多