Reputation: 475
I have a mailbox that receives an automated email every 5 mins from a remote station. contained within that email is a string that needs to be compared with the same string from the previous email.
I am trying to automate this process for obvious reasons.
So far i am able to read the ConversationTopic
of the emails, however, i can't seem to figure out how to read the content of the emails.
when it call this:
email.Load();
MessageBox.Show(email.TextBody.Text.ToString());
i get the following error:
You must load or assign this property before you can read its value
I have had a google and i can't find anything that relates to my instance, so any help would be great.
This is my full code so far:
private void Form1_Load(object sender, EventArgs e)
{
try
{
//MessageBox.Show("Registering Exchange connection");
_service = new ExchangeService
{
Credentials = new WebCredentials("[email protected]", "*****")
};
}
catch
{
MessageBox.Show("new ExchangeService failed.");
return;
}
// This is the office365 webservice URL
_service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
// Prepare seperate class for writing email to the database
try
{
//MessageBox.Show("Reading mail");
// Read 100 mails
foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(10)))
{
if (email.ConversationTopic.ToString().Contains("from RockBLOCK 300234066454740"))
{
email.Load();
MessageBox.Show(email.TextBody.Text.ToString());
}
}
MessageBox.Show("Exiting");
}
catch (Exception ex)
{
MessageBox.Show("An error has occured. \n:" + ex.Message);
}
}
Upvotes: 4
Views: 3471
Reputation: 5173
The exception is thrown because you're trying to read the property Item.TextBody
. This property is not a first-class email property.
The docs say:
Not all important email properties and elements are first-class properties and elements. To get the other properties or elements, you need to add them to your
PropertySet
if you're using the EWS Managed API, or use a property path to add them to your EWS operation call. For example, to retrieve the text body ... , create yourPropertySet
...
In your case:
email.Load(new PropertySet(EmailMessageSchema.ConversationTopic, ItemSchema.TextBody));
Using this request EWS will load and return an EmailMessage
with the two properties from the PropertySet
.
NOTE:
By specifying a PropertySet
with the properties you need to work with, EWS may process your request faster since it has not to search for all first-class-email properties. Moreover you will not run in such an error, where you're trying to read a property, which isn't a member of the first-class-email properties.
Upvotes: 9