PaziewskiPatryk
PaziewskiPatryk

Reputation: 35

MailKit MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted error

I have an issue connected with MailKit. On the dev environment it works correctly and sends the e-mails correctly. I have a beta environment of the system hosted on my domain (where the email server is located - kumminui.specodit.pl) I have created the production environment at kumminui.pl (so the .specodit part has been removed). Right now the emails are not working.

Error no1 (with port 587 and hostname equal to server name and 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

I send the email with .NET Core and MailKit:

    private async Task<MimeMessage> PrepareMessage(AppUser user, string subject, BodyBuilder body)
    {
        var emailSMTPUserName = (await _settingsRepository.Get("emailSMTPUserName"))?.Value ?? "[email protected]";
        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;
    }

And Send:

    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();
        }
    }

The message is loaded from DB and sent to the user at a specific moment. What could be an issue with the production environment? Those applications works on the same machine only the domain changes.

SMTP log:

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:<[email protected]> SIZE=51244 BODY=8BITMIME
Jul 27 10:40:18 specodit.pl dotnet-example[9875]: C: RCPT TO:<[email protected]>
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

Upvotes: 1

Views: 2271

Answers (1)

jstedfast
jstedfast

Reputation: 38528

The error being returned as part of the RCPT TO command is weird seeing as how the error message seems to refer back to the EHLO command:

C: EHLO [146.59.19.44]

The SMTP server is rejecting you because the IP address that the client is claiming to be is the same IP address as the server.

You can override this by setting the client.LocalDomain property which will be used in the EHLO command instead of the auto-detected IP address. You could try setting that to something (maybe "localhost"?) and see if that solves it.

Upvotes: 2

Related Questions