How to use a hotmail/outlook account to send email in c#

Question:

I'm trying to use an account to send email, with my project in C#, using the namespace System.Net.Mail , but I can't authenticate to the outlook server, it throws the exception:

System.Net.Mail.SmtpException: 'Mailbox not available. The server's response was: 5.7.3 Requested action aborted; user not authenticated'

Here is my code:

private void Email()
{

     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("meuemail@outlook.com", "minhasenha");
     client.EnableSsl = true;
     client.Credentials = credentials;
     client.TargetName = "smtp-mail.outlook.com";
     try
     {
         MailMessage mail = new MailMessage();
         mail.From = new MailAddress("ricardopulini@outlook.com",string.Empty,System.Text.Encoding.UTF8);
         mail.To.Add (new MailAddress("destinatario@mail.com"));
         mail.Subject = "Teste de e-mail";

         mail.Body = "Teste de e-mail";
         client.Send(mail);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         throw ex;
     }
 }

Answer:

This code suits me well:

MailMessage mail = new MailMessage("remetente", "destinatario");
SmtpClient client = new SmtpClient();

client.EnableSsl = true;
client.Host = "smtp-mail.outlook.com";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("email", "senha");

client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;

mail.Subject = "teste";
mail.Body = "teste2";

client.Send(mail);

In addition to going to Hotmail settings and enabling the use of apps, in the account settings.

Scroll to Top