Failed to send email from 1and1 (ionos) hosting server using ASP .Net Core and Mailkit

二次信任 提交于 2020-12-15 05:23:31

问题


I have created a simple email service in my .Net Core application, which sends mail messages using provider email. It works just fine on my local machine with port 587. But when I published it to my 1and1 hosting server, it failed to send the email.

Here is my little code, how I create client using the Mailkit:

using(var client = new SmtpClient()) {
    client.ServerCertificateValidationCallback = (sender, certificate, certChainType, errors) => true;
    client.CheckCertificateRevocation = false;
    client.SslProtocols = SslProtocols.Tls12;
    await client.ConnectAsync("smtp.ionos.de", 587, MailKit.Security.SecureSocketOptions.SslOnConnect).ConfigureAwait(false); //465, 25
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    await client.AuthenticateAsync("MyEmail", "MyPass").ConfigureAwait(false);
    await client.SendAsync(email).ConfigureAwait(false);
    await client.DisconnectAsync(true).ConfigureAwait(false);
}

I have tried with different ports like 25, 465, and also 587, but none of them works on the server.

I also contacted the admin of 1and1 and he said that I have to set the ServerCertificateValidationCallback to false and they only support TLS 1.2 now. So I did as he said but now it doesn't work on my local machine, nor the server.

I got this error:

An error occurred while attempting to establish an SSL or TLS connection. This usually means that the SSL certificate presented by the server is not trusted by the system for one or more of the following reasons:

  1. The server is using a self-signed certificate which cannot be verified.
  2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
  3. A Certificate Authority CRL server for one or more of the certificates in the chain is temporarily unavailable.
  4. The certificate presented by the server is expired or invalid. It is also possible that the set of SSL/TLS protocols supported by the client and server do not match

or

The SMTP server does not support authentication.

Notes: Sending email from the server did work without TLS-setting about 2 months ago. After they have changed something on the server, it doesn't work anymore.

Does anyone have the same issue?


回答1:


I had the same problem. After having contacted them several times they told me that the Ionos smtp did not work on their windows environment, and that we had to use the following configuration:

"EmailConfiguration": {
    "SmtpServer": "mrnet.kundenserver.de",
    "SmtpPort": 25
}

In this case you must also delete the authenticate method and modify SecureSocketOptions to None.

using (var emailClient = new SmtpClient()) {
          emailClient.Connect(_emailConfiguration.SmtpServer, _emailConfiguration.SmtpPort, SecureSocketOptions.None);
          emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
          //Remove this line
          //emailClient.Authenticate(_emailConfiguration.SmtpUsername, _emailConfiguration.SmtpPassword);
          emailClient.Send(message);
          emailClient.Disconnect(true);  
}

This configuration doesn't work in debug mode but works for me when I publish it to their servers



来源:https://stackoverflow.com/questions/63055620/failed-to-send-email-from-1and1-ionos-hosting-server-using-asp-net-core-and-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!