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

Asked

Viewed 892 times

3

I am trying to use an account to send email, with my project in c#, using the namespace System.Net.Mail, however I can’t authenticate on outlook server, it throws me the exception:

System.Net.Mail.Smtpexception: 'Mailbox not available. A server response was: 5.7.3 Requested action aborted; user not authenticated'

Follows 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;
     }
 }
  • Check your MS account security settings. You should probably allow some usage option.

1 answer

0

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 account settings.

  • Good afternoon, thank you I am not able to find where enables it in the account?

  • My code is the same as yours, but not access, I believe that’s right, in google I got, but in microsoft account I can not find.

  • There are two questions: 1-enable pop options in settings->account 2- check in https://account.microsoft.com/account. if there is a device that is not released.

  • Another thing: tried to put Hotmail.com in place of outlook.com in your email

  • POP is released and yes I tried with Hotmail too!

  • tested with another email?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.