使用 MailKit 发送电子邮件的 dotnet 核心网站中的奇怪问题
Weird Issue in dotnet core website using MailKit to send email
我刚从供应商那里继承了一个网站,并检查了该网站的错误和源代码。
该应用程序基于 dotnet core 1.1 构建,并使用 Mailkit 通过我们的公司代理通过 SMTP 发送电子邮件。
问题是邮件发送功能在发送电子邮件时表现不稳定。大多数时候我调试它出错的发送方法。我们使用 Authentication
Required Flag 并将 userId 和 Password 传递给 MailKit Authenticate
Method
StackTrace 消息
AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful
at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding,
ICredentials credentials, CancellationToken cancellationToken) at
MailKit.MailService.Authenticate(String userName, String password,
CancellationToken cancellationToken) at
Rules.Emailer.SendNotification.Send(String to, String from, String
subject, String body) in
C:\Workspace\G\Rules\Emailer\SendNotification.cs:line
94 at G.Rules.Emailer.UserNotifications.ResetPassword(String
代码片段
using (var client = new SmtpClient())
{
client.Connect(EmailConfiguration.SmtpServer, EmailConfiguration.SmtpPort, EmailConfiguration.UseSsl);
if (EmailConfiguration.RequiresAuthentication)
{
client.Authenticate(EmailConfiguration.Username, EmailConfiguration.Password);
}
//TODO: Only Send if PROD
client.Send(message);
client.Disconnect(true);
}
return true;
}
Connected to smtp://<IP>:25/?starttls=when-available
S: 220 <Server_REDACTED>.<DOMAIN_REDACTED>.COM Microsoft ESMTP MAIL Service ready at Tue, 27 Mar 2018 03:22:42 -0400
C: EHLO [10.207.8.74]
S: 250-<Server_REDACTED>.<DOMAIN_REDACTED>.COM Hello [10.207.8.74]
S: 250-SIZE
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-X-ANONYMOUSTLS
S: 250-AUTH NTLM
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250-XRDST
S: 250 XSHADOW
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [10.207.8.74]
S: 250-<Server_REDACTED>.<DOMAIN_REDACTED>.COM Hello [10.207.8.74]
S: 250-SIZE
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH NTLM LOGIN
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250-XRDST
S: 250 XSHADOW
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: <EMAIL_REDACTED>
S: 334 UGFzc3dvcmQ6
C: <PSWD_REDACTED>
S: 535 5.7.3 Authentication unsuccessful
NTLM连接,你可以试试:
await client.ConnectAsync(SmtpServer.SenderServer, SmtpServer.Port).ConfigureAwait(false);
if (client.AuthenticationMechanisms.Contains("NTLM"))
{
var ntlm = new SaslMechanismNtlm(SmtpServer.UserName, SmtpServer.Password);
await client.AuthenticateAsync(ntlm).ConfigureAwait(false);
}
else
{
await client.AuthenticateAsync(SmtpServer.UserName, SmtpServer.Password).ConfigureAwait(false);
}
我刚从供应商那里继承了一个网站,并检查了该网站的错误和源代码。
该应用程序基于 dotnet core 1.1 构建,并使用 Mailkit 通过我们的公司代理通过 SMTP 发送电子邮件。
问题是邮件发送功能在发送电子邮件时表现不稳定。大多数时候我调试它出错的发送方法。我们使用 Authentication
Required Flag 并将 userId 和 Password 传递给 MailKit Authenticate
Method
StackTrace 消息
AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful
at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken) at MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken) at Rules.Emailer.SendNotification.Send(String to, String from, String subject, String body) in C:\Workspace\G\Rules\Emailer\SendNotification.cs:line 94 at G.Rules.Emailer.UserNotifications.ResetPassword(String
代码片段
using (var client = new SmtpClient())
{
client.Connect(EmailConfiguration.SmtpServer, EmailConfiguration.SmtpPort, EmailConfiguration.UseSsl);
if (EmailConfiguration.RequiresAuthentication)
{
client.Authenticate(EmailConfiguration.Username, EmailConfiguration.Password);
}
//TODO: Only Send if PROD
client.Send(message);
client.Disconnect(true);
}
return true;
}
Connected to smtp://<IP>:25/?starttls=when-available
S: 220 <Server_REDACTED>.<DOMAIN_REDACTED>.COM Microsoft ESMTP MAIL Service ready at Tue, 27 Mar 2018 03:22:42 -0400
C: EHLO [10.207.8.74]
S: 250-<Server_REDACTED>.<DOMAIN_REDACTED>.COM Hello [10.207.8.74]
S: 250-SIZE
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-X-ANONYMOUSTLS
S: 250-AUTH NTLM
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250-XRDST
S: 250 XSHADOW
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [10.207.8.74]
S: 250-<Server_REDACTED>.<DOMAIN_REDACTED>.COM Hello [10.207.8.74]
S: 250-SIZE
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH NTLM LOGIN
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250-XRDST
S: 250 XSHADOW
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: <EMAIL_REDACTED>
S: 334 UGFzc3dvcmQ6
C: <PSWD_REDACTED>
S: 535 5.7.3 Authentication unsuccessful
NTLM连接,你可以试试:
await client.ConnectAsync(SmtpServer.SenderServer, SmtpServer.Port).ConfigureAwait(false);
if (client.AuthenticationMechanisms.Contains("NTLM"))
{
var ntlm = new SaslMechanismNtlm(SmtpServer.UserName, SmtpServer.Password);
await client.AuthenticateAsync(ntlm).ConfigureAwait(false);
}
else
{
await client.AuthenticateAsync(SmtpServer.UserName, SmtpServer.Password).ConfigureAwait(false);
}