0

I have code that read email attachments, embedded and not embedded, but if have both in the same email, only read embedded attachment.

Any help will be appreciated..

My code.

{
        string strFiltro = String.Format("SINCE {0}-{1}-{2} BEFORE {3}-{4}-{5} {6} ",
                                      strDiaInicial,
                                      strMesInicial,
                                      strAnioInicial,
                                      strDiaFinal,
                                      strMesFinal,
                                      strAnioFinal,
                                      (this.MensajesNoLeidos == true ? "UNSEEN" : "ALL"));

        IEnumerable<Message> mensajesFiltrados = folder.Search(strFiltro/*MessageFetchMode.Full,10*/);
        mensajesFiltrados = mensajesFiltrados.Where(x => x.Attachments.Count() > 0 || x.EmbeddedResources.Count() > 0);

        foreach (Message message in mensajesFiltrados)
        {
            if (message.Attachments.Count() > 0)
            {
                ArchivosAdjuntosFiltrados = message.Attachments.Where(x => x.ContentType.Name.ToLower().Contains(".pdf")) x.ContentType.Name.ToLower().Contains(".xml"));
                resultado[0] = resultado[0] + ArchivosAdjuntosFiltrados.Count();
            }
            else
            {
                if (message.EmbeddedResources.Count() > 0)
                {
                    //never get Embedded pdf or xml files ArchivosAdjuntosFiltrados = message.EmbeddedResources.Where(x => x.ContentType.Name.Contains(".pdf") || x.ContentType.Name.Contains(".xml"));

                }
            }
        }
        foreach (Attachment atachemtn in ArchivosAdjuntosFiltrados)
        {
            strNombreArchivo = atachemtn.FileName;
            String attachData = atachemtn.GetTextData().Replace('-', '+'); attachData = attachData.Replace('_', '/');

            if (intPosicion > 0)
            {
                attachData = attachData.Substring(0, attachData.Length - 31);
            }

            if (strNombreArchivo != String.Empty)
            {
                if (File.Exists(Path.Combine(strRutaDescargas, atachemtn.FileName)) == true)
                {
                    if (pBlnSobreescribirArchivos == true)
                    {

                        byte[] data = Convert.FromBase64String(attachData);
                        File.WriteAllBytes(Path.Combine(this.strRutaDescargas, strNombreArchivo), data);
                    }
                }
            }
        }
    }
}
Dai
  • 141,631
  • 28
  • 261
  • 374
KlingCan
  • 29
  • 5
  • 1
    Please reformat your code so it's actually readable. – Dai Jun 23 '21 at 13:34
  • i just fixed the problem, any help will be appreciated – KlingCan Jun 23 '21 at 13:52
  • 1
    What library or API are you using to go through emails? I don't recognize it. – Dai Jun 23 '21 at 14:08
  • using ImapX; using ImapX.Enums; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using Microsoft.VisualBasic; – KlingCan Jun 24 '21 at 15:46

0 Answers0