简体   繁体   English

smtp 客户端抛出错误消息操作已超时

[英]smtp client throwing error message operation has timed out

I am using SmtpClient with office365 mailserver to send email.我正在使用带有 office365 邮件服务器的 SmtpClient 来发送 email。 But everytime i try smtpclient.Send(msg),it will throw 'Operation has timed out' smtpexception.但是每次我尝试 smtpclient.Send(msg) 时,它都会抛出 '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.我已经尝试了所有早期的选项,比如将端口更改为 587 并增加超时值,但没有任何效果。 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.基本上,当您无法连接到 SMTP 服务器时,就会发生此问题,这就是超时发生的原因。 You are getting this message on because the default Timeout value of 100 seconds is being crossed.您收到此消息是因为超过了 100 秒的默认超时值。

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.可能有几个问题导致此问题发生,即错误的 SMTP 地址、SMTP 拒绝、端口设置、SSL 配置等您需要修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM