1

I am using SmtpClient with office365 mailserver to send email. But everytime i try smtpclient.Send(msg),it will throw 'Operation has timed out' smtpexception. I have tried all the earlier options like to change the port to 587 and increase the timeout value but nothing works. Can anyone help me out. Below is my source code.

    using (MailMessage msg = new MailMessage
    {                    
        From = new MailAddress("Sender@ourdomain.com"),
        Subject = this.Subject,
        Body = this.Body,
        IsBodyHtml = true
    })
    {
        msg.To.Add(new MailAddress("Receiver@ourdomain.com"));
        using (SmtpClient client = new SmtpClient
        {
            Host = "smtp.office365.com",
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true,
            UseDefaultCredentials = false,                        
            Credentials = new System.Net.NetworkCredential("username", "password"),
            Port = 587
        })
        {
            try
            {
                client.TargetName = "STARTTLS/smtp.office365.com";
                client.Send(msg);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
KrishD
  • 11
  • 1
  • 2
  • IMHO https://learn.microsoft.com/en-us/exchange/mail-flow-best-practices/use-connectors-to-configure-mail-flow/set-up-connectors-to-route-mail#part-2-configure-mail-to-flow-from-your-email-server-to-microsoft-365-or-office-365 – Jeremy Lakeman Jan 27 '22 at 05:37
  • Did you try this way ? https://stackoverflow.com/a/61093253/14973743 – Anand Sowmithiran Jan 27 '22 at 06:11
  • @AnandSowmithiran Yea.. still the same Issue ... – KrishD Jan 27 '22 at 11:22
  • @KrishD Please post your stack trace of the exception that you are getting. – Rahul Sharma Jan 27 '22 at 14:48
  • @RahulSharma System.Net.Mail.SmtpException: The operation has timed out. at System.Net.Mail.SmtpClient.Send(MailMessage message) – KrishD Jan 28 '22 at 04:31
  • @KrishD This error message is too generic. You need to figure out what parameter is going incorrectly to cause this issue. As my answer suggest, there can be several settings causing this. – Rahul Sharma Jan 28 '22 at 07:17
  • @RahulSharma if i try to run this inside my client network , i am getting this exception. there is nothing in the stacktrace also. But if i run the same outside the client network i am getting the below message.... The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. – KrishD Jan 28 '22 at 13:44
  • @KrishD Add: `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;` after you configure your `SMTP` client and try again. – Rahul Sharma Jan 28 '22 at 13:46
  • @RahulSharma no i added above line as well... it dint work.. :| – KrishD Jan 31 '22 at 04:42
  • @KrishD Try this: Enable Client SMTP submission on the licensed mailbox being used: From Microsoft 365 Admin Center, go to Active Users and select the user. Go to the Mail tab. In the Email apps section, select Manage email apps. Verify that the Authenticated SMTP setting is checked (enabled). Select Save changes – Rahul Sharma Jan 31 '22 at 06:06
  • @KrishD You can also try: Disable Multi-Factor Authentication (MFA) on the licensed mailbox being used: In the Microsoft 365 admin center, in the left navigation menu, choose Users > Active users. On the Active user's page, choose Multi-Factor Authentication. On the Multi-Factor Authentication page, select the user and disable the Multi-Factor Authentication status. – Rahul Sharma Jan 31 '22 at 06:07

1 Answers1

0

Basically this problem happens when you are not able to connect to your SMTP server and that is why the timeout occurs. You are getting this message on because the default Timeout value of 100 seconds is being crossed.

There could be several issue why this problem could occur i.e. Wrong SMTP address, SMTP reject, Port setting, SSL configuration etc which you need to fix.

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54