0

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.

Byte
  • 27
  • 6
  • "nothing seems to work" - we need the exception details etc – jazb Apr 01 '22 at 03:22
  • take a look at : https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – jazb Apr 01 '22 at 03:36
  • if I use Gmail SMTP it works just this outlook catching error @Jazb – Byte Apr 01 '22 at 04:32
  • if i use Gmail SMTP and Gmail account it works fine while in outlook I get error – Byte Apr 01 '22 at 04:42
  • Does this answer your question? [Sending email through Gmail SMTP server with C#](https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – Eugene Astafiev Apr 03 '22 at 22:00

3 Answers3

1

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;
A. Niese
  • 399
  • 3
  • 13
0

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

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

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

Ratul
  • 43
  • 2
  • 8
  • How-to use ***AllowLegacyTLSClients*** in NET (C#) with a SmtpClient class? – Kiquenet Jun 17 '22 at 19:31
  • You have to enable the Legacy TLS on your Microsoft Exchange Admin first. Then use _smtp-legacy.office365.com_ as _HOST_ – Ratul Jun 23 '22 at 17:10