Now we shall look into retrieving the mail message from the server, and then go on to extract the header information.
You can see that we have now modified the MailMessage Class.
|
|
161 |
public class MailMessage |
162 |
Inherits System.Web.Mail.MailMessage | |
Code Sample1 |
Generated using CodeView |
The class now inherits from the SMTP MailMessage class. This means that it now can hold all the information of the email without us having to re-invent the wheel; it also makes it easy to pass the mail back out using the SMTP Mail class.
Now that we are going to be retrieving the entire message we can expect to receive a lot more data. The Getdata function has been enhanced to be able to read more than just a short reply.
|
|
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 | |
Code sample2 |
Generated using CodeView |
The message stream is read in a loop until there is no more data. There is a small delay to allow the DataAvailable flag to be set, 250 milliseconds seems more adequate, unless you have anti-virus software that scans emails, in which you may need to increase this.
There 2 new public functions in the class, getHeader and getMail. getHeader uses the TOP pop3 command to retrieve just the header component of the mail message. Where as getMail users the RETR pop3 command to retrieve the entire message and then seperates it into its Header and Body parts. Both functions use the private setHeader function to extract the header information into the MailMessage.
The setHeader function extracts the standard header information and a few of the more useful ones such as Message-ID. The mail Header and does contain a lot more information, as to whether is useful is up to you.
The example below shows the getMail function in action. You can see getHeader function in action in the next part where we go on to delete messages.