Sending mail via .net core smtpClient: SmtpException / FormatException

牧云@^-^@ 提交于 2019-12-13 03:21:05

问题


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

This code is working fine.

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:

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.

Stack trace:

   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)

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:

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

Stack trace:

   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

Creation of the MailMessage object:

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

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


回答1:


I found the answer myself:

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 :(



来源:https://stackoverflow.com/questions/54218912/sending-mail-via-net-core-smtpclient-smtpexception-formatexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!