通过 .net core smtpClient 发送邮件:SmtpException/FormatException [英] Sending mail via .net core smtpClient: SmtpException / FormatException

查看:21
本文介绍了通过 .net core smtpClient 发送邮件:SmtpException/FormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send mails via SmtpClient in .net core. Basically I'm just migrating some old .net framework code to .net core. In the old system it's done in the following way:

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 核心,如下所示:

Now I migrated this code to .net core like below:

 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 first issue is now that I get an error message:

输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符或填充字符中的非法字符.

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,如下所示:

Because of that error I tried to convert the user and passwort strings to Base64 like below:

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);              
}

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

Doing this I get another error:

SMTP 服务器需要安全连接或客户端未通过身份验证.服务器响应为:5.7.1 客户端未通过身份验证

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:ReposMailTestMailTestProgram.cs:line 67

创建 MailMessage 对象:

Creation of the MailMessage object:

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

谁能弄清楚我做错了什么?对我来说,除了转换为 Base64 之外,代码看起来完全相同.

Can anyone figure out, what I am doing wrong? For me the code looks exactly the same except the conversion to Base64.

推荐答案

我自己找到了答案:

这很尴尬,我感到很惭愧,但我所做的所有测试都使用了错误的密码.由于令人讨厌的错误消息,我从未想到它:(

It's embarassing and I feel ashamed, but all the tests I did I was using the wrong password. It just never came to my mind because of the irritating error message :(

这篇关于通过 .net core smtpClient 发送邮件:SmtpException/FormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆