Hello. I am writing an application on ASP.NET Core. I use MailKit to send mail. The problem is this: it is impossible to connect to the SMTP server (I did not understand the reason). Decided to use SSL. Code:

public async Task SendEmailAsync(string email, string subject, string message) { try { var emailMessage = new MimeMessage(); emailMessage.From.Add(new MailboxAddress("Administration", "test@mail.ru")); emailMessage.To.Add(new MailboxAddress(email)); emailMessage.Subject = subject; emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = message }; using (var client = new SmtpClient()) { await client.ConnectAsync("smtp.mail.ru", 25, false); await client.AuthenticateAsync("test@mail.ru", "*****"); await client.SendAsync(emailMessage); await client.DisconnectAsync(true); } } catch (Exception e) { Console.WriteLine("Ошибка при отправке почты" + e.Message); } } 

Error: An error occurred while attempting to establish an SSL or TLS connection. SSL / TLS. The following is the case for the following reasons: I need you to verify the server's certificate. The certificate presented by the server is expired or invalid. See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate for possible solutions.

An error occurred while trying to establish an SSL or TLS connection. You may be trying to connect to a port that does not support SSL / TLS. Another possibility is that the SSL certificate presented by the server is not trusted by the system for one or more of the following reasons: The server is using a self-signed certificate that cannot be verified. The local system does not have a root or intermediate certificate required to verify the server certificate. The certificate provided by the server has expired or is invalid. See for possible solutions.

I watched the link. The solution did not help. I decided to do it as described here: Confirming Email in ASP.NET Identity

Did not help.

And what is more strange, the fact that a letter was sent a couple of times, but later ceased. Tried to specify other ports, did not help. I did not understand what was the matter.

I will be glad to hear your answers.

  • One possibility is that you are trying to connect to a port which does not support SSL/TLS. is there exactly to be port 25 ? - tym32167
  • Tym32167, I pointed out all the ports, it did not work. - General2001

1 answer 1

This is the code I sent when I worked, this is his copy-paste, because now I have nothing to check it with.

 using(var client = new SmtpClient("smtp.mail.ru", 25)) { client.Credentials = new NetworkCredential("***@mail.ru", "*****"); client.EnableSsl = true; var message = new MailMessage("***@mail.ru", "email@to.com", args[0], args[1]); message.IsBodyHtml = true; client.Send(message); } 

I took your version of the program, sent myself several letters to mail.ru and to gmail.com, everything works perfectly.

 class EmailSender { public async Task SendEmailAsync(string email, string subject, string message) { try { var emailMessage = new MimeMessage(); emailMessage.From.Add(new MailboxAddress("Administration", "*****@mail.ru")); emailMessage.To.Add(new MailboxAddress(email)); emailMessage.Subject = subject; emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = message }; using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Timeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds; await client.ConnectAsync("smtp.mail.ru", 25, false); await client.AuthenticateAsync("*****@mail.ru", "**********"); await client.SendAsync(emailMessage); await client.DisconnectAsync(true); } } catch (Exception e) { Console.WriteLine("Ошибка при отправке почты" + e.Message); } } } 
  • Some properties are missing. But this code is the same as I wrote. Therefore, I do not think that will work. - General2001
  • @ General2001 well, it’s 100% working about 5 years ago on a regular freyivorka :) I can't test it now, if you have such an opportunity - try it. Well, and await client.ConnectAsync("smtp.mail.ru", 25, false); if the last parameter is about SSL, then it probably should be true - tym32167
  • Tym32167, thank you very much for your help. You are the only person here who responds to my messages. For that respect. But I tried it too. Virtually all options. Even the mail changed. And does not work. The letter was not sent first; it was sent a couple of times later. Then it stopped again. What surprises me is that there was a successful connection after all. But! The error reappears. - General2001
  • @ General2001 If you managed to send an email at least 1 time, it means that one of your folders was successful - that is, you had the correct version of the code that you need - tym32167 February
  • That's the point. I myself can not understand what could be wrong in my code. It is not clear one thing, if once went and then ceased, what could be the matter? For some reason, I always have such errors. If I start somewhere, I immediately get a bunch of pitfalls. And it immediately gives the impression that the noob ... - General2001