【问题标题】:MailKit MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted errorMailKit MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted 错误
【发布时间】:2021-10-03 04:14:33
【问题描述】:

我遇到了与 MailKit 相关的问题。在开发环境中,它可以正常工作并正确发送电子邮件。我有一个托管在我的域上的系统的 beta 环境(电子邮件服务器所在的位置 - kumminui.specodit.pl) 我已经在 kumminui.pl 创建了生产环境(因此 .specodit 部分已被删除)。现在电子邮件无法正常工作。

错误 1(端口 587 和主机名等于服务器名称和 STARTTLS):

    fail: API.Program[0]
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:       An error occurred during migration
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:       MailKit.Net.Smtp.SmtpCommandException: 146.59.19.44 is _my_ address
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted(MimeMessage message, MailboxAddress mailbox, SmtpResponse response)
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at MailKit.Net.Smtp.SmtpClient.ProcessMailFromResponse(MimeMessage message, MailboxAddress mailbox, SmtpResponse response)
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at MailKit.Net.Smtp.SmtpClient.FlushCommandQueueAsync(MimeMessage message, MailboxAddress sender, IList`1 recipients, Boolean doAsync, Cancell
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at MailKit.Net.Smtp.SmtpClient.SendAsync(FormatOptions options, MimeMessage message, MailboxAddress sender, IList`1 recipients, Boolean doAsyn
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at StableManagement.Services.MailService.SendMessage(Notification notification) in /home/admin/web/kumminui.pl/App/StableManagement/Services/M
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at StableManagement.Data.ScheduleTask.ScheduledItemChangeState(DateTime scheduleFor) in /home/admin/web/kumminui.pl/App/StableManagement/Data/
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at StableManagement.Data.ScheduleTask.RecoverScheduledTasks() in /home/admin/web/kumminui.pl/App/StableManagement/Data/ScheduleTask.cs:line 48
Jul 27 10:08:08 specodit.pl dotnet-example[3445]:          at API.Program.Main(String[] args) in /home/admin/web/kumminui.pl/App/StableManagement/Program.cs:line 33

我使用 .NET Core 和 MailKit 发送电子邮件:

    private async Task<MimeMessage> PrepareMessage(AppUser user, string subject, BodyBuilder body)
    {
        var emailSMTPUserName = (await _settingsRepository.Get("emailSMTPUserName"))?.Value ?? "admin@kumminui.pl";
        MimeMessage message = new MimeMessage();

        MailboxAddress from = new MailboxAddress("Kumminui - Administracja", emailSMTPUserName);
        message.From.Add(from);

        MailboxAddress to = new MailboxAddress(user.FirstName + " " + user.LastName, user.Email);
        message.To.Add(to);

        message.Subject = subject;
        var path = "./Data/htmls/emails/assets/logo.png";
        var image = body.LinkedResources.Add(path);
        image.ContentId = MimeUtils.GenerateMessageId();
        image.ContentDisposition = new ContentDisposition() { Disposition = ContentDisposition.Inline };
        body.HtmlBody = body.HtmlBody.Replace("./assets/logo.png", string.Format("cid:{0}", image.ContentId));

        message.Body = body.ToMessageBody();

        return message;
    }

然后发送:

    public async Task SendMessage(Notification notification)
    {
        MimeMessage message;
        using (var memory = new MemoryStream(notification.Message, false))
        {
            message = MimeMessage.Load(memory);
        }

        var emailSMTPHost = (await _settingsRepository.Get("emailSMTPHost"))?.Value;
        var emailSMTPPort = (await _settingsRepository.Get("emailSMTPPort"))?.Value;
        var emailSMTPUserName = (await _settingsRepository.Get("emailSMTPUserName"))?.Value;
        var emailSMTPPassword = (await _settingsRepository.Get("emailSMTPPassword"))?.Value;
        if (emailSMTPHost is not null
            && emailSMTPPort is not null
            && emailSMTPUserName is not null
            && emailSMTPPassword is not null)
        {
            SmtpClient client = new SmtpClient();
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            await client.ConnectAsync(emailSMTPHost, Int32.Parse(emailSMTPPort), SecureSocketOptions.Auto);
            await client.AuthenticateAsync(emailSMTPUserName, emailSMTPPassword);
            Console.WriteLine(client.IsAuthenticated);
            await client.SendAsync(message);
            await client.DisconnectAsync(true);
            client.Dispose();
        }
    }

消息从数据库加载并在特定时刻发送给用户。生产环境可能有什么问题?这些应用程序可以在同一台机器上运行,只是域发生了变化。

SMTP 日志:

Connected to smtps://specodit.pl:465/
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 220 specodit.pl ESMTP Exim 4.90_1 Ubuntu Tue, 27 Jul 2021 10:40:18 +0000
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: C: EHLO [146.59.19.44]
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 250-specodit.pl Hello [146.59.19.44] [146.59.19.44]
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 250-SIZE 52428800
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 250-8BITMIME
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 250-PIPELINING
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 250-AUTH PLAIN LOGIN
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 250-CHUNKING
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 250 HELP
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: C: AUTH PLAIN AGFkbWluQGt1bW1pbnVpLnBsAGMxOU91bkNsN08=
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 235 Authentication succeeded
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: C: MAIL FROM:<admin@kumminui.pl> SIZE=51244 BODY=8BITMIME
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: C: RCPT TO:<biuro@specodit.pl>
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: S: 550 146.59.19.44 is _my_ address
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: C: RSET

【问题讨论】:

    标签: c# asp.net-core mailkit


    【解决方案1】:

    作为RCPT TO 命令的一部分返回的错误很奇怪,因为错误消息似乎引用了EHLO 命令:

    C: EHLO [146.59.19.44]
    

    SMTP 服务器拒绝您,因为客户端声称的 IP 地址与服务器的 IP 地址相同。

    您可以通过设置client.LocalDomain 属性来覆盖此设置,该属性将在EHLO 命令中使用,而不是自动检测到的IP 地址。您可以尝试将其设置为某些内容(可能是“localhost”?),看看是否可以解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 2019-03-30
      • 2018-09-28
      • 2021-08-20
      • 1970-01-01
      • 2020-04-08
      相关资源
      最近更新 更多