发送到Exchange服务器时SmtpClient超时

时间:2014-11-25 14:26:23

标签: c# timeout exchange-server smtpclient

我在邮寄到Exchange服务器时遇到问题。 邮件已发送,但SmtpClient始终等待8秒,然后收到超时异常。

我认为它与Exchange邮件队列有关,但我找不到它的修复程序。

致以最诚挚的问候,

迈克尔

    public void SendMessage(MailMessage message, string guid)
    {
        if(!ValidSettings())
        {
            return;
        }
        if(!string.IsNullOrEmpty(_bccEmail))
        {
            message.Bcc.Add(_bccEmail);
        }
        message.From = new MailAddress(_emailFrom, _weergaveNaam);

        bool error = true;
        int tried = 0;
        while(error && tried < _maxRetry)
        {
            if(_smtpClient == null)
            {
                _smtpClient = new SmtpClient(_serverUrl, _serverPort) {Credentials = new NetworkCredential(_serverUser, _serverPassword), EnableSsl = _useSsl};
            }

            using(UnitOfWork uow = new UnitOfWork())
            {
                tried++;
                EmailLog log = new EmailLog(uow) {Guid = guid, From = message.From.Address};
                StringBuilder sb = new StringBuilder();

                foreach(MailAddress mailAddress in message.To)
                {
                    if(sb.Length > 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(mailAddress.Address);
                }

                log.To = sb.ToString();
                sb.Clear();

                foreach (MailAddress mailAddress in message.Bcc)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(mailAddress.Address);
                }

                log.Bcc = sb.ToString();
                sb.Clear();

                foreach (MailAddress mailAddress in message.CC)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(mailAddress.Address);
                }

                log.Cc = sb.ToString();
                sb.Clear();

                log.Subject = message.Subject;

                try
                {
                    _smtpClient.Timeout = 8000;
                    _smtpClient.Send(message);
                    log.EmailStatus = EmailStatusEnum.Succes;
                    error = false;
                }
                catch(SmtpException ex)
                {
                    log.EmailStatus = EmailStatusEnum.Error;
                    log.ServerMessage = ex.Message;
                    log.ServerCode = ex.StatusCode.ToString();
                    error = true;
                }
                catch(Exception ex)
                {
                    log.EmailStatus = EmailStatusEnum.Error;
                    log.ServerMessage = ex.Message;
                    error = true;
                }
                finally
                {
                    log.Save();
                    uow.CommitChanges();
                }
            }
        }

        message.Dispose();
    }

Echange日志: http://pastebin.com/UUiNyVx8

0 个答案:

没有答案