1
votes

I'm getting emails from Outlook Inbox to display on an Email Search Screen, based on filters (Sender, Subject, Message).

I'm able to get all but the body of the E-mail:

OutlookRepository.cs:

public FindItemsResults<Item> GetEmails(string loginUsuario, string de, string assunto, string conteudo, WellKnownFolderName pasta = WellKnownFolderName.Inbox)
        {
            ExchangeService service = GetExchangeService(loginUsuario);

            var inbox = Folder.Bind(service, WellKnownFolderName.Inbox);

            SearchFilter sf = null;

            // Search by Sender
            if (!string.IsNullOrWhiteSpace(de))
                sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.ContainsSubstring(EmailMessageSchema.From, de));

            // Search by Subject
            if (!string.IsNullOrWhiteSpace(assunto))
                sf = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, assunto));

            // Search by Body
            if (!string.IsNullOrWhiteSpace(conteudo))
                sf = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, conteudo));

            var view = new ItemView(100);

            var emails = service.FindItems(pasta, sf, view);

            return emails;
        }

On return "emails", Body throws a exception: Body = '(new System.Linq.SystemCore_EnumerableDebugView(emails).Items[0]).Body' threw an exception of type 'Microsoft.Exchange.WebServices.Data.ServiceObjectPropertyException'

EmailController.cs:

public JsonResult PesquisarEmails(string de, string assunto, string conteudo)
        {
            FindItemsResults<Item> emails = ServiceFactory.GetService<IComunicacaoService>(true).RecuperarEmails(UsuarioAutenticado.Funcionario.Login, de, assunto, conteudo);

            var lista = new List<EmailFormatado>();

            foreach (Item email in emails)
            {
                lista.Add(new EmailFormatado()
                { 
                    DataEmail = email.DateTimeReceived.FormatarDataCompleta(),
                    Remetente = (email as EmailMessage).From.Address,
                    AssuntoEmail = email.Subject,
                    Descricao = email.TextBody
            });
            }

            return Json(lista, JsonRequestBehavior.AllowGet);
        }

Tryed to use "email.TextBody" and "email.Body" but it's throws a exception: email.TextBody = 'email.TextBody' threw an exception of type 'Microsoft.Exchange.WebServices.Data.ServiceObjectPropertyException'

Any help?

1

1 Answers

0
votes

The findItem operation only returns a subset of Item properties and the Body isn't returned (the from.address also isn't) see https://github.com/MicrosoftDocs/office-developer-exchange-docs/blob/master/docs/exchange-web-services/email-properties-and-elements-in-ews-in-exchange.md so what you will need in your code is either a Load of each Item (bad idea from a performance perspective) or a LoadPropertiesFromItems eg something like

service.LoadPropertiesForItems(emails, PropertySet.FirstClassProperties);