c# - 在 ubuntu 上通过 .net core 发送电子邮件返回 base64 错误

标签 c# ubuntu base64 smtpclient

当我尝试通过在 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)

我发现有人遇到了同样的问题:Sending mail via .net core smtpClient: SmtpException / FormatException

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

那么有人有想法吗?

谢谢

最佳答案

让我们把所有评论放在一个答案中。

不要使用 SmptClient。使用 MailKit反而。 SmtpClient已过时,Microsoft 本身 recommends使用 MailKit或其他图书馆。

调用堆栈显示在尝试使用 Windows 身份验证进行身份验证时出现错误。在这种情况下,这显然是错误的,但该错误可能不会准确修复,因为该类已过时。

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

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 之上,这意味着它可以轻松创建复杂的消息。从 another example 复制,您可以使用 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 ();

关于c# - 在 ubuntu 上通过 .net core 发送电子邮件返回 base64 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56056242/

相关文章:

c# - 在控制台应用程序中创建图像或位图 - 似乎无法找到 System.Drawing?

c# - 如何用零格式化或填充字符串日期?

c# - 在 Ubuntu 中从 ttyUSB 端口创建 COM 端口

c++ - 如何在 C++ 中从 GDI+ 图像创建 Base64 编码的字符串?

encryption - 最后所有加密都使用==

javascript - jCrop HTML5 Canvas Base64

c# - MSDTC - 与底层事务管理器的通信失败

c# - 为什么空模型返回到 post 方法?

c++ - 如何在 Linux 上的特定目录(不需要 sudo 权限的目录)中安装 C++ 库(例如 Blitz++)?

linux - 巨大的二进制文件几乎缩小了 100%?