KrishD
KrishD

Reputation: 11

smtp client throwing error message operation has timed out

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("[email protected]"),
        Subject = this.Subject,
        Body = this.Body,
        IsBodyHtml = true
    })
    {
        msg.To.Add(new MailAddress("[email protected]"));
        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;
            }
        }
    }

Upvotes: 1

Views: 10216

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 8302

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.

Upvotes: 0

Related Questions