public bool ProcessMail(SendableMail mail) { var success = true; foreach (var connInfo in GroupByHost(mail)) { var addresses = connInfo.Addresses.Select(a => a.Address).ToArray(); var connector = connInfo.Connector; var client = SMTPClient.Create(_container, connector, connInfo.Host, connInfo.Port); if (!client.Connect()) { success = false; TriggerMailError(mail, connInfo, client.LastStatus, client.LastException); continue; } if (!client.SendMail(mail.From.Address, addresses, mail.ToString())) { success = false; TriggerMailError(mail, connInfo, client.LastStatus, client.LastException); } client.Close(); // TODO } return(success); }
public void HandleMailError(SendableMail mail, MessageProcessor.ConnectorInfo info, SMTPStatusCode?status, Exception e) { var connector = info.Connector; if (status == SMTPStatusCode.NotAvailiable) { if (mail.RetryCount < connector.RetryCount) { Logger.InfoFormat("Remote host was not availiable, retrying to send mail in {0} (try {1}/{2})", connector.RetryTime, mail.RetryCount + 1, connector.RetryCount + 1); mail.RetryCount++; lock (_mailQueue) { _mailQueue.Enqueue(mail, connector.RetryTime); } } else { Logger.InfoFormat("Remote host was not availiable, but retry count exceeded (try {0}/{0}).", connector.RetryCount + 1); } } if (e != null) { Logger.Error("An error occured while sending a mail:", e); } // TODO }
protected virtual void TriggerMailError(SendableMail mail, ConnectorInfo info, SMTPStatusCode?status = null, Exception e = null) { var handler = OnMailError; if (handler != null) { handler(mail, info, status, e); } }
public void Enqueue(MailMessage mail) { var from = new MailAddress(mail.Sender); var to = mail.Recipients.Select(r => new MailAddress(r)).ToArray(); var content = new MailContent(mail.Subject, from, mail.Html, mail.Text); foreach (var recipient in to) { content.AddRecipient(recipient); } var parsed = new Mail(from, to, content.ToString()); var sendableMail = new SendableMail(parsed, mail.Connector); _mailQueue.Enqueue(sendableMail, TimeSpan.Zero); }
private IEnumerable <ConnectorInfo> GroupByHost(SendableMail mail) { foreach (var recipientGroup in mail.Recipients .GroupBy(r => r.Host)) { var host = recipientGroup.Key; var connector = mail.Settings as ISendConnector ?? sendConnectors.GetByDomain(host) ?? sendConnectors.DefaultConnector; string remoteHost; int remotePort; if (!connector.UseSmarthost) { var records = new DnsStubResolver().Resolve <MxRecord>(recipientGroup.Key); var record = records.OrderBy(r => r.Preference).FirstOrDefault(); if (record == null) { TriggerMailError(mail, new ConnectorInfo { Connector = connector, Host = recipientGroup.Key, Port = 25, Addresses = recipientGroup }, null, new NoMailHostFoundException(recipientGroup.Key)); continue; } remoteHost = record.ExchangeDomainName.ToString(); remotePort = 25; } else { remoteHost = connector.RemoteAddress.ToString(); remotePort = connector.RemotePort; } yield return(new ConnectorInfo { Connector = connector, Host = remoteHost, Port = remotePort, Addresses = recipientGroup }); } }
protected virtual void WorkerCallback() { while (true) { try { SendableMail mail = null; lock (_mailQueue) { if (_mailQueue.Peek() != null) { mail = _mailQueue.Dequeue(); } } if (mail != null) { var success = _processor.ProcessMail(mail); if (mail.ResultHandler != null) { mail.ResultHandler(success); } } Thread.Sleep(TickDefaultMilliseconds); if (_askStop.WaitOne(0, true)) { _informStopped.Set(); break; } } catch (Exception e) { Logger.Error("An exception occured while sending a mail.", e); } } RaiseFinishedEvent(); }
private void OnMailTimer(object state) { try { var time = DateTime.Now; var sendEicar = _timeTable.SendEicarFile && new Random().NextDouble() <= EicarProbability; var from = GetFrom(); var to = GetRecipients(); var fromMail = new MailAddress(from); var toMail = to.Select(r => new MailAddress(r)).ToArray(); var content = CreateContent(fromMail, toMail, sendEicar); var sendableMail = new SendableMail(fromMail, toMail, content.ToString(), null) { ResultHandler = success => { var evt = new MailEvent { Eicar = sendEicar, Success = success, Time = time }; lock (_events) { _events.Add(evt); } if (success) { _timeTables.IncreaseSuccessMailCount(_timeTable.Id); } else { _timeTables.IncreaseErrorMailCount(_timeTable.Id); } } }; if (_timeTable.ProtocolLevel > ProtocolLevel.Off) { Logger.InfoFormat("Timetable '{0}' enqueued a mail.", _timeTable.Name); if (_timeTable.ProtocolLevel == ProtocolLevel.Verbose) { Logger.InfoFormat("From: {0}, To: {1}", from, string.Join(", ", to)); } } _queue.Enqueue(sendableMail, TimeSpan.Zero); } catch (Exception e) { Logger.Error( string.Format("Timetable '{0}': An exception occured while sending a mail.", _timeTable.Name), e); } finally { _timer.Change(GetWaitTime(), TimeSpan.Zero); } }