How to read Body of POP3 outlook account with MAPI .pst using C#

95 Views Asked by At

I am trying to write a program that connects to a POP3 Outlook account (the account is on a different computer than the one I am coding on) with MAPI setup in a personal sotrage folder (.pst). I am trying to parse emails that are only in specific folders but since the MAPI doesn't synch the folders all the emails display in 'One folder'. I included IF statements to filter out the emails I want and I am able to read the header, From, Subject of the emails I want. The emails that are not part of that "specific folder" are not read.

My trouble comes when I want to read the body of the email. I included a "Console.WriteLine(message.FindFirstPlainTextVersion().GetBodyAsText());" but that only reads the first paragraph of the body, not the whole body. I cannot find a function that reads the entire body. I am using Mac instead of windows so some using statements like Microsoft.Office.Interop.Outlook won't work (not even sure if I need to use that)

Below is my code:

using System;
using OpenPop.Pop3;
using OpenPop.Pop3.Exceptions;
using OpenPop.Mime.Header;
using OpenPop.Mime;
using Org.BouncyCastle.Asn1.X509;
using MimeKit;

namespace Ada_Buyers
{
    class Program
    {
        static void Main(string\[\] args)
        {
            // Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            // Specify host, username, password, Port and SecurityOptions for your client
            client.Connect("hostnumber", port, true);
    
            // Authenticate with the server using username and password
            client.Authenticate("[email protected]", "password");
    
            // Print message after connection
            Console.WriteLine(Environment.NewLine + "Connected to POP3 server.");
    
    
            // Get the number of messages in the inbox folder
            int messageCount = client.GetMessageCount();
            Console.WriteLine($"Number of messages in inbox folder: {messageCount}");
    
            // Print message before looping through messages
            Console.WriteLine("Looping through messages...");
    
            int PrivatePropertyCount = 0;
            int PropertyTwentyFour = 0;
            int notapplicable = 0;
    
            for (int i = 1; i <= messageCount; i++)
            {
                try
                {
                    // Get the message headers for the current message
                    MessageHeader headers = client.GetMessageHeaders(i);
    
                    // Check if the message is from the desired sender(s)
                    if (headers.From != null && (headers.From.Address.Equals("no-    [email protected]", StringComparison.OrdinalIgnoreCase)))                     
                    {
                    
                    // Get the message using the message number
                    var message = client.GetMessage(i);
                    PropertyTwentyFour++;
    
                        // Print the entire email
                        Console.WriteLine("From: " + message.Headers.From.Address);
                        Console.WriteLine("To: " + message.Headers.To[0].Address);
                        Console.WriteLine("Subject: " + message.Headers.Subject);
                        Console.WriteLine("Email Body:");
                    }
    
                    else if (headers.From != null && (headers.From.Address.Equals("no-                             [email protected]", StringComparison.OrdinalIgnoreCase)))
                    {
                        // Get the message using the message number
                        Message message = client.GetMessage(i);
                        PropertyTwentyFour++;
    
                        // Print the entire email
                        Console.WriteLine("From: " + message.Headers.From.Address);
                        Console.WriteLine("To: " + message.Headers.To[0].Address);
                        Console.WriteLine("Subject: " + message.Headers.Subject);
                        Console.WriteLine("Email Body:");
    
                    }
                    else
                    {
                        notapplicable++;
                        Console.WriteLine("Email number: " + i + " was not in private property      or property 24 folder.");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error processing message {i}: {ex.Message}");
                }
        }
        // Print the number of emails that contain the header [email protected]
        Console.WriteLine($"Number of emails containing the header private Property:      {PrivatePropertyCount}"); Console.WriteLine($"Number of emails containing the header Property 24 Alert: {PropertyTwentyFour}"); 
        Console.WriteLine($"Number of emails not send from private property of property 24 is: {notapplicable}");
        }
    }
}

I just changed the hostname, port number, email address, and password for security reasons.

I am a second year student trying to work on some personal projects but we don't do such advance stuff yet and I've googel just about everything before asking for help, chatGPT also cannot help me and none of my classmates can. Can someone please help me regarding this issue. Feels like this is my last hope but will try and search for other alternatives in the meantime.

THANKS IN ADVANCE

0

There are 0 best solutions below