asp.net,pop3实现收邮件代码实例

19 篇文章 0 订阅
PopMail.aspx.vb (Logging On & Logging Off)
  1 public class PopMail
  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 Answer as System.Web.UI.WebControls.Literal
  7     protected WithEvents Alliance1 as DPM.Alliance
  8     protected WithEvents Alliance2 as DPM.Alliance
  9     protected WithEvents Usr as System.Web.UI.WebControls.TextBox
  10 
  11     private Sub Page_Load(ByVal sender as System.objectByVal e as System.EventArgs) Handles MyBase.Load
  12         if Page.IsPostBack Then
  13             Dim p as new pop3(Srv.Text)
  14             Dim success as Boolean = false
  15 
  16             success = p.logon(Usr.Text, Passwd.Text)
  17             p.logoff()
  18             if success Then
  19                 Answer.Text = "Logon Successfull"
  20             else
  21                 Answer.Text = "Logon Unsuccessfull"
  22             End if
  23         End if
  24     End Sub
  25 
  26     class pop3
  27         private s as System.Net.Sockets.NetworkStream
  28         private t as new System.Net.Sockets.TcpClient()
  29         private Cnct as Boolean = false
  30 
  31         public Sub new(ByVal Server as string)
  32             'open port 110 ( the pop3 port ) On the server
  33             Try
  34                 'catch any error resuting from a bad server name
  35                 t.Connect(Server, 110)
  36                 s = t.GetStream()
  37                 'check that the connection is okay
  38                 if Left(getData(), 3) = "+OK" Then
  39                     Cnct = true
  40                 End if
  41             catch
  42             End Try
  43         End Sub
  44 
  45         public function logon(ByVal User as stringByVal passwd as stringas Boolean
  46             Dim ret as string
  47 
  48             logon = false
  49 
  50             'make sure you have a connection
  51             if Cnct Then
  52                 'send the username
  53                 ret = SendCmd("user " + User)
  54 
  55                 'if that was successfull, send the password
  56                 if Left(ret, 3) = "+OK" Then
  57                     ret = SendCmd("pass " + passwd)
  58 
  59                     'if that was successfull set the return flas to true
  60                     if Left(ret, 3) = "+OK" Then
  61                         logon = true
  62                     End if
  63                 End if
  64             End if
  65         End function
  66 
  67         public function logoff() as string
  68             if Cnct Then
  69                 logoff = SendCmd("QUIT")
  70             End if
  71         End function
  72 
  73         private function SendCmd(ByVal Cmd as stringas string
  74             Dim bCmd as byte()
  75             'byte encode the command
  76             bCmd = System.Text.Encoding.ASCII.GetBytes(Cmd + vbCrLf)
  77 
  78             'send the data
  79             s.Write(bCmd, 0, bCmd.Length)
  80             SendCmd = getData()
  81         End function
  82 
  83         private function getData() as string
  84             Dim bData(t.ReceiveBufferSize) as byte
  85             'get the response
  86             s.Read(bData, 0, bData.Length)
  87             'return the response
  88             getData = System.Text.Encoding.ASCII.GetString(bData)
  89         End function
  90     End class
  91 
  92 End class
PopMail.aspx.vb (Getting MailBox & Message Size)
  1 Imports System.Text.RegularExpressions
  2 public class PopMail4
  3     Inherits System.Web.UI.Page
  4     protected WithEvents Srv as System.Web.UI.WebControls.TextBox
  5     protected WithEvents Passwd as System.Web.UI.WebControls.TextBox
  6     protected WithEvents Button1 as System.Web.UI.WebControls.Button
  7     protected WithEvents Usr as System.Web.UI.WebControls.TextBox
  8     protected WithEvents AnswerTable as System.Web.UI.WebControls.Table
  9     protected WithEvents Answer as System.Web.UI.WebControls.Literal
  10     protected WithEvents MsgCount as System.Web.UI.WebControls.TableCell
  11     protected WithEvents Alliance1 as DPM.Alliance
  12     protected WithEvents Alliance2 as DPM.Alliance
  13     protected WithEvents MsgBoxSize as System.Web.UI.WebControls.TableCell
  14 
  15     private Sub Page_Load(ByVal sender as System.objectByVal e as System.EventArgs) Handles MyBase.Load
  16         if Page.IsPostBack Then
  17             Dim p as new pop3(Srv.Text)
  18 
  19             Dim success as Boolean = false
  20 
  21             success = p.logon(Usr.Text, Passwd.Text)
  22             if success Then
  23                 Answer.Text = "Logon Successfull"
  24 
  25                 'create and array for the mail details
  26                 Dim ma(p.MessageCount) as pop3.MailMessage
  27                 Dim r as TableRow
  28                 Dim c as TableCell
  29                 'cycle through mails, diplsaying size and id
  30                 Dim cnt as Integer
  31                 for cnt = 1 To p.MessageCount
  32                     'create a new instane of mail in the array
  33                     ma(cnt) = new pop3.MailMessage()
  34                     ma(cnt).ID = cnt
  35                     'get the mail message in full
  36                     p.getMail(ma(cnt))
  37 
  38                     'display the returned values
  39                     'these have been htmlencoded to prevent problems with being display
  40                     'in a web page
  41                     r = new TableRow()
  42                     c = new TableCell()
  43                     c.Text = "To:"
  44                     c.HorizontalAlign = HorizontalAlign.Left
  45                     c.VerticalAlign = VerticalAlign.Top
  46                     c.Font.Bold = true
  47                     r.Cells.Add(c)
  48                     c = new TableCell()
  49                     c.Text = Server.HtmlEncode(ma(cnt).To)
  50                     c.HorizontalAlign = HorizontalAlign.Left
  51                     c.VerticalAlign = VerticalAlign.Top
  52                     r.Cells.Add(c)
  53                     AnswerTable.Rows.Add(r)
  54 
  55                     r = new TableRow()
  56                     c = new TableCell()
  57                     c.Text = "Cc:"
  58                     c.HorizontalAlign = HorizontalAlign.Left
  59                     c.VerticalAlign = VerticalAlign.Top
  60                     c.Font.Bold = true
  61                     r.Cells.Add(c)
  62                     c = new TableCell()
  63                     c.Text = Server.HtmlEncode(ma(cnt).Cc)
  64                     c.HorizontalAlign = HorizontalAlign.Left
  65                     c.VerticalAlign = VerticalAlign.Top
  66                     r.Cells.Add(c)
  67                     AnswerTable.Rows.Add(r)
  68 
  69                     r = new TableRow()
  70                     c = new TableCell()
  71                     c.Text = "From:"
  72                     c.HorizontalAlign = HorizontalAlign.Left
  73                     c.VerticalAlign = VerticalAlign.Top
  74                     c.Font.Bold = true
  75                     r.Cells.Add(c)
  76                     c = new TableCell()
  77                     c.Text = Server.HtmlEncode(ma(cnt).From)
  78                     c.HorizontalAlign = HorizontalAlign.Left
  79                     c.VerticalAlign = VerticalAlign.Top
  80                     r.Cells.Add(c)
  81                     AnswerTable.Rows.Add(r)
  82 
  83                     r = new TableRow()
  84                     c = new TableCell()
  85                     c.Text = "Subject:"
  86                     c.HorizontalAlign = HorizontalAlign.Left
  87                     c.VerticalAlign = VerticalAlign.Top
  88                     c.Font.Bold = true
  89                     r.Cells.Add(c)
  90                     c = new TableCell()
  91                     c.Text = Server.HtmlEncode(ma(cnt).Subject)
  92                     c.HorizontalAlign = HorizontalAlign.Left
  93                     c.VerticalAlign = VerticalAlign.Top
  94                     r.Cells.Add(c)
  95                     AnswerTable.Rows.Add(r)
  96 
  97                     r = new TableRow()
  98                     c = new TableCell()
  99                     c.Text = "Reply To:"
  100                     c.HorizontalAlign = HorizontalAlign.Left
  101                     c.VerticalAlign = VerticalAlign.Top
  102                     c.Font.Bold = true
  103                     r.Cells.Add(c)
  104                     c = new TableCell()
  105                     c.Text = Server.HtmlEncode(ma(cnt).Headers("Reply-To"))
  106                     c.HorizontalAlign = HorizontalAlign.Left
  107                     c.VerticalAlign = VerticalAlign.Top
  108                     r.Cells.Add(c)
  109                     AnswerTable.Rows.Add(r)
  110 
  111                     r = new TableRow()
  112                     c = new TableCell()
  113                     c.Text = "Message ID:"
  114                     c.HorizontalAlign = HorizontalAlign.Left
  115                     c.VerticalAlign = VerticalAlign.Top
  116                     c.Font.Bold = true
  117                     r.Cells.Add(c)
  118                     c = new TableCell()
  119                     c.Text = Server.HtmlEncode(ma(cnt).Headers("Message-ID"))
  120                     c.HorizontalAlign = HorizontalAlign.Left
  121                     c.VerticalAlign = VerticalAlign.Top
  122                     r.Cells.Add(c)
  123                     AnswerTable.Rows.Add(r)
  124 
  125                     r = new TableRow()
  126                     c = new TableCell()
  127                     c.Text = "Date:"
  128                     c.HorizontalAlign = HorizontalAlign.Left
  129                     c.VerticalAlign = VerticalAlign.Top
  130                     c.Font.Bold = true
  131                     r.Cells.Add(c)
  132                     c = new TableCell()
  133                     c.Text = Server.HtmlEncode(ma(cnt).Headers("Date"))
  134                     c.HorizontalAlign = HorizontalAlign.Left
  135                     c.VerticalAlign = VerticalAlign.Top
  136                     r.Cells.Add(c)
  137                     AnswerTable.Rows.Add(r)
  138 
  139                     r = new TableRow()
  140                     c = new TableCell()
  141                     c.Text = Server.HtmlEncode(ma(cnt).Body).Replace(vbCrLf, "<br>")
  142                     c.ColumnSpan = 2
  143                     c.HorizontalAlign = HorizontalAlign.Left
  144                     c.VerticalAlign = VerticalAlign.Top
  145                     c.BackColor = System.Drawing.Color.White
  146                     r.Cells.Add(c)
  147                     AnswerTable.Rows.Add(r)
  148 
  149                 Next
  150                 'show table
  151                 AnswerTable.Visible = true
  152             else
  153                 Answer.Text = "Logon Unsuccessfull"
  154             End if
  155             p.logoff()
  156         End if
  157     End Sub
  158 
  159     class pop3
  160         'class for holding details about each mail
  161         public class MailMessage
  162             Inherits System.Web.Mail.MailMessage
  163 
  164             private _ID as Integer = -1
  165             Property ID() as Integer
  166                 Get
  167                     Return _ID
  168                 End Get
  169                 Set(ByVal Value as Integer)
  170                     _ID = Value
  171                 End Set
  172             End Property
  173 
  174             private _Size as Integer = -1
  175             Property Size() as Integer
  176                 Get
  177                     Return _Size
  178                 End Get
  179                 Set(ByVal Value as Integer)
  180                     _Size = Value
  181                 End Set
  182             End Property
  183         End class
  184 
  185         private function getData() as string
  186             Dim bData(t.ReceiveBufferSize) as byte
  187             getData = ""
  188             Do
  189                 'get the data
  190                 s.Read(bData, 0, bData.Length)
  191                 getData += System.Text.Encoding.ASCII.GetString(bData)
  192                 'clear byte array for next pass
  193                 bData.Clear(bData, 0, bData.Length)
  194                 'wait for the dataavailble flag to get set
  195                 System.Threading.Thread.Sleep(250)
  196                 'if there is more data repeat
  197             Loop while s.DataAvailable
  198         End function
  199 
  200         public function getHeader(ByRef m as MailMessage) as Boolean
  201             Dim h as string
  202 
  203             h = SendCmd("top " + m.ID.ToString + " 0")
  204 
  205             'check the command was accepted OK
  206             if Left(h, 3) = "+OK" Then
  207                 setHeader(m, h)
  208                 Return true
  209             else
  210                 Return false
  211             End if
  212         End function
  213 
  214         public function getMail(ByRef m as MailMessage) as Boolean
  215             Dim msg as string
  216             Dim hend as Integer
  217 
  218             'make sure the buffer is big enough to hold the message.
  219             'to be sure make it just a little bigger
  220             't.ReceiveBufferSize = m.Size
  221             msg = SendCmd("retr " + m.ID.ToString + " 0")
  222 
  223             'check the command was accepted OK
  224             if Left(msg, 3) = "+OK" Then
  225                 'find the end of the header.
  226                 'this is denoted by the first empty line in the message
  227                 hend = msg.IndexOf(vbCrLf + vbCrLf)
  228 
  229                 'set the header
  230                 setHeader(m, Left(msg, hend))
  231 
  232                 'set the mail body
  233                 'this is everything after the header.
  234                 m.Body = msg.Substring(hend + 4)
  235                 Return true
  236             else
  237                 Return false
  238             End if
  239         End function
  240 
  241         private Sub setHeader(ByRef m as MailMessage, ByVal h as string)
  242             'extract standard headers values from header string using regular expressions
  243             'and save to the mailmessage class
  244             m.To = Regex.Match(h, "/nTo:(?<to>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("to").Value
  245             m.From = Regex.Match(h, "/nFrom:(?<from>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("from").Value
  246             m.Cc = Regex.Match(h, "/nCC:(?<cc>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("cc").Value
  247             m.Subject = Regex.Match(h, "/nSubject:(?<subject>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("subject").Value
  248 
  249             'clear existing header values
  250             'this is just incase the same mailmessage instance is being reused
  251             'for different mails
  252             m.Headers.Clear()
  253 
  254             'extract less standard headers values from header string using regular expressions
  255             'and add to the mailmessage.headers.
  256             m.Headers.Add("Reply-To", Regex.Match(h, "/nReply-To:(?<ReplyTo>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("ReplyTo").Value)
  257             m.Headers.Add("Message-ID", Regex.Match(h, "/nMessage-ID:(?<MessageID>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("MessageID").Value)
  258             m.Headers.Add("Date", Regex.Match(h, "/nDate:(?<Date>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("Date").Value)
  259         End Sub
  260 
  261         '-- the code below is as before .
  262 
  263         'a private holder for the number of mails
  264         private _MessageCount as Integer = -1
  265         readonly Property MessageCount() as Integer
  266             Get
  267                 'if the count has not yet been set, request details from server
  268                 if _MessageCount = -1 Then
  269                     GetStats()
  270                 End if
  271                 Return _MessageCount
  272             End Get
  273         End Property
  274 
  275         'private holder for mailbox size
  276         private _MailBoxSize as Integer = -1
  277         readonly Property MailBoxSize() as Integer
  278             Get
  279                 'if the size has not yet been set, request details from server
  280                 if _MailBoxSize = -1 Then
  281                     GetStats()
  282                 End if
  283                 Return _MailBoxSize
  284             End Get
  285         End Property
  286 
  287         private Sub GetStats()
  288             Dim a as string()
  289             'issue the stat command to the server
  290             'split the returned string into an array
  291             a = SendCmd("stat").Split(" ")
  292 
  293             'set the count
  294             _MessageCount = CType(a(1), Integer)
  295             'set the size
  296             _MailBoxSize = CType(a(2), Integer)
  297         End Sub
  298 
  299         public function getSize(ByVal m as MailMessage) as Boolean
  300             Dim a() as string
  301 
  302             'perform the list command
  303             a = SendCmd("list " + m.ID.ToString).Split(" ")
  304 
  305             'if the command was ok set the message size
  306             if a(0) = "+OK" Then
  307                 m.Size = CType(a(2), Integer)
  308                 Return true
  309                 'else indicate the mail doesn't exist
  310             else
  311                 m.ID = -1
  312                 m.Size = 0
  313                 Return false
  314             End if
  315         End function
  316 
  317         private s as System.Net.Sockets.NetworkStream
  318         private t as new System.Net.Sockets.TcpClient()
  319         private Cnct as Boolean = false
  320 
  321         public Sub new(ByVal Server as string)
  322             'open port 110 ( the pop3 port ) On the server
  323             Try
  324                 'catch any error resuting from a bad server name
  325                 t.Connect(Server, 110)
  326                 s = t.GetStream()
  327                 'check that the connection is okay
  328                 if Left(getData(), 3) = "+OK" Then
  329                     Cnct = true
  330                 End if
  331             catch
  332             End Try
  333         End Sub
  334 
  335         public function logon(ByVal User as stringByVal passwd as stringas Boolean
  336             Dim ret as string
  337 
  338             logon = false
  339 
  340             'make sure you have a connection
  341             if Cnct Then
  342                 'send the username
  343                 ret = SendCmd("user " + User)
  344 
  345                 'if that was successfull, send the password
  346                 if Left(ret, 3) = "+OK" Then
  347                     ret = SendCmd("pass " + passwd)
  348 
  349                     'if that was successfull set the return flas to true
  350                     if Left(ret, 3) = "+OK" Then
  351                         logon = true
  352                     End if
  353                 End if
  354             End if
  355         End function
  356 
  357         public function logoff() as string
  358             if Cnct Then
  359                 logoff = SendCmd("QUIT")
  360             End if
  361         End function
  362 
  363         private function SendCmd(ByVal Cmd as stringas string
  364             Dim bCmd as byte()
  365             'byte encode the command
  366             bCmd = System.Text.Encoding.ASCII.GetBytes(Cmd + vbCrLf)
  367 
  368             'send the data
  369             s.Write(bCmd, 0, bCmd.Length)
  370             SendCmd = getData()
  371         End function
  372     End class
  373 End class
  374 
PopMail.aspx.vb (Retrieve Message and Headers)
  1 Imports System.Text.RegularExpressions
  2 public class PopMail4
  3     Inherits System.Web.UI.Page
  4     protected WithEvents Srv as System.Web.UI.WebControls.TextBox
  5     protected WithEvents Passwd as System.Web.UI.WebControls.TextBox
  6     protected WithEvents Button1 as System.Web.UI.WebControls.Button
  7     protected WithEvents Usr as System.Web.UI.WebControls.TextBox
  8     protected WithEvents AnswerTable as System.Web.UI.WebControls.Table
  9     protected WithEvents Answer as System.Web.UI.WebControls.Literal
  10     protected WithEvents MsgCount as System.Web.UI.WebControls.TableCell
  11     protected WithEvents Alliance1 as DPM.Alliance
  12     protected WithEvents Alliance2 as DPM.Alliance
  13     protected WithEvents MsgBoxSize as System.Web.UI.WebControls.TableCell
  14 
  15     private Sub Page_Load(ByVal sender as System.objectByVal e as System.EventArgs) Handles MyBase.Load
  16         if Page.IsPostBack Then
  17             Dim p as new pop3(Srv.Text)
  18 
  19             Dim success as Boolean = false
  20 
  21             success = p.logon(Usr.Text, Passwd.Text)
  22             if success Then
  23                 Answer.Text = "Logon Successfull"
  24 
  25                 'create and array for the mail details
  26                 Dim ma(p.MessageCount) as pop3.MailMessage
  27                 Dim r as TableRow
  28                 Dim c as TableCell
  29                 'cycle through mails, diplsaying size and id
  30                 Dim cnt as Integer
  31                 for cnt = 1 To p.MessageCount
  32                     'create a new instane of mail in the array
  33                     ma(cnt) = new pop3.MailMessage()
  34                     ma(cnt).ID = cnt
  35                     'get the mail message in full
  36                     p.getMail(ma(cnt))
  37 
  38                     'display the returned values
  39                     'these have been htmlencoded to prevent problems with being display
  40                     'in a web page
  41                     r = new TableRow()
  42                     c = new TableCell()
  43                     c.Text = "To:"
  44                     c.HorizontalAlign = HorizontalAlign.Left
  45                     c.VerticalAlign = VerticalAlign.Top
  46                     c.Font.Bold = true
  47                     r.Cells.Add(c)
  48                     c = new TableCell()
  49                     c.Text = Server.HtmlEncode(ma(cnt).To)
  50                     c.HorizontalAlign = HorizontalAlign.Left
  51                     c.VerticalAlign = VerticalAlign.Top
  52                     r.Cells.Add(c)
  53                     AnswerTable.Rows.Add(r)
  54 
  55                     r = new TableRow()
  56                     c = new TableCell()
  57                     c.Text = "Cc:"
  58                     c.HorizontalAlign = HorizontalAlign.Left
  59                     c.VerticalAlign = VerticalAlign.Top
  60                     c.Font.Bold = true
  61                     r.Cells.Add(c)
  62                     c = new TableCell()
  63                     c.Text = Server.HtmlEncode(ma(cnt).Cc)
  64                     c.HorizontalAlign = HorizontalAlign.Left
  65                     c.VerticalAlign = VerticalAlign.Top
  66                     r.Cells.Add(c)
  67                     AnswerTable.Rows.Add(r)
  68 
  69                     r = new TableRow()
  70                     c = new TableCell()
  71                     c.Text = "From:"
  72                     c.HorizontalAlign = HorizontalAlign.Left
  73                     c.VerticalAlign = VerticalAlign.Top
  74                     c.Font.Bold = true
  75                     r.Cells.Add(c)
  76                     c = new TableCell()
  77                     c.Text = Server.HtmlEncode(ma(cnt).From)
  78                     c.HorizontalAlign = HorizontalAlign.Left
  79                     c.VerticalAlign = VerticalAlign.Top
  80                     r.Cells.Add(c)
  81                     AnswerTable.Rows.Add(r)
  82 
  83                     r = new TableRow()
  84                     c = new TableCell()
  85                     c.Text = "Subject:"
  86                     c.HorizontalAlign = HorizontalAlign.Left
  87                     c.VerticalAlign = VerticalAlign.Top
  88                     c.Font.Bold = true
  89                     r.Cells.Add(c)
  90                     c = new TableCell()
  91                     c.Text = Server.HtmlEncode(ma(cnt).Subject)
  92                     c.HorizontalAlign = HorizontalAlign.Left
  93                     c.VerticalAlign = VerticalAlign.Top
  94                     r.Cells.Add(c)
  95                     AnswerTable.Rows.Add(r)
  96 
  97                     r = new TableRow()
  98                     c = new TableCell()
  99                     c.Text = "Reply To:"
  100                     c.HorizontalAlign = HorizontalAlign.Left
  101                     c.VerticalAlign = VerticalAlign.Top
  102                     c.Font.Bold = true
  103                     r.Cells.Add(c)
  104                     c = new TableCell()
  105                     c.Text = Server.HtmlEncode(ma(cnt).Headers("Reply-To"))
  106                     c.HorizontalAlign = HorizontalAlign.Left
  107                     c.VerticalAlign = VerticalAlign.Top
  108                     r.Cells.Add(c)
  109                     AnswerTable.Rows.Add(r)
  110 
  111                     r = new TableRow()
  112                     c = new TableCell()
  113                     c.Text = "Message ID:"
  114                     c.HorizontalAlign = HorizontalAlign.Left
  115                     c.VerticalAlign = VerticalAlign.Top
  116                     c.Font.Bold = true
  117                     r.Cells.Add(c)
  118                     c = new TableCell()
  119                     c.Text = Server.HtmlEncode(ma(cnt).Headers("Message-ID"))
  120                     c.HorizontalAlign = HorizontalAlign.Left
  121                     c.VerticalAlign = VerticalAlign.Top
  122                     r.Cells.Add(c)
  123                     AnswerTable.Rows.Add(r)
  124 
  125                     r = new TableRow()
  126                     c = new TableCell()
  127                     c.Text = "Date:"
  128                     c.HorizontalAlign = HorizontalAlign.Left
  129                     c.VerticalAlign = VerticalAlign.Top
  130                     c.Font.Bold = true
  131                     r.Cells.Add(c)
  132                     c = new TableCell()
  133                     c.Text = Server.HtmlEncode(ma(cnt).Headers("Date"))
  134                     c.HorizontalAlign = HorizontalAlign.Left
  135                     c.VerticalAlign = VerticalAlign.Top
  136                     r.Cells.Add(c)
  137                     AnswerTable.Rows.Add(r)
  138 
  139                     r = new TableRow()
  140                     c = new TableCell()
  141                     c.Text = Server.HtmlEncode(ma(cnt).Body).Replace(vbCrLf, "<br>")
  142                     c.ColumnSpan = 2
  143                     c.HorizontalAlign = HorizontalAlign.Left
  144                     c.VerticalAlign = VerticalAlign.Top
  145                     c.BackColor = System.Drawing.Color.White
  146                     r.Cells.Add(c)
  147                     AnswerTable.Rows.Add(r)
  148 
  149                 Next
  150                 'show table
  151                 AnswerTable.Visible = true
  152             else
  153                 Answer.Text = "Logon Unsuccessfull"
  154             End if
  155             p.logoff()
  156         End if
  157     End Sub
  158 
  159     class pop3
  160         'class for holding details about each mail
  161         public class MailMessage
  162             Inherits System.Web.Mail.MailMessage
  163 
  164             private _ID as Integer = -1
  165             Property ID() as Integer
  166                 Get
  167                     Return _ID
  168                 End Get
  169                 Set(ByVal Value as Integer)
  170                     _ID = Value
  171                 End Set
  172             End Property
  173 
  174             private _Size as Integer = -1
  175             Property Size() as Integer
  176                 Get
  177                     Return _Size
  178                 End Get
  179                 Set(ByVal Value as Integer)
  180                     _Size = Value
  181                 End Set
  182             End Property
  183         End class
  184 
  185         private function getData() as string
  186             Dim bData(t.ReceiveBufferSize) as byte
  187             getData = ""
  188             Do
  189                 'get the data
  190                 s.Read(bData, 0, bData.Length)
  191                 getData += System.Text.Encoding.ASCII.GetString(bData)
  192                 'clear byte array for next pass
  193                 bData.Clear(bData, 0, bData.Length)
  194                 'wait for the dataavailble flag to get set
  195                 System.Threading.Thread.Sleep(250)
  196                 'if there is more data repeat
  197             Loop while s.DataAvailable
  198         End function
  199 
  200         public function getHeader(ByRef m as MailMessage) as Boolean
  201             Dim h as string
  202 
  203             h = SendCmd("top " + m.ID.ToString + " 0")
  204 
  205             'check the command was accepted OK
  206             if Left(h, 3) = "+OK" Then
  207                 setHeader(m, h)
  208                 Return true
  209             else
  210                 Return false
  211             End if
  212         End function
  213 
  214         public function getMail(ByRef m as MailMessage) as Boolean
  215             Dim msg as string
  216             Dim hend as Integer
  217 
  218             'make sure the buffer is big enough to hold the message.
  219             'to be sure make it just a little bigger
  220             't.ReceiveBufferSize = m.Size
  221             msg = SendCmd("retr " + m.ID.ToString + " 0")
  222 
  223             'check the command was accepted OK
  224             if Left(msg, 3) = "+OK" Then
  225                 'find the end of the header.
  226                 'this is denoted by the first empty line in the message
  227                 hend = msg.IndexOf(vbCrLf + vbCrLf)
  228 
  229                 'set the header
  230                 setHeader(m, Left(msg, hend))
  231 
  232                 'set the mail body
  233                 'this is everything after the header.
  234                 m.Body = msg.Substring(hend + 4)
  235                 Return true
  236             else
  237                 Return false
  238             End if
  239         End function
  240 
  241         private Sub setHeader(ByRef m as MailMessage, ByVal h as string)
  242             'extract standard headers values from header string using regular expressions
  243             'and save to the mailmessage class
  244             m.To = Regex.Match(h, "/nTo:(?<to>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("to").Value
  245             m.From = Regex.Match(h, "/nFrom:(?<from>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("from").Value
  246             m.Cc = Regex.Match(h, "/nCC:(?<cc>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("cc").Value
  247             m.Subject = Regex.Match(h, "/nSubject:(?<subject>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("subject").Value
  248 
  249             'clear existing header values
  250             'this is just incase the same mailmessage instance is being reused
  251             'for different mails
  252             m.Headers.Clear()
  253 
  254             'extract less standard headers values from header string using regular expressions
  255             'and add to the mailmessage.headers.
  256             m.Headers.Add("Reply-To", Regex.Match(h, "/nReply-To:(?<ReplyTo>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("ReplyTo").Value)
  257             m.Headers.Add("Message-ID", Regex.Match(h, "/nMessage-ID:(?<MessageID>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("MessageID").Value)
  258             m.Headers.Add("Date", Regex.Match(h, "/nDate:(?<Date>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("Date").Value)
  259         End Sub
  260 
  261         '-- the code below is as before .
  262 
  263         'a private holder for the number of mails
  264         private _MessageCount as Integer = -1
  265         readonly Property MessageCount() as Integer
  266             Get
  267                 'if the count has not yet been set, request details from server
  268                 if _MessageCount = -1 Then
  269                     GetStats()
  270                 End if
  271                 Return _MessageCount
  272             End Get
  273         End Property
  274 
  275         'private holder for mailbox size
  276         private _MailBoxSize as Integer = -1
  277         readonly Property MailBoxSize() as Integer
  278             Get
  279                 'if the size has not yet been set, request details from server
  280                 if _MailBoxSize = -1 Then
  281                     GetStats()
  282                 End if
  283                 Return _MailBoxSize
  284             End Get
  285         End Property
  286 
  287         private Sub GetStats()
  288             Dim a as string()
  289             'issue the stat command to the server
  290             'split the returned string into an array
  291             a = SendCmd("stat").Split(" ")
  292 
  293             'set the count
  294             _MessageCount = CType(a(1), Integer)
  295             'set the size
  296             _MailBoxSize = CType(a(2), Integer)
  297         End Sub
  298 
  299         public function getSize(ByVal m as MailMessage) as Boolean
  300             Dim a() as string
  301 
  302             'perform the list command
  303             a = SendCmd("list " + m.ID.ToString).Split(" ")
  304 
  305             'if the command was ok set the message size
  306             if a(0) = "+OK" Then
  307                 m.Size = CType(a(2), Integer)
  308                 Return true
  309                 'else indicate the mail doesn't exist
  310             else
  311                 m.ID = -1
  312                 m.Size = 0
  313                 Return false
  314             End if
  315         End function
  316 
  317         private s as System.Net.Sockets.NetworkStream
  318         private t as new System.Net.Sockets.TcpClient()
  319         private Cnct as Boolean = false
  320 
  321         public Sub new(ByVal Server as string)
  322             'open port 110 ( the pop3 port ) On the server
  323             Try
  324                 'catch any error resuting from a bad server name
  325                 t.Connect(Server, 110)
  326                 s = t.GetStream()
  327                 'check that the connection is okay
  328                 if Left(getData(), 3) = "+OK" Then
  329                     Cnct = true
  330                 End if
  331             catch
  332             End Try
  333         End Sub
  334 
  335         public function logon(ByVal User as stringByVal passwd as stringas Boolean
  336             Dim ret as string
  337 
  338             logon = false
  339 
  340             'make sure you have a connection
  341             if Cnct Then
  342                 'send the username
  343                 ret = SendCmd("user " + User)
  344 
  345                 'if that was successfull, send the password
  346                 if Left(ret, 3) = "+OK" Then
  347                     ret = SendCmd("pass " + passwd)
  348 
  349                     'if that was successfull set the return flas to true
  350                     if Left(ret, 3) = "+OK" Then
  351                         logon = true
  352                     End if
  353                 End if
  354             End if
  355         End function
  356 
  357         public function logoff() as string
  358             if Cnct Then
  359                 logoff = SendCmd("QUIT")
  360             End if
  361         End function
  362 
  363         private function SendCmd(ByVal Cmd as stringas string
  364             Dim bCmd as byte()
  365             'byte encode the command
  366             bCmd = System.Text.Encoding.ASCII.GetBytes(Cmd + vbCrLf)
  367 
  368             'send the data
  369             s.Write(bCmd, 0, bCmd.Length)
  370             SendCmd = getData()
  371         End function
  372     End class
  373 End class
  

PopMail.aspx.vb (Deleting Messages)
  1 Imports System.Text.RegularExpressions
  2 public class PopMail5
  3     Inherits System.Web.UI.Page
  4     protected WithEvents Srv as System.Web.UI.WebControls.TextBox
  5     protected WithEvents Passwd as System.Web.UI.WebControls.TextBox
  6     protected WithEvents Button1 as System.Web.UI.WebControls.Button
  7     protected WithEvents Usr as System.Web.UI.WebControls.TextBox
  8     protected WithEvents AnswerTable as System.Web.UI.WebControls.Table
  9     protected WithEvents Answer as System.Web.UI.WebControls.Literal
  10     protected WithEvents MsgCount as System.Web.UI.WebControls.TableCell
  11     protected WithEvents Alliance1 as DPM.Alliance
  12     protected WithEvents Alliance2 as DPM.Alliance
  13     protected WithEvents MsgBoxSize as System.Web.UI.WebControls.TableCell
  14 
  15     private Sub Page_Load(ByVal sender as System.objectByVal e as System.EventArgs) Handles MyBase.Load
  16         if Page.IsPostBack Then
  17             Dim p as new pop3(Srv.Text)
  18 
  19             Dim success as Boolean = false
  20 
  21             success = p.logon(Usr.Text, Passwd.Text)
  22             if success Then
  23                 Answer.Text = "Logon Successfull"
  24                 Button1.Text = "Get List / Delete Messages"
  25                 'create and array for the mail details
  26                 Dim ma(p.MessageCount) as pop3.MailMessage
  27                 Dim r as TableRow
  28                 Dim c as TableCell
  29                 Dim cb as HtmlInputCheckBox
  30                 'cycle through mails, diplsaying size and id
  31                 Dim cnt as Integer
  32                 for cnt = 1 To p.MessageCount
  33                     'create a new instane of mail in the array
  34                     ma(cnt) = new pop3.MailMessage()
  35                     ma(cnt).ID = cnt
  36                     'get the mail header and size
  37                     p.getHeader(ma(cnt))
  38                     p.getSize(ma(cnt))
  39 
  40                     r = new TableRow()
  41                     'check wether the mail has been marked for deletion
  42                     if Request.Form.Item(ma(cnt).Headers("Message-ID")) <> "" Then
  43                         'if it has, delete it a indicate its deletion.
  44                         if p.delete(ma(cnt)) Then
  45                             r.Font.Strikeout = true
  46                         End if
  47                     End if
  48 
  49                     c = new TableCell()
  50                     c.Text = ma(cnt).Subject
  51                     c.HorizontalAlign = HorizontalAlign.Left
  52                     c.VerticalAlign = VerticalAlign.Top
  53                     c.BorderWidth = Unit.Pixel(1)
  54                     r.Cells.Add(c)
  55 
  56                     c = new TableCell()
  57                     c.Text = ma(cnt).Size.ToString
  58                     c.HorizontalAlign = HorizontalAlign.Left
  59                     c.VerticalAlign = VerticalAlign.Top
  60                     c.BorderWidth = Unit.Pixel(1)
  61                     r.Cells.Add(c)
  62 
  63                     c = new TableCell()
  64                     cb = new HtmlInputCheckBox()
  65                     cb.ID = ma(cnt).Headers("Message-ID")
  66                     c.Controls.Add(cb)
  67                     c.HorizontalAlign = HorizontalAlign.Left
  68                     c.VerticalAlign = VerticalAlign.Top
  69                     c.BorderWidth = Unit.Pixel(1)
  70                     r.Cells.Add(c)
  71 
  72                     AnswerTable.Rows.Add(r)
  73                 Next
  74                 'show table
  75                 AnswerTable.Visible = true
  76             else
  77                 Answer.Text = "Logon Unsuccessfull"
  78             End if
  79             p.logoff()
  80         End if
  81     End Sub
  82 
  83 
  84     class pop3
  85         public function delete(ByRef m as MailMessage) as Boolean
  86             'perform the dele [n] command
  87             if Left(SendCmd("dele " + m.ID.ToString), 3) = "+OK" Then
  88                 'if it completed successfully return true
  89                 Return true
  90             else
  91                 Return false
  92             End if
  93         End function
  94 
  95         '-- the code below is as before .
  96 
  97         'class for holding details about each mail
  98         public class MailMessage
  99             Inherits System.Web.Mail.MailMessage
  100 
  101             private _ID as Integer = -1
  102             Property ID() as Integer
  103                 Get
  104                     Return _ID
  105                 End Get
  106                 Set(ByVal Value as Integer)
  107                     _ID = Value
  108                 End Set
  109             End Property
  110 
  111             private _Size as Integer = -1
  112             Property Size() as Integer
  113                 Get
  114                     Return _Size
  115                 End Get
  116                 Set(ByVal Value as Integer)
  117                     _Size = Value
  118                 End Set
  119             End Property
  120         End class
  121 
  122         private function getData() as string
  123             Dim bData(t.ReceiveBufferSize) as byte
  124             getData = ""
  125             Do
  126                 'get the data
  127                 s.Read(bData, 0, bData.Length)
  128                 getData += System.Text.Encoding.ASCII.GetString(bData)
  129                 'clear byte array for next pass
  130                 bData.Clear(bData, 0, bData.Length)
  131                 'wait for the dataavailble flag to get set
  132                 System.Threading.Thread.Sleep(250)
  133                 'if there is more data repeat
  134             Loop while s.DataAvailable
  135         End function
  136 
  137         public function getHeader(ByRef m as MailMessage) as Boolean
  138             Dim h as string
  139 
  140             h = SendCmd("top " + m.ID.ToString + " 0")
  141 
  142             'check the command was accepted OK
  143             if Left(h, 3) = "+OK" Then
  144                 setHeader(m, h)
  145                 Return true
  146             else
  147                 Return false
  148             End if
  149         End function
  150 
  151         public function getMail(ByRef m as MailMessage) as Boolean
  152             Dim msg as string
  153             Dim hend as Integer
  154 
  155             'if you haven't already go the message size
  156             'get it
  157             if m.Size = -1 Then
  158                 getSize(m)
  159             End if
  160 
  161             'make sure the buffer is big enough to hold the message.
  162             'to be sure make it just a little bigger
  163             't.ReceiveBufferSize = m.Size
  164             msg = SendCmd("retr " + m.ID.ToString + " 0")
  165 
  166             'check the command was accepted OK
  167             if Left(msg, 3) = "+OK" Then
  168                 'find the end of the header.
  169                 'this is denoted by the first empty line in the message
  170                 hend = msg.IndexOf(vbCrLf + vbCrLf)
  171 
  172                 'set the header
  173                 setHeader(m, Left(msg, hend))
  174 
  175                 'set the mail body
  176                 'this is everything after the header.
  177                 m.Body = msg.Substring(hend + 4)
  178                 Return true
  179             else
  180                 Return false
  181             End if
  182         End function
  183 
  184         private Sub setHeader(ByRef m as MailMessage, ByVal h as string)
  185             'extract standard headers values from header string using regular expressions
  186             'and save to the mailmessage class
  187             m.To = Regex.Match(h, "/nTo:(?<to>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("to").Value
  188             m.From = Regex.Match(h, "/nFrom:(?<from>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("from").Value
  189             m.Cc = Regex.Match(h, "/nCC:(?<cc>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("cc").Value
  190             m.Subject = Regex.Match(h, "/nSubject:(?<subject>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("subject").Value
  191 
  192             'clear existing header values
  193             'this is just incase the same mailmessage instance is being reused
  194             'for different mails
  195             m.Headers.Clear()
  196 
  197             'extract less standard headers values from header string using regular expressions
  198             'and add to the mailmessage.headers.
  199             m.Headers.Add("Reply-To", Regex.Match(h, "/nReply-To:(?<ReplyTo>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("ReplyTo").Value)
  200             m.Headers.Add("Message-ID", Regex.Match(h, "/nMessage-ID:(?<MessageID>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("MessageID").Value)
  201             m.Headers.Add("Date", Regex.Match(h, "/nDate:(?<Date>[^/n]+)/n", RegexOptions.IgnoreCase).Groups("Date").Value)
  202         End Sub
  203 
  204         'a private holder for the number of mails
  205         private _MessageCount as Integer = -1
  206         readonly Property MessageCount() as Integer
  207             Get
  208                 'if the count has not yet been set, request details from server
  209                 if _MessageCount = -1 Then
  210                     GetStats()
  211                 End if
  212                 Return _MessageCount
  213             End Get
  214         End Property
  215 
  216         'private holder for mailbox size
  217         private _MailBoxSize as Integer = -1
  218         readonly Property MailBoxSize() as Integer
  219             Get
  220                 'if the size has not yet been set, request details from server
  221                 if _MailBoxSize = -1 Then
  222                     GetStats()
  223                 End if
  224                 Return _MailBoxSize
  225             End Get
  226         End Property
  227 
  228         private Sub GetStats()
  229             Dim a as string()
  230             'issue the stat command to the server
  231             'split the returned string into an array
  232             a = SendCmd("stat").Split(" ")
  233 
  234             'set the count
  235             _MessageCount = CType(a(1), Integer)
  236             'set the size
  237             _MailBoxSize = CType(a(2), Integer)
  238         End Sub
  239 
  240         public function getSize(ByVal m as MailMessage) as Boolean
  241             Dim a() as string
  242 
  243             'perform the list command
  244             a = SendCmd("list " + m.ID.ToString).Split(" ")
  245 
  246             'if the command was ok set the message size
  247             if a(0) = "+OK" Then
  248                 m.Size = CType(a(2), Integer)
  249                 Return true
  250                 'else indicate the mail doesn't exist
  251             else
  252                 m.ID = -1
  253                 m.Size = 0
  254                 Return false
  255             End if
  256         End function
  257 
  258         private s as System.Net.Sockets.NetworkStream
  259         private t as new System.Net.Sockets.TcpClient()
  260         private Cnct as Boolean = false
  261 
  262         public Sub new(ByVal Server as string)
  263             'open port 110 ( the pop3 port ) On the server
  264             Try
  265                 'catch any error resuting from a bad server name
  266                 t.Connect(Server, 110)
  267                 s = t.GetStream()
  268                 'check that the connection is okay
  269                 if Left(getData(), 3) = "+OK" Then
  270                     Cnct = true
  271                 End if
  272             catch
  273             End Try
  274         End Sub
  275 
  276         public function logon(ByVal User as stringByVal passwd as stringas Boolean
  277             Dim ret as string
  278 
  279             logon = false
  280 
  281             'make sure you have a connection
  282             if Cnct Then
  283                 'send the username
  284                 ret = SendCmd("user " + User)
  285 
  286                 'if that was successfull, send the password
  287                 if Left(ret, 3) = "+OK" Then
  288                     ret = SendCmd("pass " + passwd)
  289 
  290                     'if that was successfull set the return flas to true
  291                     if Left(ret, 3) = "+OK" Then
  292                         logon = true
  293                     End if
  294                 End if
  295             End if
  296         End function
  297 
  298         public function logoff() as string
  299             if Cnct Then
  300                 logoff = SendCmd("QUIT")
  301             End if
  302         End function
  303 
  304         private function SendCmd(ByVal Cmd as stringas string
  305             Dim bCmd as byte()
  306             'byte encode the command
  307             bCmd = System.Text.Encoding.ASCII.GetBytes(Cmd + vbCrLf)
  308 
  309             'send the data
  310             s.Write(bCmd, 0, bCmd.Length)
  311             SendCmd = getData()
  312         End function
  313     End class
  314 
  315     private Sub InitializeComponent()
  316 
  317     End Sub
  318 End class
  319 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值