Exemplo n.º 1
0
        /// <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();
            }
        }
Exemplo n.º 2
0
        public void Send(IList <string> tos, string subject, string body, Encoding subjectEncoding, Encoding bodyEncoding, bool isHtmlBody, string attachmentPath)
        {
            var packageId = GeneratorPackgeId.GetPakcageId();

            EmitterPackageData data = new EmitterPackageData(packageId, tos, subject, body);

            data.SubjectEncoding = subjectEncoding;

            data.BodyEncoding = bodyEncoding;

            data.IsBodyHtml = isHtmlBody;

            data.AttachmentPath = attachmentPath;

            this._dataQueue.Enqueue(data);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 释放
        /// </summary>
        private IList <SendResultEventArgs> GetSurplusData(SendResult result)
        {
            lock (_synState)
            {
                var count = _dataQueue.Count;

                IList <SendResultEventArgs> args = new List <SendResultEventArgs>();
                while (count > 0)
                {
                    EmitterPackageData data = null;
                    if (_dataQueue.TryDequeue(out data))
                    {
                        args.Add(new SendResultEventArgs(TagName, data, result, ""));
                    }
                    count--;
                }
                return(args);
            }
        }
Exemplo n.º 4
0
        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);
            }
        }