?
Solved

Extract email content to Txt file

Posted on 2011-03-18
7
Medium Priority
?
481 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
Independent Software Vendors: 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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

801 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