/// <summary> /// 执行 /// </summary> /// <returns></returns> public void Excute() { lock (_synState) { while (!_dataQueue.IsEmpty) { EmitterPackageData data = null; SendResultEventArgs args = null; Exception exception = null; SendResult result = SendResult.Success; try { if (_dataQueue.TryDequeue(out data)) { if (_smtpClient == null) { Connect(); } var mailMessage = data.GetSmtpMessage(_emailConfig.Account); _smtpClient.Send(mailMessage); args = new SendResultEventArgs(TagName, data, SendResult.Success, "成功"); } } catch (FormatException e) { exception = e; result = SendResult.Args; } catch (SmtpException e) { exception = e; result = SendResult.Smtp; } catch (Exception e) { exception = e; result = SendResult.Ohter; } finally { InvokeComplete(data, result, exception); if (result == SendResult.Ohter || result == SendResult.Smtp) { Stop(); } } } //关闭链接 CloseConnect(); } }
private void InvokeComplete(EmitterPackageData data, SendResult result, Exception e) { if (onSendComplete != null) { SendResultEventArgs item = new SendResultEventArgs(TagName, data, result, e); IList <SendResultEventArgs> args = null; if (result == SendResult.Ohter || result == SendResult.Smtp) { args = this.GetSurplusData(result); } else { args = new List <SendResultEventArgs>(); } args.Add(item); onSendComplete.BeginInvoke(this, args.ToArray(), null, null); } }