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