Solved

Extract email content to Txt file

Posted on 2011-03-18
7
469 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

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

Author Comment

by:touyets17
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now