Exemplo n.º 1
0
        private TOEmail _composeEmailAlert(string emailFrom, string emailTo, string emailCc, string emailCco, string subject)
        {
            TOEmail ret = new TOEmail();

            ret.Msg.From = emailFrom;
            if (!string.IsNullOrEmpty(emailTo))
            {
                ret.Msg.To = emailTo;
            }
            if (!string.IsNullOrEmpty(emailCc))
            {
                ret.Msg.Cc = emailCc;
            }
            if (!string.IsNullOrEmpty(emailCco))
            {
                ret.Msg.Cco = emailCco;
            }
            if (!string.IsNullOrEmpty(subject))
            {
                ret.Msg.Subject = subject;
            }

            ret.Msg.IsBodyHtml = false;
            return(ret);
        }
Exemplo n.º 2
0
 private void _queueEmail()
 {
     try
     {
         while (_isRunning)
         {
             TOEmail to = null;
             if (_cqEmail.TryDequeue(out to))
             {
                 if (to != null)
                 {
                     this._processEmail(to);
                 }
             }
             else
             {
                 lock (_syncEmail)
                 {
                     Monitor.Wait(_syncEmail, 200);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         logger.Error("Erro no envio do email: " + ex.Message, ex);
     }
 }
Exemplo n.º 3
0
        private void _processEmail(TOEmail email)
        {
            EmailNotaCorretagemInfo info = new EmailNotaCorretagemInfo();

            try
            {
                // Inserir no BD
                info.ArquivoNota       = email.Msg.FileAttach;
                info.Bolsa             = email.Exchange;
                info.DtRegistroEmail   = DateTime.Now;
                info.EmailDestinatario = email.Msg.To;
                if (!string.IsNullOrEmpty(email.Msg.Cc))
                {
                    info.EmailDestinatarioCc = email.Msg.Cc;
                }
                if (!string.IsNullOrEmpty(email.Msg.Cco))
                {
                    info.EmailDestinatarioCco = email.Msg.Cco;
                }
                info.Assunto     = email.Msg.Subject;
                info.Body        = email.Msg.Body;
                info.EmailOrigem = email.Msg.From;
                info.Status      = email.Status;
                info.DescStatus  = email.DescStatus;

                if (!string.IsNullOrEmpty(email.IdCliente))
                {
                    info.IdCliente = Convert.ToInt32(email.IdCliente);
                }

                if (!email.Status.Equals(StatusInfo.ERRO) && !string.IsNullOrEmpty(email.Msg.To))
                {
                    logger.DebugFormat("===> SendEmail: IDCliente[{0}] Emails:[{1}] Subject:[{2}]", info.IdCliente, email.Msg.To, email.Msg.Subject);
                    SpiderMail.SendEmail(_cfg, email.Msg);
                }
                else
                {
                    logger.DebugFormat("===> SendEmail: Email com ERRO ou destinatario vazio. IDCliente[{0}], Emails:[{1}] Subject:[{2}]", info.IdCliente, email.Msg.To, email.Msg.Subject);
                }
                if (_db != null)
                {
                    _db.InserirEmailNotaCorretagem(info);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Erro no envio dos emails: " + ex.Message, ex);
                info.Status     = StatusInfo.ERRO;
                info.DescStatus = ex.Message;
                if (_db != null)
                {
                    _db.InserirEmailNotaCorretagem(info);
                }
            }
        }
Exemplo n.º 4
0
        private TOEmail _composeEmailMsg(string emailFrom, string emailTo, string file, string exchange, string idCliente, FileWatcherConfigItem cfg, Dictionary <int, List <string> > dicEmails = null)
        {
            TOEmail ret = new TOEmail();

            try
            {
                string subjectBrokerage;
                if (!string.IsNullOrEmpty(cfg.TemplateFile) && string.IsNullOrEmpty(_templateContent))
                {
                    _templateContent = File.ReadAllText(cfg.TemplateFile);
                }
                subjectBrokerage   = cfg.SubjectEmail.Replace("#clientid#", idCliente);
                ret.IdCliente      = idCliente;
                ret.Exchange       = exchange;
                ret.Msg.From       = emailFrom;
                ret.Msg.To         = emailTo;
                ret.Msg.FileAttach = file;
                ret.Msg.Subject    = subjectBrokerage;
                if (!string.IsNullOrEmpty(_templateContent))
                {
                    ret.Msg.Body       = _templateContent;
                    ret.Msg.IsBodyHtml = true;
                }

                logger.DebugFormat("===> Email Message Compose: From:[{0}] To:[{1}] Subject:[{2}]", ret.Msg.From, ret.Msg.To, ret.Msg.Subject);

                // Verificacao do ClientID com Assunto, anexo e emails gerados.
                if (cfg.ClientIdCheck)
                {
                    int aux;
                    if (int.TryParse(ret.IdCliente, out aux))
                    {
                        if (aux != 0)
                        {
                            // Verificar se IdCliente esta presente no assunto
                            if (ret.Msg.Subject.IndexOf(ret.IdCliente) < 0)
                            {
                                throw new Exception("Client ID is not present at email subject");
                            }

                            // Verificar se IdCliente esta no anexo
                            string fileAux = ret.Msg.FileAttach.Substring(ret.Msg.FileAttach.LastIndexOf("\\"));
                            if (fileAux.IndexOf(ret.IdCliente) < 0)
                            {
                                throw new Exception("Client ID is not present at file name");
                            }

                            // Verificar se os emails da mensagem correspondem ao cliente
                            if (dicEmails == null)
                            {
                                throw new Exception("Email collection is null");
                            }

                            List <string> lst = null;
                            if (dicEmails.TryGetValue(aux, out lst))
                            {
                                foreach (string email in lst)
                                {
                                    if (ret.Msg.To.IndexOf(email, StringComparison.CurrentCultureIgnoreCase) < 0)
                                    {
                                        throw new Exception("Email differs from collection");
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Unable to parse ClientId to int to proceed check");
                    }
                }



                // Verificacao de testes: se os parametros destinatarios de emails estao definidos no config do servico, irao
                // sobrescrever os sets vindo dos parametros
                if (!string.IsNullOrEmpty(_emailTo))
                {
                    ret.Msg.Subject = string.Format("{0} - [{1}]", ret.Msg.Subject, ret.Msg.To);
                    ret.Msg.To      = _emailTo;
                }
                if (!string.IsNullOrEmpty(_emailCc))
                {
                    ret.Msg.Cc = _emailCc;
                }
                if (!string.IsNullOrEmpty(_emailCco))
                {
                    ret.Msg.Cco = _emailCco;
                }

                ret.Status = StatusInfo.OK;
            }
            catch (Exception ex)
            {
                logger.Error("Problemas na composicao da mensagem de Nota de Corretagem: " + ex.Message, ex);
                ret = null;
            }
            return(ret);
        }
Exemplo n.º 5
0
        public void ProcessEmailPath(string path, FileWatcherConfigItem cfg)
        {
            try
            {
                StringBuilder sbAlert = new StringBuilder();
                _composeMessageAlert(ref sbAlert, "Diretorio a processar: " + path, 1);

                // Buscando os emails
                Dictionary <int, List <string> > dicEmails = new Dictionary <int, List <string> >();
                logger.Info("Buscando Emails: " + cfg.Exchange);
                switch (cfg.Type)
                {
                case TypeWatcher.BMF:
                    dicEmails = new DbEmailOracle().BuscarClienteBmf();
                    break;

                case TypeWatcher.BOVESPA:
                    dicEmails = new DbEmailOracle().BuscarClienteBovespa();
                    break;

                case TypeWatcher.POSICAO_BMF:
                    dicEmails = new DbEmail().BuscarPosicaoClienteEmail();
                    break;
                }
                if (dicEmails == null || dicEmails.Count == 0)
                {
                    logger.Error("Problemas na consulta dos emails dos clientes...");
                    return;
                }
                logger.InfoFormat("Fim busca Emails. Tipo [{0}]. Accounts: [{1}] ", cfg.NameType, dicEmails.Count);



                // Ler o diretorio com os arquivos
                if (Directory.Exists(path))
                {
                    string[] arqNames = Directory.GetFiles(path);
                    logger.Info("Numero de Arquivos: " + arqNames.Length);
                    foreach (string arqName in arqNames)
                    {
                        int idClient = this._extractIdCliente(arqName);
                        if (idClient == 0)
                        {
                            _composeMessageAlert(ref sbAlert, "ERRO. Não foi possivel extrair  IdCliente", 2);
                            logger.Error("ERRO. Impossivel buscar cliente");
                            continue;
                        }

                        string emailTo = string.Empty;
                        if (!this._getEmail(idClient, dicEmails, ref emailTo))
                        {
                            _composeMessageAlert(ref sbAlert, "Email inexistente para cliente :" + idClient, 2);
                        }
                        else
                        {
                            string msg = string.Format("FileName: [{0}] IdCliente: [{1}] Email: [{2}]", arqName, idClient.ToString("D8"), emailTo);
                            _composeMessageAlert(ref sbAlert, msg, 1);
                            TOEmail toMsg = _composeEmailMsg(_emailFrom, emailTo, arqName, cfg.Exchange, idClient.ToString(), cfg, dicEmails);
                            this.AddEmail(toMsg);
                        }
                    }
                }
                else
                {
                    _composeMessageAlert(ref sbAlert, "Diretorio nao existe!!", 2);
                }

                // Gerando mensagem de processamento
                TOEmail toEmailAlert = this._composeEmailAlert(_emailFrom, _emailAlert, _emailCc,
                                                               _emailCco, string.Format("{0} - Processamento de Arquivo", cfg.NameType));
                toEmailAlert.Msg.Body = sbAlert.ToString();
                this.AddEmail(toEmailAlert);
            }
            catch (Exception ex)
            {
                logger.Error("Problemas no processamento do diretorio de arquivos: " + path + " " + ex.Message, ex);
            }
        }
Exemplo n.º 6
0
 public void AddEmail(TOEmail email)
 {
     _cqEmail.Enqueue(email);
     lock (_syncEmail)
         Monitor.Pulse(_syncEmail);
 }