在ubuntu上通过.net核心发送电子邮件返回base64错误

时间:2019-05-09 09:23:24

标签: 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)

我发现一个遇到相同问题的人:this

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

那么有人知道吗?

谢谢

2 个答案:

答案 0 :(得分:0)

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

不要使用SmptClient。请改用MailKitSmtpClient已过时,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 ();

答案 1 :(得分:0)

就像Panagiotis Kanavos在评论中所说,SmtpClient已被弃用。我改用MailKit,现在可以正常使用了。