Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / May 2004

Tip: Looking for answers? Try searching our database.

Read POP 3 Mail.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Programmer - 24 May 2004 08:01 GMT
Hi all
I wan't to know if i'm able to read mail from a mail server. My mail server
is a pop3 server (UNIX) and i want to be able to get the mails from an aspx
or an asmx. with out using external objs.
Only classes from the .NET

Is there a way??
Thanks in advance
Calvin Luttrell/ProjectThunder.com - 24 May 2004 08:22 GMT
If you search the internet you can find C# classes that can do this.

I wrote some stuff in VB.Net I've haven't tested it in about 8 months but it might get you started or at least convince you to purchase a component.

-Calvin Luttrell
ProjectThunder.com

Public Class Pop3

Inherits BusniessThunder

Implements ThunderInterface

Private oTCP As New TcpClient()

Private oStream As NetworkStream

Private _name As String

Private _ds As DataSet

Private _pagedata As Hashtable

#Region "Thunder Interface Stuff"

Property PageData() As Hashtable Implements ThunderInterface.PageData

Get

Return _pagedata

End Get

Set(ByVal Value As Hashtable)

_pagedata = Value

End Set

End Property

Property Name() As String Implements ThunderInterface.Name

Get

Return _name

End Get

Set(ByVal Value As String)

_name = Value

End Set

End Property

Property DS() As DataSet Implements ThunderInterface.DS

Get

Return _ds

End Get

Set(ByVal Value As DataSet)

_ds = Value

End Set

End Property

Function Execute() As DataSet Implements ThunderInterface.Execute

If _name.ToUpper = "DELETEMESSAGES" Then

Else

_ds = GetMessages()

End If

Return _ds

End Function

Function Logout()

SendCommand(oStream, "QUIT" + vbCrLf)

oTCP.Close()

End Function

Function DeleteMessages()

Dim orow As DataRow

For Each orow In _ds.Tables(0).Rows

If orow.RowState = DataRowState.Deleted Then

DeleteMessage(orow)

End If

Next

End Function

Sub DeleteMessage(ByVal orow As DataRow)

SendCommand(oStream, "DELE " + orow("Message-ID").ToString + vbCrLf)

End Sub

Function ExecuteThunder(ByVal oThunder As ThunderInterface)

' oThunder.PageData() = GetPageData()

oThunder.Execute()

End Function

#End Region

Sub Login()

Dim sRes As String

oTCP.Connect(_pagedata("HOST"), 110)

oStream = oTCP.GetStream

System.Diagnostics.Debug.WriteLine(GetResponse(oStream))

sRes = SendCommand(oStream, "user " + _pagedata("USER") + vbCrLf)

If sRes.StartsWith("-ERR") Then Exit Sub

System.Diagnostics.Debug.WriteLine(sRes)

sRes = SendCommand(oStream, "pass " + _pagedata("PASS") + vbCrLf)

If sRes.StartsWith("-ERR") Then Exit Sub

System.Diagnostics.Debug.WriteLine(sRes)

End Sub

Private Function GetMessages() As DataSet

Dim sRes As String

Dim sMess As String

Dim iNum As Integer

Dim oDS As New DataSet()

Login()

sRes = SendCommand(oStream, "stat" + vbCrLf)

If sRes.StartsWith("-ERR") Then Exit Function

System.Diagnostics.Debug.WriteLine(sRes)

Dim tmpArray() As String

tmpArray = Split(sRes, " ")

Dim thisMess As Integer

Dim numMess As String = tmpArray(1)

For thisMess = CInt(numMess) - 1 To 1 Step -1

' System.Threading.Thread.Sleep(30)

sMess = SendCommand(oStream, "top " + thisMess.ToString + " 10" + vbCrLf)

If sMess.StartsWith("+OK") And sMess.IndexOf(": ") > 0 Then

ParseMessage(sMess, oDS)

End If

Next

Return oDS

End Function

Function ParseMessage(ByVal sData As String, ByRef oEmailData As DataSet)

Dim iStart As Integer

Dim iEnd As Integer

Dim inextpair As Integer

Dim sMessage As String = ""

Dim sBody As String

Dim iLen As Integer

Dim iPart As Integer

Dim sname As String

Dim svalue As String

If oEmailData Is Nothing Then oEmailData = New DataSet()

If Not oEmailData.Tables.Contains("Data") Then oEmailData.Tables.Add("Data")

Dim orow As DataRow = oEmailData.Tables("Data").NewRow

Try

'Get Header and Body

sMessage = sData.Substring(1, sData.IndexOf(vbCrLf + vbCrLf))

sBody = sData.Substring(sData.IndexOf(vbCrLf + vbCrLf))

Catch

Exit Function

Finally

sData = Nothing

End Try

'Creppy Crawler

'Get Starting postion of first name pair

iPart = sMessage.IndexOf(": ", 1) + 2

'go back to name :

iStart = sMessage.LastIndexOf(vbCrLf, iPart) + 2

'get entire length of email

iLen = sMessage.Length

Try

'work until end of email

Do While iStart < iLen

'make the line we are working on is part of the name value pair

If iPart + 2 > iLen Then Exit Do

If Not sMessage.IndexOf(": ", iPart + 2) = -1 Then

'Find the next ": " for that next name pair

iPart = sMessage.IndexOf(": ", iPart) + 2

'This should shuld check for a vbcrlf between crawling to the next name pair

'This is for when someone puts ": " the freak'n suject

If sMessage.Substring(iStart, iPart - iStart).IndexOf(vbCrLf) < 0 Then

iPart = sMessage.IndexOf(": ", iPart) + 2

End If

'crawl back to find the end of the last line

inextpair = sMessage.LastIndexOf(vbCrLf, iPart) + 2

'This gives us the postion in the mini-string which is actually the length of the amount of data we want

iEnd = sMessage.Substring(iStart, inextpair - iStart).LastIndexOf(vbCrLf) + 2

Else

iEnd = iLen - iStart

End If

'Mini me string for parsing name value pair

Dim stemp = sMessage.Substring(iStart, iEnd)

sname = stemp.Substring(0, stemp.IndexOf(": "))

svalue = stemp.Substring(stemp.IndexOf(": ") + 2)

'check to see if the colomn exists yet

If Not oEmailData.Tables("Data").Columns.Contains(sname) Then

'add colomn

oEmailData.Tables("Data").Columns.Add(sname)

End If

'assign value

orow(sname) = svalue

iStart += iEnd

Loop

Catch ex As Exception

System.Diagnostics.Debug.WriteLine(ex.Message)

End Try

'grab body and stick it in the table

If Not oEmailData.Tables("Data").Columns.Contains("BODY") Then

oEmailData.Tables("Data").Columns.Add("BODY")

End If

orow("BODY") = sBody

'add row

oEmailData.Tables(0).Rows.Add(orow)

'TODO parse body remove attachments

Return oEmailData

End Function

Function SendCommand(ByRef netstream As NetworkStream, ByVal sToSend As String) As String

Dim bData() As Byte = Encoding.ASCII.GetBytes(sToSend.ToCharArray)

netstream.Write(bData, 0, bData.Length())

Return GetResponse(netstream)

End Function

' check if there is a response to get and return it

Function GetResponse(ByRef netstream As NetworkStream) As String

Dim bytes(oTCP.ReceiveBufferSize) As Byte

Dim ret As Integer = netstream.Read(bytes, 0, bytes.Length)

' Returns the data received

Dim returndata As String = Encoding.ASCII.GetString(bytes)

Return returndata

End Function

End Class

> Hi all
> I wan't to know if i'm able to read mail from a mail server. My mail server
[quoted text clipped - 4 lines]
> Is there a way??
> Thanks in advance
Jan Tielens - 24 May 2004 11:01 GMT
There are some resources on the net available:
http://www.developerfusion.com/show/4071/
http://www.codeproject.com/useritems/POP3Library.asp
http://www.programmersheaven.com/search/LinkDetail.asp?Typ=2&ID67

Signature

Greetz
Jan
________________
Read my weblog: http://weblogs.asp.net/jan

 If you search the internet you can find C# classes that can do this.

 I wrote some stuff in VB.Net I've haven't tested it in about 8 months but it might get you started or at least convince you to purchase a component.

 -Calvin Luttrell
 ProjectThunder.com

 Public Class Pop3

 Inherits BusniessThunder

 Implements ThunderInterface

 Private oTCP As New TcpClient()

 Private oStream As NetworkStream

 Private _name As String

 Private _ds As DataSet

 Private _pagedata As Hashtable

 #Region "Thunder Interface Stuff"

 Property PageData() As Hashtable Implements ThunderInterface.PageData

 Get

 Return _pagedata

 End Get

 Set(ByVal Value As Hashtable)

 _pagedata = Value

 End Set

 End Property

 Property Name() As String Implements ThunderInterface.Name

 Get

 Return _name

 End Get

 Set(ByVal Value As String)

 _name = Value

 End Set

 End Property

 Property DS() As DataSet Implements ThunderInterface.DS

 Get

 Return _ds

 End Get

 Set(ByVal Value As DataSet)

 _ds = Value

 End Set

 End Property

 Function Execute() As DataSet Implements ThunderInterface.Execute

 If _name.ToUpper = "DELETEMESSAGES" Then

 Else

 _ds = GetMessages()

 End If

 Return _ds

 End Function

 Function Logout()

 SendCommand(oStream, "QUIT" + vbCrLf)

 oTCP.Close()

 End Function

 Function DeleteMessages()

 Dim orow As DataRow

 For Each orow In _ds.Tables(0).Rows

 If orow.RowState = DataRowState.Deleted Then

 DeleteMessage(orow)

 End If

 Next

 End Function

 Sub DeleteMessage(ByVal orow As DataRow)

 SendCommand(oStream, "DELE " + orow("Message-ID").ToString + vbCrLf)

 End Sub

 Function ExecuteThunder(ByVal oThunder As ThunderInterface)

 ' oThunder.PageData() = GetPageData()

 oThunder.Execute()

 End Function

 #End Region

 Sub Login()

 Dim sRes As String

 oTCP.Connect(_pagedata("HOST"), 110)

 oStream = oTCP.GetStream

 System.Diagnostics.Debug.WriteLine(GetResponse(oStream))

 sRes = SendCommand(oStream, "user " + _pagedata("USER") + vbCrLf)

 If sRes.StartsWith("-ERR") Then Exit Sub

 System.Diagnostics.Debug.WriteLine(sRes)

 sRes = SendCommand(oStream, "pass " + _pagedata("PASS") + vbCrLf)

 If sRes.StartsWith("-ERR") Then Exit Sub

 System.Diagnostics.Debug.WriteLine(sRes)

 End Sub

 Private Function GetMessages() As DataSet

 Dim sRes As String

 Dim sMess As String

 Dim iNum As Integer

 Dim oDS As New DataSet()

 Login()

 sRes = SendCommand(oStream, "stat" + vbCrLf)

 If sRes.StartsWith("-ERR") Then Exit Function

 System.Diagnostics.Debug.WriteLine(sRes)

 Dim tmpArray() As String

 tmpArray = Split(sRes, " ")

 Dim thisMess As Integer

 Dim numMess As String = tmpArray(1)

 For thisMess = CInt(numMess) - 1 To 1 Step -1

 ' System.Threading.Thread.Sleep(30)

 sMess = SendCommand(oStream, "top " + thisMess.ToString + " 10" + vbCrLf)

 If sMess.StartsWith("+OK") And sMess.IndexOf(": ") > 0 Then

 ParseMessage(sMess, oDS)

 End If

 Next

 Return oDS

 End Function

 Function ParseMessage(ByVal sData As String, ByRef oEmailData As DataSet)

 Dim iStart As Integer

 Dim iEnd As Integer

 Dim inextpair As Integer

 Dim sMessage As String = ""

 Dim sBody As String

 Dim iLen As Integer

 Dim iPart As Integer

 Dim sname As String

 Dim svalue As String

 If oEmailData Is Nothing Then oEmailData = New DataSet()

 If Not oEmailData.Tables.Contains("Data") Then oEmailData.Tables.Add("Data")

 Dim orow As DataRow = oEmailData.Tables("Data").NewRow

 Try

 'Get Header and Body

 sMessage = sData.Substring(1, sData.IndexOf(vbCrLf + vbCrLf))

 sBody = sData.Substring(sData.IndexOf(vbCrLf + vbCrLf))

 Catch

 Exit Function

 Finally

 sData = Nothing

 End Try

 'Creppy Crawler

 'Get Starting postion of first name pair

 iPart = sMessage.IndexOf(": ", 1) + 2

 'go back to name :

 iStart = sMessage.LastIndexOf(vbCrLf, iPart) + 2

 'get entire length of email

 iLen = sMessage.Length

 Try

 'work until end of email

 Do While iStart < iLen

 'make the line we are working on is part of the name value pair

 If iPart + 2 > iLen Then Exit Do

 If Not sMessage.IndexOf(": ", iPart + 2) = -1 Then

 'Find the next ": " for that next name pair

 iPart = sMessage.IndexOf(": ", iPart) + 2

 'This should shuld check for a vbcrlf between crawling to the next name pair

 'This is for when someone puts ": " the freak'n suject

 If sMessage.Substring(iStart, iPart - iStart).IndexOf(vbCrLf) < 0 Then

 iPart = sMessage.IndexOf(": ", iPart) + 2

 End If

 'crawl back to find the end of the last line

 inextpair = sMessage.LastIndexOf(vbCrLf, iPart) + 2

 'This gives us the postion in the mini-string which is actually the length of the amount of data we want

 iEnd = sMessage.Substring(iStart, inextpair - iStart).LastIndexOf(vbCrLf) + 2

 Else

 iEnd = iLen - iStart

 End If

 'Mini me string for parsing name value pair

 Dim stemp = sMessage.Substring(iStart, iEnd)

 sname = stemp.Substring(0, stemp.IndexOf(": "))

 svalue = stemp.Substring(stemp.IndexOf(": ") + 2)

 'check to see if the colomn exists yet

 If Not oEmailData.Tables("Data").Columns.Contains(sname) Then

 'add colomn

 oEmailData.Tables("Data").Columns.Add(sname)

 End If

 'assign value

 orow(sname) = svalue

 iStart += iEnd

 Loop

 Catch ex As Exception

 System.Diagnostics.Debug.WriteLine(ex.Message)

 End Try

 'grab body and stick it in the table

 If Not oEmailData.Tables("Data").Columns.Contains("BODY") Then

 oEmailData.Tables("Data").Columns.Add("BODY")

 End If

 orow("BODY") = sBody

 'add row

 oEmailData.Tables(0).Rows.Add(orow)

 'TODO parse body remove attachments

 Return oEmailData

 End Function

 Function SendCommand(ByRef netstream As NetworkStream, ByVal sToSend As String) As String

 Dim bData() As Byte = Encoding.ASCII.GetBytes(sToSend.ToCharArray)

 netstream.Write(bData, 0, bData.Length())

 Return GetResponse(netstream)

 End Function

 ' check if there is a response to get and return it

 Function GetResponse(ByRef netstream As NetworkStream) As String

 Dim bytes(oTCP.ReceiveBufferSize) As Byte

 Dim ret As Integer = netstream.Read(bytes, 0, bytes.Length)

 ' Returns the data received

 Dim returndata As String = Encoding.ASCII.GetString(bytes)

 Return returndata

 End Function

 End Class

 > Hi all
 > I wan't to know if i'm able to read mail from a mail server. My mail server
 > is a pop3 server (UNIX) and i want to be able to get the mails from an aspx
 > or an asmx. with out using external objs.
 > Only classes from the .NET
 >
 > Is there a way??
 > Thanks in advance
Chad Z. Hower aka Kudzu - 24 May 2004 19:42 GMT
"Programmer" <seeyou_gr@hotmail.com> wrote in news:#ZkLpzVQEHA.2520
@TK2MSFTNGP11.phx.gbl:
> I wan't to know if i'm able to read mail from a mail server. My mail server
> is a pop3 server (UNIX) and i want to be able to get the mails from an aspx
> or an asmx. with out using external objs.
> Only classes from the .NET

Yes - but you'll have installation issues with CDO and other.

This assembly is .NET, and free but you have to distribute it.
http://www.indyproject.org/

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
     "Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
 http://www.atozed.com/IntraWeb/

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.