コード例 #1
0
        public void ProcessarAnexos(IEnumerable <EmailAnexosViewModel> anexos, string diretorioArquivos)
        {
            if (anexos == null)
            {
                return;
            }

            foreach (var anexo in anexos)
            {
                try
                {
                    var fi        = new FileInfo(string.Format("{0}\\{1}", diretorioArquivos, anexo.Path.Replace("||*", "\\")));
                    var novoAnexo = new EmailAnexo(fi.Extension, anexo.Nome, anexo.Path.Replace("||*", "\\"), fi.Length,
                                                   false, null, anexo.IdentificadorAnexoItem);
                    _anexos.Add(novoAnexo);
                }
                catch
                {
                    // ignored
                }
            }
        }
コード例 #2
0
        protected void ProcessarAnexos()
        {
            var contadorAnexo = 0;

            foreach (var anexo in _mensagem.Attachments)
            {
                contadorAnexo++;
                if (string.Compare(anexo.ContentType.Charset, "message/rfc822", StringComparison.Ordinal) != 0)
                {
                    var fileName = anexo.ContentDisposition?.FileName ?? anexo.ContentType.Name;

                    var extensao = ObtemExtensao(fileName.Replace("\"", "").Replace("\t", ""));
                    _nomeAnexoPadrao = "Anexo-";

                    if (!string.IsNullOrEmpty(fileName))
                    {
                        _nomeAnexoPadrao =
                            fileName.Replace("\"", "").Replace("\t", "").Replace(" ", "_").Replace("\\\\", "\\")
                            .Replace(":", "").Replace("*", "").Replace("|", "").Replace("?", "")
                            .Replace("<", "").Replace(">", "").Replace("/", "").Replace("&", "");
                    }
                    else
                    {
                        var header =
                            HttpUtility.UrlDecode(anexo.Headers["Content-Disposition"].ToString())
                            .Replace(@"attachment; filename*=UTF-8''", "").Replace("attachment", "");

                        if (!string.IsNullOrEmpty(header))
                        {
                            var contador = 1;
                            foreach (var item in header.Split(','))
                            {
                                if (contador == 1)
                                {
                                    _nomeAnexoPadrao = item.Replace("\"", "").Replace("\t", "").Replace(" ", "_")
                                                       .Replace("\\\\", "\\").Replace(":", "").Replace("*", "").Replace("|", "")
                                                       .Replace("?", "").Replace("<", "").Replace(">", "").Replace("/", "")
                                                       .Replace("&", "");
                                }

                                contador++;
                            }
                        }
                    }

                    if (_nomeAnexoPadrao == "Anexo-")
                    {
                        _nomeAnexoPadrao = _nomeAnexoPadrao + contadorAnexo + ".jpg";
                    }


                    var diretorio   = DownloadAnexo(anexo);
                    var imagemCorpo = false;
                    var contentId   = fileName.Replace("\"", "")
                                      .Replace("\t", "")
                                      .Replace(" ", "_")
                                      .Replace("\\\\", "\\");

                    try
                    {
                        var header = HttpUtility.UrlDecode(anexo.Headers["Content-Disposition"].ToString());
                        if (header.Split(';').Any(item => item.Contains("inline")))
                        {
                            imagemCorpo = true;
                        }
                    }
                    catch
                    {
                    }

                    var sType = anexo.ContentType.MediaType.ToLower();
                    if (sType.Contains("image"))
                    {
                        if (anexo.Headers["Content-ID"] != "")
                        {
                            contentId = anexo.Headers["Content-ID"];
                        }
                    }

                    var emailAnexo = new EmailAnexo(extensao, _nomeAnexoPadrao, diretorio,
                                                    anexo.ContentDisposition.Size ?? 0,
                                                    imagemCorpo, contentId, _identificadorAnexoItem.ToString());
                    _anexos.Add(emailAnexo);
                }
                else
                {
                    //try
                    //{
                    //    if (attachedMessage.Subject != null)
                    //        _nomeAnexoPadrao = attachedMessage.Subject.Replace(" ", "_") + ".eml";
                    //}
                    //catch (Exception ex)
                    //{
                    _nomeAnexoPadrao = "EmailAnexo_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day +
                                       ".eml";
                    //}

                    _anexos.Add(new EmailAnexo(".eml", _nomeAnexoPadrao, DownloadAnexo(anexo),
                                               anexo.ContentDisposition.Size ?? 0, false,
                                               null));
                }
            }

            TratarImagensEmAlternateViews();
        }
コード例 #3
0
        protected void SalvarImagensTexto(EmailFormViewModel model, string diretorioArquivos)
        {
            try
            {
                var doc1 = new HtmlDocument();
                doc1.LoadHtml(model.Html);

                foreach (var item in doc1.DocumentNode.SelectNodes("//img"))
                {
                    var att = item.Attributes["src"];

                    if (att.Value.Trim().Contains("data:"))
                    {
                        try
                        {
                            var binarioImagem =
                                att.Value.Substring((att.Value.IndexOf(",", StringComparison.Ordinal) + 1),
                                                    ((att.Value.Length - att.Value.IndexOf(",", StringComparison.Ordinal)) - 1));
                            var dirArquivo = SalvarArrayByteParaImagem(binarioImagem, diretorioArquivos,
                                                                       model.EmailIdProvisorio);

                            if (!string.IsNullOrEmpty(dirArquivo))
                            {
                                var fi        = new FileInfo(string.Format("{0}\\{1}", diretorioArquivos, dirArquivo));
                                var contentId = "@" + GetRandomString(10);
                                var novoAnexo = new EmailAnexo(fi.Extension, fi.Name, dirArquivo, fi.Length, true,
                                                               contentId);
                                _anexos.Add(novoAnexo);

                                item.SetAttributeValue("src", @"/Imagem/CorpoEmail/" + novoAnexo.IdProvisorio);
                                StringWriter sw = new StringWriter();
                                doc1.Save(sw);
                                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(sw.ToString(),
                                                                                                     Encoding.GetEncoding("utf-8"), "text/html");
                                model.Html = sw.ToString();
                            }
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    }
                    else if (att.Value.Trim().Contains("Imagem/CorpoEmail"))
                    {
                        long idEmailAnexo;
                        long.TryParse(
                            att.Value.Substring((att.Value.IndexOf("/CorpoEmail/", StringComparison.Ordinal) + 12),
                                                ((att.Value.Length - att.Value.IndexOf("/CorpoEmail/", StringComparison.Ordinal)) -
                                                 12)),
                            out idEmailAnexo);

                        if (idEmailAnexo <= 0)
                        {
                            continue;
                        }

                        var emailAnexo = _emailAnexoServico.ObterPorId(idEmailAnexo);

                        if (emailAnexo == null)
                        {
                            continue;
                        }

                        var novoAnexo = new EmailAnexo(emailAnexo.Extensao, emailAnexo.Nome, emailAnexo.Path,
                                                       emailAnexo.Tamanho, emailAnexo.ImagemCorpo, emailAnexo.ContentId);
                        _anexos.Add(novoAnexo);

                        item.SetAttributeValue("src", @"/Imagem/CorpoEmail/" + novoAnexo.IdProvisorio);
                        var sw = new StringWriter();
                        doc1.Save(sw);
                        var htmlView = AlternateView.CreateAlternateViewFromString(sw.ToString(),
                                                                                   Encoding.GetEncoding("utf-8"), "text/html");
                        model.Html = sw.ToString();
                    }
                }
            }
            catch (Exception)
            {
                //   utils.log.gravaLog(Server.MapPath("~/"), ex, "Falha capturada no script (Convert numeroTelefone) " + HttpContext.Current.Request.Url.AbsolutePath);
            }
        }