通过 .net core smtpClient 发送邮件:SmtpException / FormatException

Sending mail via .net core smtpClient: SmtpException / FormatException

我正在尝试通过 .net 核心中的 SmtpClient 发送邮件。基本上我只是将一些旧的 .net 框架代码迁移到 .net 核心。在旧系统中,它是通过以下方式完成的:

using (var smtpClient = new SmtpClient("smtp.xyz.de", 587))
{
   smtpClient.UseDefaultCredentials = false;
   smtpClient.Credentials = new System.Net.NetworkCredential("user", "password", "domain");
   smtpClient.EnableSsl = true;
   smtpClient.Send(mailMessage);
}

此代码运行良好。

现在我将此代码迁移到 .net 核心,如下所示:

 using (var smtpClient = new SmtpClient("smtp.xyz.de", 587))
 {
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = new NetworkCredential("user", "password", "domain");
    smtpClient.EnableSsl = true;  
    smtpClient.Send(mailMessage);
 }

第一个问题是现在我收到一条错误消息:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

堆栈跟踪:

   at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
   at System.Convert.FromBase64String(String s)
   at System.Net.Mail.SmtpNegotiateAuthenticationModule.GetSecurityLayerOutgoingBlob(String challenge, NTAuthentication clientContext)
   at System.Net.Mail.SmtpNegotiateAuthenticationModule.Authenticate(String challenge, NetworkCredential credential, Object sessionCookie, String spn, ChannelBinding channelBindingToken)
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

由于该错误,我尝试将用户和密码字符串转换为 Base64,如下所示:

using (var smtpClient = new SmtpClient("smtp.xyz.de", 587))
{
   var userEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes("user"));
   var passwordEncoded = convert.ToBase64String(Encoding.UTF8.GetBytes("password"));

   smtpClient.UseDefaultCredentials = false;
   smtpClient.Credentials = new NetworkCredential(userEncoded, passwordEncoded, "domain");
   smtpClient.EnableSsl = true;               
   smtpClient.Send(mailMessage);              
}

这样做我得到另一个错误:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

堆栈跟踪:

   at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at MailTest.Program.ProgramStart() in C:\Repos\MailTest\MailTest\Program.cs:line 67

MailMessage 对象的创建:

mailMessage = new MailMessage("sender@xyz.com", "recipient@xyz.com", "subject", "body");

任何人都可以找出我做错了什么吗?对我来说,除了转换为 Base64 之外,代码看起来完全一样。

我自己找到了答案:

这很尴尬,我感到很惭愧,但我所做的所有测试都是使用错误的密码。由于烦人的错误消息,我从来没有想到它:(