Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Extract email content to Txt file

Posted on 2011-03-18
7
Medium Priority
?
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

604 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