Solved

Extract email content to Txt file

Posted on 2011-03-18
7
475 Views
Last Modified: 2012-05-11
Hi all,

A lot of searching on EE and Google and came up empty handed.

I have a piece of code below that should extract the content of a hotmail email message into a text file but it refuses to work. I don't know if it's only because i fail to define the code properly or if I am approaching it completely wrong.

It should ask for the URL and then go and extract that URL's email content (trying to use it for hotmail). Preferably, I would liek for teh code NOT to open any webpage but just perform teh task in teh background.

I'm new here and have tried to attached teh code snippet but elt em know if you can't see it properly


Dim szFileName As String
        Dim MyFile As String
        MyFile = Space$(300)
        If URLDownloadToCacheFile(0, " Whatever URL", MyFile, Len(MyFile), 0, 0) = 0 Then DownloadFile = Trim(MyFile)

Open in new window

0
Comment
Question by:touyets17
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35164267
According to the documentation, MyFile will hold the name of the local cache file which has the contents. I don't see any code to do an actual extraction of the message. My estimation is you need to load MyFile into a StreamReader or FileStream and read the contents of the file.
0
 
LVL 4

Expert Comment

by:coolcurrent4u
ID: 35164318
You can use different metho to extract email from a given url, but you need some basic things

First you need to retrieve the webpage as string, you can use tcpclient, httpwebrequest, etc. The  you need to use regular expression to extract the email address from the downloaded webpage.


check the sample code
using System.Net.WebClient client = new System.Net.WebClient()
  dim html as string = client.DownloadString("http://www.google.com")
end using
 //string pattern is Email validation Expressioin 
Dim re As Regex = New Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")

Dime myMatches as MatchCollection = re.Matches(responseText)

dim str as string = vbnullstring

for each m as Match in myMatches 

str += m.Value & " , "
Next

debug.pring9 str)

Open in new window

0
 

Author Comment

by:touyets17
ID: 35164488
Hi Coolcurrent4u,

i'm sorry to say but that is some piece of coding that i have never seen before and i fail to understand it or even how this code works. maybe that is a bit too advanced for me :(

When i put in your code exactly as is, it gives me 2 error messages:
- it refuses ystem.Net.WebClient because: " 'WebClient' is a type in 'Net' and cannot be used as an expression" (I am using VB express 2005 if that is helpful)
- Type MatchCollection is not defined
- Type Macth is not defined
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 4

Expert Comment

by:coolcurrent4u
ID: 35164509
Ok let me make a demo cod for you, but first the website you need to extract email from
0
 

Author Comment

by:touyets17
ID: 35164536
Thanks Coolcurrent4u.

I want to do hotmail.com if possible (i'd put in a specific URL to extra from) but can you try with http://en.wikipedia.org/wiki/French_language for example.
0
 
LVL 4

Accepted Solution

by:
coolcurrent4u earned 500 total points
ID: 35165386
Run the following code

 
Imports System.Text.RegularExpressions
Imports System.Net
Imports System.IO
Public Class Form1
    Dim html As String = vbNullString
    Private Sub btnExtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtract.Click
        lblStatus.Text = "Status: Downloading web page..."
        btnExtract.Enabled = False
        Application.DoEvents()
        Dim strResult As String = vbNullString
        Try

            strResult = DownloadHTMLPage(txtUrl.Text)
            '//string pattern is Email validation Expressioin 
            Application.DoEvents()
            'txtResult.Text = strResult
            lblStatus.Text = "Status: Extracting email..."
            Dim re As Regex = New Regex("[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+")
            Dim myMatches As MatchCollection = re.Matches(strResult)
            Dim str As String = vbNullString
            Application.DoEvents()
            For Each m As Match In myMatches
                str &= m.Value & " , "
            Next
            txtResult.Text = str
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Extract Url", _
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
        lblStatus.Text = "Status: Done!"
        btnExtract.Enabled = True
    End Sub
    ''' <summary>
    ''' Function to download HTML web page
    ''' </summary>
    ''' <param name="_URL">URL address to download web page</param>
    ''' <returns>HTML contents as a string</returns>
    Public Function DownloadHTMLPage(ByVal _URL As String) As String
        Dim _PageContent As String = Nothing
        Try
            ' Open a connection
            Dim _HttpWebRequest As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(_URL), System.Net.HttpWebRequest)

            ' You can also specify additional header values like the user agent or the referer: (Optional)
            _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
            _HttpWebRequest.Referer = "http://www.google.com/"

            ' set timeout for 10 seconds (Optional)
            _HttpWebRequest.Timeout = 120000

            ' Request response:
            Dim _WebResponse As System.Net.WebResponse = _HttpWebRequest.GetResponse()

            ' Open data stream:
            Dim _WebStream As System.IO.Stream = _WebResponse.GetResponseStream()

            ' Create reader object:
            Dim _StreamReader As New System.IO.StreamReader(_WebStream)

            ' Read the entire stream content:
            _PageContent = _StreamReader.ReadToEnd()

            ' Cleanup
            _StreamReader.Close()
            _WebStream.Close()
            _WebResponse.Close()
        Catch _Exception As Exception
            ' Error
            Return ("Exception caught in process:" & _Exception.ToString())
        End Try
        Return _PageContent
    End Function
End Class

Open in new window


it will give this output

 ScreenShot
0
 

Author Closing Comment

by:touyets17
ID: 35415284
Hi all, sorry, I was away on holidays for a few weeks there. coolcurrent4u:, taht is perfect. i adapted it to my data and it ran really smoothly.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question