Send Email Using Outlook.com SMTP

I am trying to send an automatic email using Outlook.com smtp support. However, I get the following exception:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host" Exception while sending email. 

My code is:

  public bool SendEmail(MailMessage msg) { try { SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com") { UseDefaultCredentials = false, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential("userAddress", "userPassword"), Port = 587, EnableSsl = true, }; smtpClient.Send(msg); msg.Dispose(); smtpClient.Dispose(); return true; } catch (Exception exp) { Console.WriteLine(exp.ToString()); return false; } } 
+4
source share
1 answer

I know this is a very old question and I can't even help, however I had a similar problem when I tried to send an email using C #.

As a result, I used this, which allowed me to send emails:

 string _sender = ""; string _password = ""; SmtpClient client = new SmtpClient("smtp-mail.outlook.com"); client.Port = 587; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(_sender, _password); client.EnableSsl = true; client.Credentials = credentials; MailMessage message = new MailMessage(_sender, "recipient of email"); message.Subject = ""; message.Body = ""; client.Send(message); 

It will probably be useless to you, but in case someone stumbles upon this question, at least there is an answer in which the working code acts as a fix!

0
source

Source: https://habr.com/ru/post/1502712/


All Articles