Logging On & Logging Off
Popmail.aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <%@ Control Language="vb" AutoEventWireup="false" Codebehind="PopMail.ascx.vb" Inherits="code.PopMail1" %> <form id="Form1" method="post" runat="server"> <table width="100%"> <tr> <TD align="right" width="50%">Server :</TD> <td align="left" width="50%"> <asp:TextBox id="Srv" runat="server" Width="328px"></asp:TextBox> </td> </tr> <TR> <TD align="right" width="50%">User :</TD> <TD align="left" width="50%"> <asp:TextBox id="Usr" runat="server" Width="328px" Wrap="False"></asp:TextBox></TD> </TR> <TR> <TD align="right" width="50%">Password :</TD> <TD align="left" width="50%"> <asp:TextBox id="Passwd" runat="server" Width="328px" TextMode="Password"></asp:TextBox></TD> </TR> <TR> <TD align="right" width="50%"></TD> <TD align="left" width="50%"> <asp:Button id="Button1" runat="server" Text="Check Logon"></asp:Button></TD> </TR> <TR> <TD align="middle" colSpan="2"> <asp:Literal id="Answer" runat="server" Text="&nbsp;"></asp:Literal></TD> </TR> </table> </form> |
Popmail.aspx,vb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | Public Class PopMail1 Inherits System.Web.UI.Control Protected WithEvents Srv As System.Web.UI.WebControls.TextBox Protected WithEvents Passwd As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Answer As System.Web.UI.WebControls.Literal Protected WithEvents Usr As System.Web.UI.WebControls.TextBox Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Page.IsPostBack Then Dim p As New pop3(Srv.Text) Dim success As Boolean = False success = p.logon(Usr.Text, Passwd.Text) p.logoff() If success Then Answer.Text = "Logon Successfull" Else Answer.Text = "Logon Unsuccessfull" End If End If End Sub Class pop3 Private s As System.Net.Sockets.NetworkStream Private t As New System.Net.Sockets.TcpClient() Private Cnct As Boolean = False Public Sub New(ByVal Server As String) 'open port 110 ( the pop3 port ) on the server Try 'catch any error resuting from a bad server name t.Connect(Server, 110) s = t.GetStream() 'check that the connection is okay If Left(getData(), 3) = "+OK" Then Cnct = True End If Catch End Try End Sub Public Function logon(ByVal User As String, ByVal passwd As String) As Boolean Dim ret As String logon = False 'make sure you have a connection If Cnct Then 'send the username ret = SendCmd("user " + User) 'if that was successfull, send the password If Left(ret, 3) = "+OK" Then ret = SendCmd("pass " + passwd) 'if that was successfull set the return flas to true If Left(ret, 3) = "+OK" Then logon = True End If End If End If End Function Public Function logoff() As String If Cnct Then logoff = SendCmd("QUIT") End If End Function Private Function SendCmd(ByVal Cmd As String) As String Dim bCmd As Byte() 'byte encode the command bCmd = System.Text.Encoding.ASCII.GetBytes(Cmd + vbCrLf) 'send the data s.Write(bCmd, 0, bCmd.Length) SendCmd = getData() End Function Private Function getData() As String Dim bData(t.ReceiveBufferSize) As Byte 'get the response s.Read(bData, 0, bData.Length) 'return the response getData = System.Text.Encoding.ASCII.GetString(bData) End Function End Class End Class |



















