C#: How to parse emails?

6 replies
Hi,

Anyone know the code or how to parse through emails that a site gives you and click the links inside of them. Im using the dll OpenPop3 but so far have only figured out how to login.

Any help would be appreciated
#c sharp #email #emails #openpop #parse
  • Profile picture of the author unnatural
    Just download the emails with the POP3 connection, then you can use the following regex to match all the links.

    #(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i
    {{ DiscussionBoard.errors[4792534].message }}
  • Profile picture of the author Edwin Torres
    Can I use OpenPop3 or do I use the built in support for POP3 that C# brings?
    {{ DiscussionBoard.errors[4792551].message }}
  • Profile picture of the author unnatural
    I checked out OpenPop3 on SourceForge but I don't see any files or examples so I can't tell you for sure. I imagine its just a helper library to communicate with the built in POP3 functions so I don't see why not, try reading the documentation.

    Also, I found another similar library called OpenPop OpenPop.NET | Free Communications software downloads at SourceForge.net which seems to actually have files to download and it says it supports all the common POP3 functions and email MIME types.
    {{ DiscussionBoard.errors[4792574].message }}
  • Profile picture of the author Edwin Torres
    This is what I have so far:

    Code:
     Pop3Client client = new Pop3Client();
                client.Connect("pop3.live.com", 995, true);
                client.Authenticate("USER", "PASSWORD");
                label18.Text = client.GetMessageCount().ToString();
                int messageCount = client.GetMessageCount();
    
    
                for (int i = 1; i <= messageCount; i++)
                {
                    // Download the header and check if the message is interesting
                    // enough to download if with body
                    MessageHeader header = client.GetMessageHeaders(i);
                    if ("Wordpress".Contains(header.Subject))
                    {
                        // The message is interesting - download it
                        OpenPop.Mime.Message message = client.GetMessage(i);
    
                        // Find the first part containing HTML
                        // null is returned if such part is not found
                        MessagePart html = message.FindFirstHtmlVersion();
                        if (html != null)
                        {
                            String htmlContained = html.GetBodyAsText();
                            // Do something with the html
                            // here showing it in a listbox
                            richTextBox1.Text = htmlContained;
                        }
                    }
                }
                client.Disconnect();
    {{ DiscussionBoard.errors[4792739].message }}
    • Profile picture of the author LakiPolitis
      You could looks for the characters "<a href", then split the strings up til you find the immediate next "</a>". Then add all those split strings into a generic list.

      Once they're in a generic list you can bind that list to a repeater, and dynamically create them as a page you could click the links on.

      Here's some info on generic lists: An Introduction to C# Generics
      {{ DiscussionBoard.errors[4800941].message }}
  • Profile picture of the author gtownfunk
    I know it looks like a bunch of gobbleygook, but you should really use the Regular Expression method that unnatural posted. C# has a great Regular Expression library and there is tons of free sample code out there. You can literally parse ALL LINKS out of an email in 3-4 lines of code.

    gtownfunk
    Signature
    {{ DiscussionBoard.errors[4804264].message }}

Trending Topics