0

I am able to retrieve all the emails and log them but I can't seem to get the body to just be plain text. I've tried looking at other examples on here but I can't seem to grasp the problem. Below is my code so far:

//retrieve emails in blocks of 50
int offset = 1;
int pageSize = 50;
bool moreEmails = true;

ItemView view = new ItemView(pageSize, offset, OffsetBasePoint.Beginning);
view.PropertySet = PropertySet.IdOnly;

while (moreEmails)
{
    findResults = service.FindItems(WellKnownFolderName.Inbox, view);

    foreach (var item in findResults.Items)
    {
         emails.Add((EmailMessage)item);
    }

    moreEmails = findResults.MoreAvailable;

    if (moreEmails)
    {
        view.Offset += pageSize;
    }
}

PropertySet properties = new 
PropertySet (BasePropertySet.FirstClassProperties);
service.LoadPropertiesForItems(emails, properties);
properties.RequestedBodyType = BodyType.Text;

I'm initialising the variables in a separate function:

private static void CheckRules()
{
    try
    {
        foreach (var EmailParam in emails)
        {
            FromEmail = EmailParam.From.Address.ToString();
            EmailDate = EmailParam.DateTimeReceived.ToString("yyyy-MM-dd hh:mm:ss");
            EmailSubject = EmailParam.Subject.ToString();
            EmailBody = EmailParam.Body.Text.ToString();
            etc...

So again, I just want the body to be plain text. Thanks in advance for any help!

SeptimVII
  • 3
  • 3
  • What does `EmailParam.Body.Text.ToString();` produce? – IronAces Sep 11 '18 at 09:05
  • @DanielShillcock it produces all xml and text of the email body – SeptimVII Sep 11 '18 at 09:10
  • I think [this should help](https://stackoverflow.com/a/33702583/1350913) – IronAces Sep 11 '18 at 09:16
  • @DanielShillcock okay so I've been tampering and got the body text, but emails stop being collected after a certain point with the error `System.NullReferenceException: Object reference not set to an instance of an object.`. Any ideas? – SeptimVII Sep 11 '18 at 10:10
  • What line of code is that happening on? Update your question – IronAces Sep 11 '18 at 10:49
  • 1
    @DanielShillcock I got it. I had an email with no subject, and I was passing the subject as a string, but since this subject was null it was throwing the error. It's sorted now so thanks again for your initial help! – SeptimVII Sep 11 '18 at 14:45

0 Answers0