How do I access the email body in code?
- Hi
I am trying to access an exchange mailbox and read the contents of an email. I'm currently using this code to retrieve the mail message ID and subject, but the FindTitems comment doesn't support returning the message body.
How do I access the message body?
Thanks
Peter
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Exchange.WebServices.Data; using System.Net; namespace EmailTestC2 { class Program { static void Main(string[] args) { ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); // Setting credentials is unnecessary when you connect from a computer that is // logged on to the domain. //service.Credentials = new WebCredentials("username", "password", "domain"); service.Url = new Uri("https://<SERVERNAME>/EWS/Exchange.asmx"); ItemView view = new ItemView(10); view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending); view.PropertySet = new PropertySet( BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived); FindItemsResults<Item> findResults = service.FindItems( WellKnownFolderName.Inbox, new SearchFilter.SearchFilterCollection( LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Weather"), new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Warning")), view); Console.WriteLine("Total number of items found: " + findResults.TotalCount.ToString()); foreach (Item item in findResults) { // Do something with the item. Console.WriteLine("ID:" + item.Id.ToString()); Console.WriteLine("Subject:" + item.Subject.ToString()); } } } }
解答
- The Body property cannot be returned by FindItems. To load properties for multiple items in a single call to EWS, the most efficient way is ExchangeService.LoadPropertiesForItems. Here's an example of how to load the Body property for all items in findResults:
service.LoadPropertiesForItems(findResults, new PropertySet(ItemSchema.Body));
Regards,
Kim
Programming Writer, Microsoft Exchange Developer Documentation Team- 已提議為解答Kim BrandlMSFT, 版主2009年11月20日 下午 06:24
- 已標示為解答Kim BrandlMSFT, 版主2010年1月27日 下午 10:11
所有回覆
- The Body property cannot be returned by FindItems. To load properties for multiple items in a single call to EWS, the most efficient way is ExchangeService.LoadPropertiesForItems. Here's an example of how to load the Body property for all items in findResults:
service.LoadPropertiesForItems(findResults, new PropertySet(ItemSchema.Body));
Regards,
Kim
Programming Writer, Microsoft Exchange Developer Documentation Team- 已提議為解答Kim BrandlMSFT, 版主2009年11月20日 下午 06:24
- 已標示為解答Kim BrandlMSFT, 版主2010年1月27日 下午 10:11
- Hi
Thanks for the reply.
I added the line above to my code just after the FindItemsResults<Item> line.
When I run the code in debug I get the following error. Looking at the 'The requesting account does not have permission to serialize tokens' line, I'm assuming that there is something not configured correctly on our server, or my account, to let me use this.
Any suggestions what I should ask our admins to looking setting up to allow this.
Peter
Microsoft.Exchange.WebServices.Data.ServiceResponseException was unhandled
Message="System.Web.Services.Protocols.SoapException: The requesting account does not have permission to serialize tokens.\r\n at Microsoft.Exchange.Services.RequestSoapHeaderServiceExtension.ProcessProxySecurityContext(SoapUnknownHeader header, AuthZClientInfo callerClientInfo)\r\n at Microsoft.Exchange.Services.RequestSoapHeaderServiceExtension.ProcessSoapHeaders(SoapMessage message, Object responsibleObject)\r\n at Microsoft.Exchange.Services.ServiceExtensionManager.DoAfterDeserializeRequest(SoapMessage message)\r\n at Microsoft.Exchange.Services.ServiceExtensionManager.<>c__DisplayClass1.<ProcessMessage>b__0()\r\n at Microsoft.Exchange.Common.IL.ILUtil.DoTryFilterCatch(TryDelegate tryDelegate, FilterDelegate filterDelegate, CatchDelegate catchDelegate)\r\n at Microsoft.Exchange.Diagnostics.ExWatson.SendReportOnUnhandledException(MethodDelegate methodDelegate, IsExceptionInteresting exceptionInteresting, Boolean terminating)\r\n at Microsoft.Exchange.Services.Core.ServiceDiagnostics.TraceErrorOnUnhandledException(MethodDelegate methodDelegate)\r\n at System.Web.Services.Protocols.SoapMessage.RunExtensions(SoapExtension[] extensions, Boolean throwOnException)\r\n at System.Web.Services.Protocols.SoapServerProtocol.CreateServerInstance()\r\n at System.Web.Services.Protocols.WebServiceHandler.Invoke()\r\n at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()"
Source="Microsoft.Exchange.WebServices"
StackTrace:
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(WebException webException)
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.InternalExecute()
at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalLoadPropertiesForItems(IEnumerable`1 items, PropertySet propertySet, ServiceErrorHandling errorHandling)
at Microsoft.Exchange.WebServices.Data.ExchangeService.LoadPropertiesForItems(IEnumerable`1 items, PropertySet propertySet)
at EmailTestC2.Program.Main(String[] args) in C:\Development\EmailTestC2\EmailTestC2\Program.cs:line 43
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: - Hi
Does anyone have any ideas how to resolve this error?
Thanks
Peter - Hi Peter,
My knowledge of server configuration is fairly limited, so unfortunately I'm not in a position to be able to answer your question. However, you may want to have a look at the Inside Microsoft Exchange Server 2007 Web Services book -- chapter 19 "Server to Server Authentication" contains some info about Token Serialization that may help shed some light on your situation.
Regards,
Kim Brandl
Programming Writer, Microsoft Exchange Developer Documentation Team