简体   繁体   中英

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

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 ie Wrong SMTP address, SMTP reject, Port setting, SSL configuration etc which you need to fix.

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