| 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.object, ByVal 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 string, ByVal passwd as string) as 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 string) as 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 | |