我正在尝试通过 .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);
}
第一个问题是现在我收到一条错误消息:
输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。
堆栈跟踪:
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);
}
这样做我得到另一个错误:
SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.1 客户端未通过身份验证
蓝山帝景
相关分类