Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   C#: How to parse emails? (https://www.warriorforum.com/programming/462359-c-how-parse-emails.html)

Edwin Torres 1st October 2011 03:06 PM

C#: How to parse emails?
 
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

unnatural 1st October 2011 04:55 PM

Re: C#: How to parse emails?
 
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

Edwin Torres 1st October 2011 05:00 PM

Re: C#: How to parse emails?
 
Can I use OpenPop3 or do I use the built in support for POP3 that C# brings?

unnatural 1st October 2011 05:07 PM

Re: C#: How to parse emails?
 
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.

Edwin Torres 1st October 2011 05:54 PM

Re: C#: How to parse emails?
 
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();


LakiPolitis 3rd October 2011 10:03 AM

Re: C#: How to parse emails?
 
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

gtownfunk 3rd October 2011 08:53 PM

Re: C#: How to parse emails?
 
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


All times are GMT -6. The time now is 06:54 PM.