• The eternal confessions of a beautiful mind...
  • DamianM.Co.UK
  • Home
  • About
  • Archives
  • Contact
  • Sitemap
  • My Flickr

    IMG_9585IMG_9115IMG_9113IMG_9111IMG_9078IMG_9075IMG_9069IMG_9065IMG_9041IMG_9032IMG_8963IMG_8928IMG_8916IMG_8915IMG_8904IMG_8876IMG_8858IMG_8830IMG_8828IMG_8826

  • Recent Posts

    • New Ohio Roller Coaster - INSANE!!!
    • Speeding
    • Captions not required
    • Unfortunate Backgrounds
    • Unfortunate Signs
    • OMG!!
    • Women as explained by engineer
    • The Monty Hall Problem
    • ahhh still love them - Motivational Posters
    • What does Mona Lisa do when the Museum is closed………
    • 21st Century kids books
    • Cool Origami
    • AWESOME Pictures!
    • Things you shouldn’t find in your vegetable patch!
    • World’s Best Graffiti…?
  • My Tools

    • Blog_LinkIt
    • DCoda Theme
    • DCoda Widgets
    • RSS_Sticky
    • WordPress.org
    • WP_BlogNetworking
    • WP_BlogRollSync
    • WP_BoilerPlate
    • WP_Censor
    • WP_ContactMe
    • WP_DeliciousPost
    • WP_EasyReply
    • WP_HeadNFoot
    • WP_LinkIt
    • WP_OneInstall
    • WP_PostDate
    • WP_PostNotes
    • WP_RssSticky
    • WP_Spoiler
    • WP_Submission
  • My Web

    • ASPAlliance
    • ClaimID
    • del.ico.us
    • Digg
    • DSLRBlog
    • DVDProfiler
    • Flickr
    • Honeyed SPAM
    • My Blog
    • My company
    • MYSpace
    • WordPress.org
    • YouTube

    POP3 Email

    .NET comes with some new and interesting toys, but the one thing that is missing
    the ability to receive POP3 Mail. Here we will build a class to take care of
    this oversight.

    Logging On & Logging Off

    Here is the code to connect to the server and communicate with it; also, it
    allows you to perform the two most basic mail functions, which are Logging On
    and Logging Off.


    If you check the code you will notice that when responses are checked to be okay we only check
    for the +OK, other information maybe returned after but this is not part of the
    standard and should be ignored.


    Getting MailBox & Message Size

    Once you have logged in the next easiest thing to do is retrieve statistics
    about the mailbox and messages.


    You will find the POP3 class code with three new properties MailBoxSize,
    MessageCount, and a new class MailMessage. Although now it only holds the ID
    and size, we fill go on to add more properties as we retrieve them from the
    server.


    If you check the code you can see in the code that the MailBoxSize and MessageCount properties have
    been written to set the value the first time they are called, this has been
    done to reduce unnecessary traffic. Your mailbox will locked once you have
    logged in and not released until you log out, so remember the quicker you
    perform your task the better. If you wish to experiment remember that if your
    code bombs out whilst you are logged into your mailbox it will remain locked
    until it times out.


    So now, you have the ability to add a “You’ve got mail!” component to you
    homepage, or better identify oversized emails in your mailbox.


    Retrieve Message and Headers

    Now we shall look into retrieving the mail message from the server, and then go
    on to extract the header information.


    If you check the code you can see that we have now modified the MailMessage Class.

    161
    162
    
            Public Class MailMessage
                Inherits System.Web.Mail.MailMessage

    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
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    
            Private Function getData() As String
                Dim bData(t.ReceiveBufferSize) As Byte
                getData = ""
                Do
                    'get the data
                    s.Read(bData, 0, bData.Length)
                    getData += System.Text.Encoding.ASCII.GetString(bData)
                    'clear byte array for next pass
                    bData.Clear(bData, 0, bData.Length)
                    'wait for the dataavailble flag to get set
                    System.Threading.Thread.Sleep(250)
                    'if there is more data repeat
                Loop While s.DataAvailable
            End Function

    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 two 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 uses the RETR pop3 command to retrieve the entire
    message and then separates 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.


    Deleting Messages

    Deleting messages is a simple but important task; here you will see the
    GetHeader function being used to create a list of Messages to be marked for
    deletion by the delete function.


    The delete function uses the DELE pop3 command to delete a mail by using the
    message number, this may be a problem. In a web situation once the list has
    been produced there could be some time until a selection is made. If this is
    the case the message cannot be guaranteed to be in the same position, as there
    is the possibility that the MailBox may have be accessed by another client.


    This being the case, the example uses the Message-ID header value to identify
    the message uniquely. It does mean however that we have to search for the
    message number. This is easily done in the loop that is used to produce the
    list, if a match is found the message is deleted and the list item struck out
    to indicate this.

    42
    43
    44
    45
    46
    47
    48
    
                        'check wether the mail has been marked for deletion
                        If Request.Form.Item(ma(cnt).Headers("Message-ID")) <> "" Then
                            'if it has, delete it a indicate its deletion.
                            If p.delete(ma(cnt)) Then
                                r.Font.Strikeout = True
                            End If
                        End If

    Leave a Reply

    Related Posts from the Past:

    • Email
    • Contact
    • Simple SMTP mailing with .NET
    • Logging On & Logging Off
    • Getting MailBox & Message Size Code