2

In asp.net core 1 app I'm trying to send email with mailkit library(version 1.8.1) to strato server. My code:

  var emailMessage = new MimeKit.MimeMessage();
  try
  {
      emailMessage.From.Add(new MimeKit.MailboxAddress("Support", "some@test.com"));
      emailMessage.To.Add(new MimeKit.MailboxAddress("", "test@test.com"));
      emailMessage.Subject = "Subject";
      var bodyBuilder = new MimeKit.BodyBuilder();
      bodyBuilder.HtmlBody = @"<b>Some body</b>";

      emailMessage.Body = bodyBuilder.ToMessageBody(); 

      using (var client = new MailKit.Net.Smtp.SmtpClient())
      {
          client.Timeout = 15000;
          // Accept all SSL certificates (in case the server supports STARTTLS)
          client.ServerCertificateValidationCallback = (s, c, h, e) => true;

          await client.ConnectAsync("smtp.strato.de", 587, MailKit.Security.SecureSocketOptions.Auto);
          // Note: since we don't have an OAuth2 token, disable
          // the XOAUTH2 authentication mechanism.
          client.AuthenticationMechanisms.Remove("XOAUTH2");

          // Note: only needed if the SMTP server requires authentication
          await client.AuthenticateAsync(login, passord);

          await client.SendAsync(emailMesage);//here error!
          await client.DisconnectAsync(true);
      }
      return true;
  }
  catch (System.Exception)
  {
      return false;
  } 

When I'm trying to send email I get 5.7.0 User not authenticated error, despite authentication passed. The same code, for example, with gmail works fine.

Any ideas how to fix it?

Sasha
  • 833
  • 1
  • 20
  • 40

2 Answers2

7

The problem is that smtp.strato.de resets its authentication state when the SMTP client sends an EHLO command after authenticating, even though the specifications strongly suggest that the client should do that.

In other words, smtp.strato.de is broken.

That said, you can work around this by doing the following:

client.QueryCapabilitiesAfterAuthenticating = false;

As long as you set that property to false before authenticating, it will work.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
1
client.EnableSsl = true;   Add this to your code

Element (Network Settings)

E-MAIL (SMTP) ERROR CODES

5.5.1 Syntax error, command unrecognized.

5.5.0 Sender e-mail address already specified.

5.5.1 Need MAIL before RCPT.

5.4.4 Host not found (not in DNS).

5.1.1 Mailbox does not exist.

5.1.3 Bad e-mail address syntax.

>   5.7.0 Your IP address is in a blacklist.

5.1.8 Sender e-mail address domain does not exist.

5.1.8 Sender address rejected by address filter.

5.7.1 Relaying denied.

MMM
  • 3,132
  • 3
  • 20
  • 32
  • smtpclient do not contain definition to enableSsl property – Sasha Oct 25 '16 at 09:42
  • We checked blacklist - we are not there. Log file shows following:C: MAIL FROM: BODY=8BITMIME C: RCPT TO: S: 530 5.7.0 User not authenticated S: 503 5.5.1 need MAIL FROM first Any ideas how to deal with this error? – Sasha Oct 25 '16 at 11:24