MailKit MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted 错误
MailKit MailKit.Net.Smtp.SmtpClient.OnSenderNotAccepted error
我遇到了与 MailKit 相关的问题。在开发环境中,它可以正常工作并正确发送电子邮件。我在我的域上托管了系统的测试版环境(电子邮件服务器所在的位置 - 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
作为 RCPT TO
命令的一部分返回的错误很奇怪,因为错误消息似乎返回到 EHLO
命令:
C: EHLO [146.59.19.44]
SMTP 服务器拒绝您,因为客户端声称的 IP 地址与服务器的 IP 地址相同。
您可以通过设置将在 EHLO
命令中使用的 client.LocalDomain
属性 而不是自动检测到的 IP 地址来覆盖它。您可以尝试将其设置为某些内容(也许是“本地主机”?),看看是否可以解决问题。
我遇到了与 MailKit 相关的问题。在开发环境中,它可以正常工作并正确发送电子邮件。我在我的域上托管了系统的测试版环境(电子邮件服务器所在的位置 - 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
作为 RCPT TO
命令的一部分返回的错误很奇怪,因为错误消息似乎返回到 EHLO
命令:
C: EHLO [146.59.19.44]
SMTP 服务器拒绝您,因为客户端声称的 IP 地址与服务器的 IP 地址相同。
您可以通过设置将在 EHLO
命令中使用的 client.LocalDomain
属性 而不是自动检测到的 IP 地址来覆盖它。您可以尝试将其设置为某些内容(也许是“本地主机”?),看看是否可以解决问题。