Search code examples
chilkat-email

Email body returns a blank or null in chilkat mail


I have the following code that reads a bunch of emails from a folder. It gets the emails fine, as I can see the number of emails are correct and the header is correct as well. However it returns nothing for the body. I do have a email with simple text as well but that too does not return anything. Part of the code is below. any help is appreciated. the account I am reading is from a office 365 and i used the imap.Connect (*i can read these emails from the Microsoft Outlook, so i think the issue is with my chilkat code)

        Chilkat.MessageSet messageSet = imap.Search(@"ALL", fetchUids);
        if (imap.LastMethodSuccess == false)
        {
            txtOutput.Text = txtOutput.Text + Environment.NewLine + imap.LastErrorText;
            return;
        }

        // Fetch the email headers into a bundle object:
        Chilkat.EmailBundle bundle = imap.FetchHeaders(messageSet);
        if (imap.LastMethodSuccess == false)
        {
            txtOutput.Text = txtOutput.Text + Environment.NewLine + imap.LastErrorText;
            return;
        }

        // Sort the email bundle by date, recipient, sender, or subject:
        bool ascending = true ; bool descending = true;
        bundle.SortByDate(ascending);
        //bundle.SortByDate(descending);

        // To sort by recipient, sender, or subject, call 
        // SortBySender, SortByRecipient, or SortBySubject.

        // Display the Subject and From of each email.
        int i = 0;
        string body="";
        while (i < bundle.MessageCount)
        {
            Chilkat.Email email = null;
            email = bundle.GetEmail(i);

            txtOutput.Text = txtOutput.Text + Environment.NewLine + Environment.NewLine + email.GetHeaderField("Date");
            //Debug.WriteLine(email.GetHeaderField("Date"));
            //Debug.WriteLine(email.Subject);
            txtOutput.Text = txtOutput.Text + Environment.NewLine + email.From;
            //Debug.WriteLine(email.From);
            //Debug.WriteLine("--");

            if (email.HasHtmlBody())
            {
                body = email.GetHtmlBody() + String.Empty;
            }
            else if (email.HasPlainTextBody()) 
            {
                body = email.GetPlainTextBody() + String.Empty;
            }
            else
            {
                body = email.Body + String.Empty; 
            }
            txtOutput.Text = txtOutput.Text + Environment.NewLine + body.Trim() ;
            i = i + 1;
        }

Solution

  • You downloaded headers-only:

    Chilkat.EmailBundle bundle = imap.FetchHeaders(messageSet);

    Of course there is no body...