首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MailKit MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted错误

MailKit MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted错误
EN

Stack Overflow用户
提问于 2021-07-27 18:21:23
回答 1查看 247关注 0票数 1

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

错误no1 (端口587,主机名等于服务器名和启动名):

代码语言:javascript
运行
复制
    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核心和MailKit发送电子邮件:

代码语言:javascript
运行
复制
    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;
    }

并发送:

代码语言:javascript
运行
复制
    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();
        }
    }

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

SMTP日志:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-28 01:42:49

由于错误消息似乎指向EHLO命令,因此作为RCPT TO命令的一部分返回的错误很奇怪:

代码语言:javascript
运行
复制
C: EHLO [146.59.19.44]

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

您可以通过设置client.LocalDomain属性来覆盖此设置,该属性将在EHLO命令中使用,而不是自动检测到的IP地址。您可以尝试将其设置为某个值(也许是"localhost"?)看看能不能解决这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68543150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档