'SMTP Outlook can't send Emails

i'm stuck in this problem when button click it always says "Failure to send email" I try several host like smtp.office365.com, pod51015.outlook.com and ports like 465, 25 and nothing seems to work

                string _sender = "myEmail.com";
                string _password = "myPass";

                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(_sender, _password);
                client.EnableSsl = true;
                client.Credentials = credentials;

                MailMessage message = new MailMessage(_sender, "toEmail.com");
                message.Subject = "mySubject";
                message.Body = "myBody";
                client.Send(message);

CTTO of this code which I also found in this forum which seem they worked for them.



Solution 1:[1]

Office 365 SMTP servers no longer allow Basic auth by default - see https://docs.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/deprecation-of-basic-authentication-exchange-online

Solution 2:[2]

Your code looks like mine, except I did force .NET to use TLS 1.2. TLS 1.1 was deprecated by office365.com in January, and this caused problems with my application.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Solution 3:[3]

Let's try this workaround, it could be helpful.

  • Enable Legacy TLS: Set-TransportConfig -AllowLegacyTLSClients $true?
  • Confirm Legacy TLS is set: Get-TransportConfig | Format-List AllowLegacyTLSClients

Change the SMTP endpoint to smtp-legacy.office365.com

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Streblechenko
Solution 2 A. Niese
Solution 3 Ratul