Search code examples
c#openpop

OpenPop trouble reading body of email


I am having trouble processing OpenPop to read the body of an email.

I think the problem is that I don't know how to handle images.

My minimal code is:

for (int i = messageCount; i > 0; i--)
                {
                    ProcessMessage(client.GetMessage(i)); //the standard call in all examples
                    client.DeleteMessage(i);
                }

public static void ProcessMessage(Message msg)
        {
            //Parse Message
            string from = msg.Headers.From.Address;
            string subject = msg.Headers.Subject;
            string body = "";
            DateTime received = msg.Headers.DateSent;

            if (msg.MessagePart.IsMultiPart == false)
            {
                body = msg.MessagePart.GetBodyAsText();
            }
            else
            {
                for (int i = 0; i < msg.MessagePart.MessageParts.Count; i++)
                {
                    MessagePart part = msg.MessagePart.MessageParts[i];
                    body += part.GetBodyAsText();  **//This is my error line**
                }
            }

I am getting an error that byte array cannot be null. I don't understand what I am missing, but this error occurs when there are images in the body (usually in signature).


Solution

  • This was the key to solving my problem:

    string body = msg.FindFirstPlainTextVersion().GetBodyAsText();