OpenPop.net get actual message text from attachment

789 Views Asked by At

I am using OpenPop.net to try and parse our links from all the emails that are in a given inbox. I found this method to get all the message :
`

  public static List<OpenPop.Mime.Message> FetchAllMessages(stringhostname,  
int port, bool useSsl, string username, string password)    
 {

 // The client disconnects from the server when being disposed


    using (Pop3Client client = new Pop3Client())
    {

        // Connect to the server
        client.Connect(hostname, port, useSsl);

        // Authenticate ourselves towards the server
        client.Authenticate(username, password);

        // Get the number of messages in the inbox
        int messageCount = client.GetMessageCount();

        // We want to download all messages
        List<OpenPop.Mime.Message> allMessages = new List<OpenPop.Mime.Message>(messageCount);

        // Messages are numbered in the interval: [1, messageCount]
        // Ergo: message numbers are 1-based.
        // Most servers give the latest message the highest number
        for (int i = messageCount; i > 0; i--)
        {
            allMessages.Add(client.GetMessage(i)); 
             foreach (var attachment in msg.FindAllAttachments())
             {
                string str = enc.GetString(attachment.Body);
             }                   
        }

        client.Disconnect();

        // Now return the fetched messages
        return allMessages;
    }
}`

Also tried to encode through Ascii character using that way :

Encode data

But, can't success.. I want to need html from attachment file. But in attachment.Body give byte array. So, How can byte array to HTML?

2

There are 2 best solutions below

1
Matěj Štágl On

You can use Encoding to get a string from a byte stream:

string result = Encoding.UTF8.GetString(bytes);

Check if your string is utf8, ascii or something else. You might have to change this a bit.

0
zaki85 On

You can use Encoding in following way:

string result = Encoding.UTF8.GetString(bytes, 0, bytes.length);