Read POP 3 Mail.

Discussion in 'ASP .Net Web Services' started by Programmer, May 24, 2004.

  1. Programmer

    Programmer Guest

    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
     
    Programmer, May 24, 2004
    #1
    1. Advertising

  2. 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
     
    Calvin Luttrell/ProjectThunder.com, May 24, 2004
    #2
    1. Advertising

  3. "Programmer" <> 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/
     
    Chad Z. Hower aka Kudzu, May 24, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Programmer

    Read POP 3 Mail.

    Programmer, May 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    661
    Chad Z. Hower aka Kudzu
    May 24, 2004
  2. Replies:
    2
    Views:
    8,606
    Randy Webb
    Feb 4, 2006
  3. Bone Ur
    Replies:
    4
    Views:
    422
    Bone Ur
    Dec 19, 2007
  4. Chris Davoli

    Tool Tip pop up - pop up time is too short

    Chris Davoli, Feb 24, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    502
    Lee Atkinson
    Feb 24, 2009
  5. Tom Mac
    Replies:
    1
    Views:
    204
    Walton Hoops
    May 3, 2011
Loading...

Share This Page