Search code examples
c#ubuntubase64smtpclient

Sending email via .net core on ubuntu return base64 error


I'm having an error when I try to send an email through my test server which is running on ubuntu 16.04. I have a pro account on OVH and I'm using this smtp :

pro1.mail.ovh.net

When I'm on debug on my workstation running on windows 10, I can send my email.

Here is my code :

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

I'm having this error :

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

Full stack trace :

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)

I found someone whose got the same problem : Sending mail via .net core smtpClient: SmtpException / FormatException

But his problem was the password, I'm sure that my password is correct.

So is there someone whose got an idea ?

Thanks


Solution

  • Let's put all the comments in an answer.

    Don't use SmptClient. Use MailKit instead. SmtpClient is obsolete and Microsoft itself recommends using MailKit or another library.

    The call stack shows that the error was raised while trying to authenticate using Windows authentication. That's clearly wrong in this case but the bug will probably not be fixed precisely because the class is obsolete.

    The Sending Messages example shows how easy it is to send 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 is built on top of MimeKit which means it can create complex messages easily. Copying from another example, you can use the BodyBuilder utility class to create a message containing both a plain text body and an HTML body with images.

            var message = new MimeMessage ();
            message.From.Add (new MailboxAddress ("Joey", "[email protected]"));
            message.To.Add (new MailboxAddress ("Alice", "[email protected]"));
            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 ();