ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
ASP.NET by Example
ASP.NET by Example

Find Prices
Read Review
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software
PopMail.aspx.vb by Damian Manifold
  1 public class PopMail2
  2     Inherits System.Web.UI.Page
  3     protected WithEvents Srv as System.Web.UI.WebControls.TextBox
  4     protected WithEvents Passwd as System.Web.UI.WebControls.TextBox
  5     protected WithEvents Button1 as System.Web.UI.WebControls.Button
  6     protected WithEvents Usr as System.Web.UI.WebControls.TextBox
  7     protected WithEvents AnswerTable as System.Web.UI.WebControls.Table
  8     protected WithEvents Answer as System.Web.UI.WebControls.Literal
  9     protected WithEvents MsgCount as System.Web.UI.WebControls.TableCell
  10     protected WithEvents Alliance2 as DPM.Alliance
  11     protected WithEvents Alliance1 as DPM.Alliance
  12     protected WithEvents MsgBoxSize as System.Web.UI.WebControls.TableCell
  13 
  14     private Sub Page_Load(ByVal sender as System.objectByVal e as System.EventArgs) Handles MyBase.Load
  15         if Page.IsPostBack Then
  16             Dim p as new pop3(Srv.Text)
  17 
  18             Dim success as Boolean = false
  19 
  20             success = p.logon(Usr.Text, Passwd.Text)
  21             if success Then
  22                 Answer.Text = "Logon Successfull"
  23                 'display mailbox details
  24                 MsgCount.Text = p.MessageCount
  25                 MsgBoxSize.Text = p.MailBoxSize
  26 
  27                 'create and array for the mail details
  28                 Dim ma(p.MessageCount) as pop3.MailMessage
  29                 Dim r as TableRow
  30                 Dim c as TableCell
  31                 'cycle through mails, diplsaying size and id
  32                 Dim cnt as Integer
  33                 for cnt = 1 To p.MessageCount
  34                     'create a new instane of mail in the array
  35                     ma(cnt) = new pop3.MailMessage()
  36                     ma(cnt).ID = cnt
  37                     p.getSize(ma(cnt))
  38                     r = new TableRow()
  39                     c = new TableCell()
  40                     c.HorizontalAlign = HorizontalAlign.Center
  41                     c.Text = ma(cnt).ID.ToString
  42                     r.Cells.Add(c)
  43                     c = new TableCell()
  44                     c.HorizontalAlign = HorizontalAlign.Center
  45                     c.Text = ma(cnt).Size.ToString
  46                     r.Cells.Add(c)
  47                     AnswerTable.Rows.Add(r)
  48                 Next
  49                 'For Each m In p.MailMessages
  50                 'Next
  51                 'show table
  52                 AnswerTable.Visible = true
  53             else
  54                 Answer.Text = "Logon Unsuccessfull"
  55             End if
  56             p.logoff()
  57         End if
  58     End Sub
  59 
  60     class pop3
  61         'class for holding details about each mail
  62         public class MailMessage
  63             private _ID as Integer = -1
  64             Property ID() as Integer
  65                 Get
  66                     Return _ID
  67                 End Get
  68                 Set(ByVal Value as Integer)
  69                     _ID = Value
  70                 End Set
  71             End Property
  72 
  73             private _Size as Integer = 0
  74             Property Size() as Integer
  75                 Get
  76                     Return _Size
  77                 End Get
  78                 Set(ByVal Value as Integer)
  79                     _Size = Value
  80                 End Set
  81             End Property
  82         End class
  83 
  84         'a private holder for the number of mails
  85         private _MessageCount as Integer = -1
  86         readonly Property MessageCount() as Integer
  87             Get
  88                 'if the count has not yet been set, request details from server
  89                 if _MessageCount = -1 Then
  90                     GetStats()
  91                 End if
  92                 Return _MessageCount
  93             End Get
  94         End Property
  95 
  96         'private holder for mailbox size
  97         private _MailBoxSize as Integer = -1
  98         readonly Property MailBoxSize() as Integer
  99             Get
  100                 'if the size has not yet been set, request details from server
  101                 if _MailBoxSize = -1 Then
  102                     GetStats()
  103                 End if
  104                 Return _MailBoxSize
  105             End Get
  106         End Property
  107 
  108         private Sub GetStats()
  109             Dim a as string()
  110             'issue the stat command to the server
  111             'split the returned string into an array
  112             a = SendCmd("stat").Split(" ")
  113 
  114             'set the count
  115             _MessageCount = CType(a(1), Integer)
  116             'set the size
  117             _MailBoxSize = CType(a(2), Integer)
  118         End Sub
  119 
  120         public function getSize(ByRef m as MailMessage) as Boolean
  121             Dim a() as string
  122 
  123             'perform the list command
  124             a = SendCmd("list " + m.ID.ToString).Split(" ")
  125 
  126             'if the command was ok set the message size
  127             if a(0) = "+OK" Then
  128                 m.Size = CType(a(2), Integer)
  129                 Return true
  130                 'else indicate the mail doesn't exist
  131             else
  132                 m.ID = -1
  133                 m.Size = 0
  134                 Return false
  135             End if
  136         End function
  137 
  138         '-- the code below is as before .
  139 
  140         private s as System.Net.Sockets.NetworkStream
  141         private t as new System.Net.Sockets.TcpClient()
  142         private Cnct as Boolean = false
  143 
  144         public Sub new(ByVal Server as string)
  145             'open port 110 ( the pop3 port ) On the server
  146             Try
  147                 'catch any error resuting from a bad server name
  148                 t.Connect(Server, 110)
  149                 s = t.GetStream()
  150                 'check that the connection is okay
  151                 if Left(getData(), 3) = "+OK" Then
  152                     Cnct = true
  153                 End if
  154             catch
  155             End Try
  156         End Sub
  157 
  158         public function logon(ByVal User as stringByVal passwd as stringas Boolean
  159             Dim ret as string
  160 
  161             logon = false
  162 
  163             'make sure you have a connection
  164             if Cnct Then
  165                 'send the username
  166                 ret = SendCmd("user " + User)
  167 
  168                 'if that was successfull, send the password
  169                 if Left(ret, 3) = "+OK" Then
  170                     ret = SendCmd("pass " + passwd)
  171 
  172                     'if that was successfull set the return flas to true
  173                     if Left(ret, 3) = "+OK" Then
  174                         logon = true
  175                     End if
  176                 End if
  177             End if
  178         End function
  179 
  180         public function logoff() as string
  181             if Cnct Then
  182                 logoff = SendCmd("QUIT")
  183             End if
  184         End function
  185 
  186         private function SendCmd(ByVal Cmd as stringas string
  187             Dim bCmd as byte()
  188             'byte encode the command
  189             bCmd = System.Text.Encoding.ASCII.GetBytes(Cmd + vbCrLf)
  190 
  191             'send the data
  192             s.Write(bCmd, 0, bCmd.Length)
  193             SendCmd = getData()
  194         End function
  195 
  196         private function getData() as string
  197             Dim bData(t.ReceiveBufferSize) as byte
  198             'get the response
  199             s.Read(bData, 0, bData.Length)
  200             'return the response
  201             getData = System.Text.Encoding.ASCII.GetString(bData)
  202         End function
  203     End class
  204 
  205 End class
  206 
PopMail.aspx.vbGenerated using CodeView
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/22/2009 4:36:38 PM