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

1

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

  

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

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

         mail.Body = "Teste de e-mail";
         client.Send(mail);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         throw ex;
     }
 }
    
asked by anonymous 28.06.2017 / 21:19

1 answer

0

This code serves 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 the Hotmail settings and enabling the use of applications, in the account settings.

    
29.06.2017 / 15:35