Search code examples
c#emailsend

Can't send mail from C# app


I'm trying to send a mail from an app I'm developing but always get the same exception message: "Aditional Information: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated".

Here's my code:

SmtpClient appClient = new SmtpClient("smtp-mail.outlook.com");
                appClient.Port = 587;
                appClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                appClient.UseDefaultCredentials = false;
                System.Net.NetworkCredential Credentials = 
                    new System.Net.NetworkCredential(Properties.Settings.Default.systemMail, 
                        Properties.Settings.Default.systemMailPass);
                appClient.EnableSsl = true;
                appClient.Credentials = Credentials;

                try
                {
                    var mail = new MailMessage(Properties.Settings.Default.systemMail.Trim(), usrMail.Trim());
                    mail.Subject = "Usuario ha sido creado con éxito";
                    mail.Body = "Se ha creado el usuario " + usrCode + " en el servidor: " + Properties.Settings.Default.serverAdress + " instancia: " + Properties.Settings.Default.serverInstance + ".";
                    appClient.Send(mail);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }

Solution

  • Try to use the UseDefaultCredentials = false before setting Credentials:

    appClient.UseDefaultCredentials = false;    
    appClient.Credentials = Credentials;