简体   繁体   中英

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;
                }

Try to use the UseDefaultCredentials = false before setting Credentials:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM