首页 > 解决方案 > 在 ubuntu 上通过 .net 核心发送电子邮件返回 base64 错误

问题描述

当我尝试通过在 ubuntu 16.04 上运行的测试服务器发送电子邮件时出现错误。我在 OVH 上有一个专业帐户,我正在使用这个 smtp:

pro1.mail.ovh.net

当我在运行 Windows 10 的工作站上进行调试时,我可以发送电子邮件。

这是我的代码:

      var smtp = m_emailConfiguration["Smtp"];
      var email = m_emailConfiguration["Email"];
      var password = m_emailConfiguration["Password"];

      try
      {              
        using (var smtpClient = new SmtpClient(smtp, 587))
        {
          var mailMessage = new MailMessage(email, mailTo, subject, body);

          smtpClient.UseDefaultCredentials = false;
          smtpClient.Credentials = new NetworkCredential(email, password);
          smtpClient.EnableSsl = true;               
          smtpClient.Send(mailMessage);              
        }
      }
      catch (Exception ex)
      {
        throw new Exception(ex.InnerException.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

完整的堆栈跟踪:

Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.FormatException: 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)

我发现有人遇到同样的问题:通过 .net core smtpClient 发送邮件:SmtpException / FormatException

但他的问题是密码,我确定我的密码是正确的。

那么有人有想法吗?

谢谢

标签: c#ubuntubase64smtpclient

解决方案


让我们将所有评论放在答案中。

不要使用 SmptClient。请改用MailKitSmtpClient 已过时,Microsoft 本身建议使用MailKit或其他库。

调用堆栈显示在尝试使用Windows身份验证进行身份验证时引发了错误。在这种情况下,这显然是错误的,但是由于该类已过时,因此该错误可能无法准确修复。

发送消息示例显示发送消息是多么容易:

using (var client = new SmtpClient ()) {
    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
    client.ServerCertificateValidationCallback = (s,c,h,e) => true;

    client.Connect ("smtp.friends.com", 587, false);

            // Note: only needed if the SMTP server requires authentication
    client.Authenticate ("joey", "password");

    client.Send (message);
    client.Disconnect (true);
}

MailKit 建立在 MimeKit 之上,这意味着它可以轻松创建复杂的消息。复制另一个示例,您可以使用BodyBuilder实用程序类来创建包含纯文本正文和带有图像的 HTML 正文的消息。

        var message = new MimeMessage ();
        message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
        message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
        message.Subject = "How you doin?";

        var builder = new BodyBuilder ();

        // Set the plain-text version of the message text
        builder.TextBody = @"Hey Alice,
....
-- Joey
";

        // In order to reference selfie.jpg from the html text, we'll need to add it
        // to builder.LinkedResources and then use its Content-Id value in the img src.
        var image = builder.LinkedResources.Add (@"C:\Users\Joey\Documents\Selfies\selfie.jpg");
        image.ContentId = MimeUtils.GenerateMessageId ();

        // Set the html version of the message text
        builder.HtmlBody = string.Format (@"<p>Hey Alice,<br>
....
<p>-- Joey<br>
<center><img src=""cid:{0}""></center>", image.ContentId);

        // We may also want to attach a calendar event for Monica's party...
        builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics");

        // Now we just need to set the message body and we're done
        message.Body = builder.ToMessageBody ();

推荐阅读