SmtpClient timeout when sending to Exchange server

506 Views Asked by At

I have a problem with mailing to a exchange server. The mails got delivered, but the SmtpClient always waits for 8 seconds and then receives a timeout exception.

I think it has something to do with the exchange mail queue, but i don't find a fix for it.

Best regards,

Michael

    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 log : http://pastebin.com/UUiNyVx8

0

There are 0 best solutions below