Solved

Extract email content to Txt file

Posted on 2011-03-18
7
474 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

777 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