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

C# 在ubuntu上通过.net内核发送电子邮件返回base64错误,c#,ubuntu,base64,smtpclient,C#,Ubuntu,Base64,Smtpclient,我试图通过运行在ubuntu 16.04上的测试服务器发送电子邮件时出错。我在OVH上有一个pro帐户,我正在使用此smtp: pro1.mail.ovh.net 当我在运行windows 10的工作站上调试时,我可以发送电子邮件 这是我的密码: var smtp = m_emailConfiguration["Smtp"]; var email = m_emailConfiguration["Email"]; var password = m_emailC

我试图通过运行在ubuntu 16.04上的测试服务器发送电子邮件时出错。我在OVH上有一个pro帐户,我正在使用此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)
我发现有人也有同样的问题:

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

有没有人有主意


谢谢

让我们把所有的评论都写在一个答案里

不要使用SmptClient。改用。已过时,并且Microsoft本身正在使用或其他库

调用堆栈显示错误是在尝试使用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 ();
var message=new mimessage();
message.From.Add(新邮箱地址(“Joey”)joey@friends.com"));
message.To.Add(新邮箱地址(“Alice”)alice@wonderland.com"));
message.Subject=“你好吗?”;
var builder=new BodyBuilder();
//设置消息文本的纯文本版本
builder.TextBody=@“嘿,爱丽丝,
....
--乔伊
";
//为了从html文本中引用selfie.jpg,我们需要添加它
//到builder.LinkedResources,然后在img src中使用其内容Id值。
var image=builder.LinkedResources.Add(@“C:\Users\Joey\Documents\Selfies\selfie.jpg”);
image.ContentId=MimeUtils.GenerateMessageId();
//设置消息文本的html版本
builder.HtmlBody=string.Format(@“Hey Alice,
.... --乔伊
“,image.ContentId); //我们可能还想为莫妮卡的派对附上一份日历。。。 builder.Attachments.Add(@“C:\Users\Joey\Documents\party.ics”); //现在我们只需要设置消息体,就完成了 message.Body=builder.ToMessageBody();
让我们把所有的评论都放在一个答案中

不要使用SmptClient。改用。已过时,并且Microsoft本身正在使用或其他库

调用堆栈显示错误是在尝试使用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 ();
var message=new mimessage();
message.From.Add(新邮箱地址(“Joey”)joey@friends.com"));
message.To.Add(新邮箱地址(“Alice”)alice@wonderland.com"));
message.Subject=“你好吗?”;
var builder=new BodyBuilder();
//设置消息文本的纯文本版本
builder.TextBody=@“嘿,爱丽丝,
....
--乔伊
";
//为了从html文本中引用selfie.jpg,我们需要添加它
//到builder.LinkedResources,然后在img src中使用其内容Id值。
var image=builder.LinkedResources.Add(@“C:\Users\Joey\Documents\Selfies\selfie.jpg”);
image.ContentId=MimeUtils.GenerateMessageId();
//设置消息文本的html版本
builder.HtmlBody=string.Format(@“Hey Alice,
.... --乔伊
“,image.ContentId); //我们可能还想为莫妮卡的派对附上一份日历。。。 builder.Attachments.Add(@“C:\Users\Joey\Documents\party.ics”); //现在我们只需要设置消息体,就完成了 message.Body=builder.ToMessageBody();
正如Panagiotis Kanavos在评论中所说,SmtpClient已被弃用。我改用MailKit,它现在运行良好。

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

发布完整的异常,而不仅仅是消息部分。使用
Exception.ToString()
可以很容易地获得。这包括所有内部异常和调用statck。事实上,您可能应该完全删除catch块,因为除了隐藏重要信息之外,它不会处理异常。您尝试发送的字符串不正确,异常解释了原因。但是,您没有发布创建该字符串或其内容的代码,因此不可能说出问题所在。这与SmtpClient或UbuntuFinally没有什么关系,它本身解释说它已经过时了,并且与。你想送什么?MailKit可能能够直接发送原始有效负载,并根据需要对其进行编码。例如,您可以轻松添加带有图像的纯文本和HTML正文。现在我看到了调用堆栈,看起来SmtpClient试图使用Windows身份验证进行连接,而它试图使用的令牌出现了问题。您真的要对此进行故障排除还是直接切换到MailKit?登录页中的示例显示了身份验证是多么容易。发布完整的异常,而不仅仅是消息部分。使用
Exception.ToString()
可以很容易地获得。这包括所有内部异常和调用statck。事实上,您可能应该完全删除catch块,因为除了隐藏重要信息之外,它不会处理异常。斯特里酒店