我对同一邮件发送进行了两种实现:
//MailKit.Net.Smtp.SmtpClient
--------------------------------------------------------------------------------------
MailKit.Net.Smtp.SmtpClient smtp;
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Test sender", "sender@domain.com"));
message.To.Add(new MailboxAddress("Test Recipiment", "recipient@domain.com"));
message.Subject = "My subject";
message.Body = new TextPart("plain")
{
Text = "my body"
};
smtp = new MailKit.Net.Smtp.SmtpClient();
smtp.Connect("mail.domain.com","25",false);
smtp.Authenticate("sender", "MyPassword");
smtp.Send(message);
smtp.Disconnect(true);
smtp.Dispose();
//System.Net.Mail.SmtpClient
--------------------------------------------------------------------------------------
SmtpClient smtp = new SmtpClient("mail.domain.com","25");
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
MailAddress fromMailAdd;
try
{
fromMailAdd = new MailAddress("sender@domain.com");
}
MailMessage mailMessage = new MailMessage
{
From = fromMailAdd,
IsBodyHtml = false,
Body = "my body",
Subject = "My subject",
};
mailMessage.To.Add(new MailAddress("recipient@domain.com"));
smtp.Credentials = new NetworkCredential("sender", "MyPassword");
smpt.Send(mailMessage);
MailKit实现正在运行System.Net.Mail无法运行。它得到"Mailbox name not allowed. The server response was: authentication is required"
我查看了MailKit SmtpClient的代码,其身份验证执行以下操作:
var saslUri = new Uri ($"smtp://{uri.Host}");
AuthenticationException authException = null;
SmtpResponse response;
SaslMechanism sasl;
bool tried = false;
string challenge;
string command;
foreach (var authmech in SaslMechanism.AuthMechanismRank) {
if (!AuthenticationMechanisms.Contains (authmech))
continue;
if ((sasl = SaslMechanism.Create (authmech, saslUri, encoding, credentials)) == null)
continue;
tried = true;
cancellationToken.ThrowIfCancellationRequested ();
// send an initial challenge if the mechanism supports it
if (sasl.SupportsInitialResponse) {
challenge = sasl.Challenge (null);
command = string.Format ("AUTH {0} {1}", authmech, challenge);
} else {
command = string.Format ("AUTH {0}", authmech);
}
response = await SendCommandAsync (command, doAsync, cancellationToken).ConfigureAwait (false);
if (response.StatusCode == SmtpStatusCode.AuthenticationMechanismTooWeak)
continue;
SaslException saslException = null;
try {
while (!sasl.IsAuthenticated) {
if (response.StatusCode != SmtpStatusCode.AuthenticationChallenge)
break;
challenge = sasl.Challenge (response.Response);
response = await SendCommandAsync (challenge, doAsync, cancellationToken).ConfigureAwait (false);
}
saslException = null;
} catch (SaslException ex) {
// reset the authentication state
response = await SendCommandAsync (string.Empty, doAsync, cancellationToken).ConfigureAwait (false);
saslException = ex;
}
if (response.StatusCode == SmtpStatusCode.AuthenticationSuccessful) {
if (sasl.NegotiatedSecurityLayer)
await EhloAsync (doAsync, cancellationToken).ConfigureAwait (false);
authenticated = true;
OnAuthenticated (response.Response);
return;
}
var message = string.Format ("{0}: {1}", (int) response.StatusCode, response.Response);
Exception inner;
if (saslException != null)
inner = new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response, saslException);
else
inner = new SmtpCommandException (SmtpErrorCode.UnexpectedStatusCode, response.StatusCode, response.Response);
authException = new AuthenticationException (message, inner);
}
if (tried)
throw authException ?? new AuthenticationException ();
throw new NotSupportedException ("No compatible authentication mechanisms found.");
凭据与.Net Mail客户端中使用的凭据是相同的NetworkCredentials类。
是否可以让.net Mail客户端执行相同类型的身份验证?
编辑: 无法更改邮件服务器本身上的内容